Traversing: $.fn.contents() supports HTMLTemplateElement

Fixes gh-3436
Closes gh-3462
This commit is contained in:
南漂一卒 2017-01-30 06:32:15 +08:00 committed by Oleg Gaidarenko
parent 5f35b5b406
commit 3e3b09d68d
2 changed files with 64 additions and 1 deletions

View File

@ -143,7 +143,18 @@ jQuery.each( {
return siblings( elem.firstChild ); return siblings( elem.firstChild );
}, },
contents: function( elem ) { contents: function( elem ) {
return elem.contentDocument || jQuery.merge( [], elem.childNodes ); if ( jQuery.nodeName( elem, "iframe" ) ) {
return elem.contentDocument;
}
// Support: IE 9 - 11 only, iOS 7 only, Android Browser <=4.3 only
// Treat the template element as a regular one in browsers that
// don't support it.
if ( jQuery.nodeName( elem, "template" ) ) {
elem = elem.content || elem;
}
return jQuery.merge( [], elem.childNodes );
} }
}, function( name, fn ) { }, function( name, fn ) {
jQuery.fn[ name ] = function( until, selector ) { jQuery.fn[ name ] = function( until, selector ) {

View File

@ -743,6 +743,58 @@ QUnit.test( "contents()", function( assert ) {
assert.equal( c[ 0 ].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" ); assert.equal( c[ 0 ].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );
} ); } );
QUnit.test( "contents() for <template />", function( assert ) {
assert.expect( 4 );
jQuery( "#qunit-fixture" ).append(
"<template id='template'>" +
" <div id='template-div0'>" +
" <span>Hello, Web Component!</span>" +
" </div>" +
" <div id='template-div1'></div>" +
" <div id='template-div2'></div>" +
"</template>"
);
var contents = jQuery( "#template" ).contents();
assert.equal( contents.length, 6, "Check template element contents" );
assert.equal( contents.find( "span" ).text(), "Hello, Web Component!", "Find span in template and check its text" );
jQuery( "<div id='templateTest' />" ).append(
jQuery( jQuery.map( contents, function( node ) {
return document.importNode( node, true );
} ) )
).appendTo( "#qunit-fixture" );
contents = jQuery( "#templateTest" ).contents();
assert.equal( contents.length, 6, "Check cloned nodes of template element contents" );
assert.equal( contents.filter( "div" ).length, 3, "Count cloned elements from template" );
} );
QUnit[ "content" in document.createElement( "template" ) ? "test" : "skip" ](
"contents() for <template /> remains inert",
function( assert ) {
assert.expect( 2 );
Globals.register( "testScript" );
Globals.register( "testImgOnload" );
jQuery( "#qunit-fixture" ).append(
"<template id='template'>" +
" <script>testScript = 1;</script>" +
" <img src='data/1x1.jpg' onload='testImgOnload = 1' >" +
"</template>"
);
var content = jQuery( "#template" ).contents();
assert.strictEqual( window.testScript, true, "script in template isn't executed" );
assert.strictEqual( window.testImgOnload, true, "onload of image in template isn't executed" );
}
);
QUnit.test( "sort direction", function( assert ) { QUnit.test( "sort direction", function( assert ) {
assert.expect( 12 ); assert.expect( 12 );