🌐 AI搜索 & 代理 主页
Skip to content

Commit 3e3b09d

Browse files
TechQuerymarkelog
authored andcommitted
Traversing: $.fn.contents() supports HTMLTemplateElement
Fixes gh-3436 Closes gh-3462
1 parent 5f35b5b commit 3e3b09d

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/traversing.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,18 @@ jQuery.each( {
143143
return siblings( elem.firstChild );
144144
},
145145
contents: function( elem ) {
146-
return elem.contentDocument || jQuery.merge( [], elem.childNodes );
146+
if ( jQuery.nodeName( elem, "iframe" ) ) {
147+
return elem.contentDocument;
148+
}
149+
150+
// Support: IE 9 - 11 only, iOS 7 only, Android Browser <=4.3 only
151+
// Treat the template element as a regular one in browsers that
152+
// don't support it.
153+
if ( jQuery.nodeName( elem, "template" ) ) {
154+
elem = elem.content || elem;
155+
}
156+
157+
return jQuery.merge( [], elem.childNodes );
147158
}
148159
}, function( name, fn ) {
149160
jQuery.fn[ name ] = function( until, selector ) {

test/unit/traversing.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,58 @@ QUnit.test( "contents()", function( assert ) {
743743
assert.equal( c[ 0 ].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );
744744
} );
745745

746+
QUnit.test( "contents() for <template />", function( assert ) {
747+
assert.expect( 4 );
748+
749+
jQuery( "#qunit-fixture" ).append(
750+
"<template id='template'>" +
751+
" <div id='template-div0'>" +
752+
" <span>Hello, Web Component!</span>" +
753+
" </div>" +
754+
" <div id='template-div1'></div>" +
755+
" <div id='template-div2'></div>" +
756+
"</template>"
757+
);
758+
759+
var contents = jQuery( "#template" ).contents();
760+
assert.equal( contents.length, 6, "Check template element contents" );
761+
762+
assert.equal( contents.find( "span" ).text(), "Hello, Web Component!", "Find span in template and check its text" );
763+
764+
jQuery( "<div id='templateTest' />" ).append(
765+
jQuery( jQuery.map( contents, function( node ) {
766+
return document.importNode( node, true );
767+
} ) )
768+
).appendTo( "#qunit-fixture" );
769+
770+
contents = jQuery( "#templateTest" ).contents();
771+
assert.equal( contents.length, 6, "Check cloned nodes of template element contents" );
772+
773+
assert.equal( contents.filter( "div" ).length, 3, "Count cloned elements from template" );
774+
} );
775+
776+
QUnit[ "content" in document.createElement( "template" ) ? "test" : "skip" ](
777+
"contents() for <template /> remains inert",
778+
function( assert ) {
779+
assert.expect( 2 );
780+
781+
Globals.register( "testScript" );
782+
Globals.register( "testImgOnload" );
783+
784+
jQuery( "#qunit-fixture" ).append(
785+
"<template id='template'>" +
786+
" <script>testScript = 1;</script>" +
787+
" <img src='data/1x1.jpg' onload='testImgOnload = 1' >" +
788+
"</template>"
789+
);
790+
791+
var content = jQuery( "#template" ).contents();
792+
793+
assert.strictEqual( window.testScript, true, "script in template isn't executed" );
794+
assert.strictEqual( window.testImgOnload, true, "onload of image in template isn't executed" );
795+
}
796+
);
797+
746798
QUnit.test( "sort direction", function( assert ) {
747799
assert.expect( 12 );
748800

0 commit comments

Comments
 (0)