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

Commit 0ba8e38

Browse files
emibloquedmethvin
authored andcommitted
Traversing: $.fn.contents() support for object
Fixes gh-4045 Closes gh-4046
1 parent 821bf34 commit 0ba8e38

File tree

5 files changed

+96
-51
lines changed

5 files changed

+96
-51
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ module.exports = function( grunt ) {
209209
{ pattern: "dist/*.map", included: false, served: true },
210210
{ pattern: "external/qunit/qunit.css", included: false, served: true },
211211
{
212-
pattern: "test/**/*.@(js|css|jpg|html|xml)",
212+
pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
213213
included: false,
214214
served: true
215215
}

src/traversing.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,18 @@ jQuery.each( {
145145
return siblings( elem.firstChild );
146146
},
147147
contents: function( elem ) {
148-
if ( nodeName( elem, "iframe" ) ) {
149-
return elem.contentDocument;
150-
}
151-
152-
// Support: IE 9 - 11 only, iOS 7 only, Android Browser <=4.3 only
153-
// Treat the template element as a regular one in browsers that
154-
// don't support it.
155-
if ( nodeName( elem, "template" ) ) {
156-
elem = elem.content || elem;
157-
}
158-
159-
return jQuery.merge( [], elem.childNodes );
148+
if ( typeof elem.contentDocument !== "undefined" ) {
149+
return elem.contentDocument;
150+
}
151+
152+
// Support: IE 9 - 11 only, iOS 7 only, Android Browser <=4.3 only
153+
// Treat the template element as a regular one in browsers that
154+
// don't support it.
155+
if ( nodeName( elem, "template" ) ) {
156+
elem = elem.content || elem;
157+
}
158+
159+
return jQuery.merge( [], elem.childNodes );
160160
}
161161
}, function( name, fn ) {
162162
jQuery.fn[ name ] = function( until, selector ) {

test/data/1x1.svg

Lines changed: 6 additions & 0 deletions
Loading

test/data/frame.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>frame</title>
4+
</head>
5+
<frameset cols="20%, 80%">
6+
<frame id="test-frame" src="iframe.html">
7+
<frame src="iframe.html">
8+
</frameset>
9+
</html>

test/unit/traversing.js

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -744,56 +744,86 @@ QUnit.test( "contents()", function( assert ) {
744744
} );
745745

746746
QUnit.test( "contents() for <template />", function( assert ) {
747-
assert.expect( 4 );
747+
assert.expect( 4 );
748748

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-
);
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+
);
758758

759-
var contents = jQuery( "#template" ).contents();
760-
assert.equal( contents.length, 6, "Check template element contents" );
759+
var contents = jQuery( "#template" ).contents();
760+
assert.equal( contents.length, 6, "Check template element contents" );
761761

762-
assert.equal( contents.find( "span" ).text(), "Hello, Web Component!", "Find span in template and check its text" );
762+
assert.equal( contents.find( "span" ).text(), "Hello, Web Component!", "Find span in template and check its text" );
763763

764-
jQuery( "<div id='templateTest' />" ).append(
765-
jQuery( jQuery.map( contents, function( node ) {
766-
return document.importNode( node, true );
767-
} ) )
768-
).appendTo( "#qunit-fixture" );
764+
jQuery( "<div id='templateTest' />" ).append(
765+
jQuery( jQuery.map( contents, function( node ) {
766+
return document.importNode( node, true );
767+
} ) )
768+
).appendTo( "#qunit-fixture" );
769769

770-
contents = jQuery( "#templateTest" ).contents();
771-
assert.equal( contents.length, 6, "Check cloned nodes of template element contents" );
770+
contents = jQuery( "#templateTest" ).contents();
771+
assert.equal( contents.length, 6, "Check cloned nodes of template element contents" );
772772

773-
assert.equal( contents.filter( "div" ).length, 3, "Count cloned elements from template" );
773+
assert.equal( contents.filter( "div" ).length, 3, "Count cloned elements from template" );
774774
} );
775775

776-
QUnit[ "content" in document.createElement( "template" ) ? "test" : "skip" ](
777-
"contents() for <template /> remains inert",
778-
function( assert ) {
779-
assert.expect( 2 );
776+
QUnit[ "content" in document.createElement( "template" ) ? "test" : "skip" ]( "contents() for <template /> remains inert", function( assert ) {
777+
assert.expect( 2 );
780778

781-
Globals.register( "testScript" );
782-
Globals.register( "testImgOnload" );
779+
Globals.register( "testScript" );
780+
Globals.register( "testImgOnload" );
783781

784-
jQuery( "#qunit-fixture" ).append(
785-
"<template id='template'>" +
786-
" <script>testScript = 1;</script>" +
787-
" <img src='" + baseURL + "1x1.jpg' onload='testImgOnload = 1' >" +
788-
"</template>"
789-
);
782+
jQuery( "#qunit-fixture" ).append(
783+
"<template id='template'>" +
784+
" <script>testScript = 1;</script>" +
785+
" <img src='" + baseURL + "1x1.jpg' onload='testImgOnload = 1' >" +
786+
"</template>"
787+
);
790788

791-
var content = jQuery( "#template" ).contents();
789+
var content = jQuery( "#template" ).contents();
792790

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-
);
791+
assert.strictEqual( window.testScript, true, "script in template isn't executed" );
792+
assert.strictEqual( window.testImgOnload, true, "onload of image in template isn't executed" );
793+
} );
794+
795+
QUnit.test( "contents() for <object />", function( assert ) {
796+
assert.expect( 2 );
797+
798+
var svgObject = jQuery( "<object id='svg-object' data='" + baseURL + "1x1.svg'></object>" );
799+
var done = assert.async();
800+
801+
svgObject.on( "load", function() {
802+
var contents = jQuery( "#svg-object" ).contents();
803+
assert.equal( contents.length, 1, "Check object contents" );
804+
assert.equal( contents.find( "svg" ).length, 1, "Find svg within object" );
805+
done();
806+
} );
807+
808+
jQuery( "#qunit-fixture" ).append( svgObject );
809+
} );
810+
811+
QUnit.test( "contents() for <frame />", function( assert ) {
812+
assert.expect( 2 );
813+
814+
var iframe = jQuery( "<iframe id='frame-contents' src='" + baseURL + "frame.html'></iframe>" );
815+
var done = assert.async();
816+
817+
iframe.on( "load", function() {
818+
var container = jQuery( "#frame-contents" ).contents();
819+
var contents = container.find( "#test-frame" ).contents();
820+
assert.equal( contents.length, 1, "Check frame contents" );
821+
assert.equal( contents.find( "body" ).length, 1, "Find body within frame" );
822+
done();
823+
} );
824+
825+
jQuery( "#qunit-fixture" ).append( iframe );
826+
} );
797827

798828
QUnit.test( "sort direction", function( assert ) {
799829
assert.expect( 12 );

0 commit comments

Comments
 (0)