Permalink
Browse files

Traversing: $.fn.contents() supports HTMLTemplateElement

Fixes gh-3436
Closes gh-3462
  • Loading branch information...
TechQuery authored and markelog committed Jan 29, 2017
1 parent 5f35b5b commit 3e3b09d68d317a3fe567e377e9032319ac5e0829
Showing with 64 additions and 1 deletion.
  1. +12 −1 src/traversing.js
  2. +52 −0 test/unit/traversing.js
View
@@ -143,7 +143,18 @@ jQuery.each( {
return siblings( elem.firstChild );
},
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 ) {
jQuery.fn[ name ] = function( until, selector ) {
View
@@ -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" );
} );
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 ) {
assert.expect( 12 );

0 comments on commit 3e3b09d

Please sign in to comment.