]> source.dussan.org Git - jquery.git/commitdiff
Traversing: $.fn.contents() support for object
authorLuis Emilio Velasco Sanchez <emibloque@gmail.com>
Mon, 14 May 2018 17:36:30 +0000 (13:36 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Mon, 14 May 2018 17:41:42 +0000 (13:41 -0400)
Fixes gh-4045
Closes gh-4046

Gruntfile.js
src/traversing.js
test/data/1x1.svg [new file with mode: 0644]
test/data/frame.html [new file with mode: 0644]
test/unit/traversing.js

index fbf9b56717a49ebcf940f54c544899752ba644b7..ed2bd775371f66045bb6b00f44725d2b9318e5dc 100644 (file)
@@ -209,7 +209,7 @@ module.exports = function( grunt ) {
                                        { pattern: "dist/*.map", included: false, served: true },
                                        { pattern: "external/qunit/qunit.css", included: false, served: true },
                                        {
-                                               pattern: "test/**/*.@(js|css|jpg|html|xml)",
+                                               pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
                                                included: false,
                                                served: true
                                        }
index 64c7252b9fc8d253d956d5396567529313ea2b56..426d5b6ea31696733f771250087951c5e6b6fb05 100644 (file)
@@ -145,18 +145,18 @@ jQuery.each( {
                return siblings( elem.firstChild );
        },
        contents: function( elem ) {
-        if ( 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 ( nodeName( elem, "template" ) ) {
-            elem = elem.content || elem;
-        }
-
-        return jQuery.merge( [], elem.childNodes );
+               if ( typeof elem.contentDocument !== "undefined" ) {
+                       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 ( nodeName( elem, "template" ) ) {
+                       elem = elem.content || elem;
+               }
+
+               return jQuery.merge( [], elem.childNodes );
        }
 }, function( name, fn ) {
        jQuery.fn[ name ] = function( until, selector ) {
diff --git a/test/data/1x1.svg b/test/data/1x1.svg
new file mode 100644 (file)
index 0000000..70b3e74
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version="1.0" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
+       "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
+<svg width="1px" height="1px"
+       xmlns="http://www.w3.org/2000/svg">
+</svg>
diff --git a/test/data/frame.html b/test/data/frame.html
new file mode 100644 (file)
index 0000000..98107be
--- /dev/null
@@ -0,0 +1,9 @@
+<html>
+       <head>
+               <title>frame</title>
+       </head>
+       <frameset cols="20%, 80%">
+               <frame id="test-frame" src="iframe.html">
+               <frame src="iframe.html">
+       </frameset>
+</html>
index dd0554137860da2474323effe2cd32756e4d5eb5..9ba66959e66d6de7b8c5911bda4ec91e5f807d2a 100644 (file)
@@ -744,56 +744,86 @@ QUnit.test( "contents()", function( assert ) {
 } );
 
 QUnit.test( "contents() for <template />", function( assert ) {
-    assert.expect( 4 );
+       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>"
-    );
+       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" );
+       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" );
+       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" );
+       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" );
+       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" );
+       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 );
+QUnit[ "content" in document.createElement( "template" ) ? "test" : "skip" ]( "contents() for <template /> remains inert", function( assert ) {
+       assert.expect( 2 );
 
-               Globals.register( "testScript" );
-               Globals.register( "testImgOnload" );
+       Globals.register( "testScript" );
+       Globals.register( "testImgOnload" );
 
-        jQuery( "#qunit-fixture" ).append(
-            "<template id='template'>" +
-            "    <script>testScript = 1;</script>" +
-            "    <img src='" + baseURL + "1x1.jpg' onload='testImgOnload = 1' >" +
-            "</template>"
-        );
+       jQuery( "#qunit-fixture" ).append(
+               "<template id='template'>" +
+               "    <script>testScript = 1;</script>" +
+               "    <img src='" + baseURL + "1x1.jpg' onload='testImgOnload = 1' >" +
+               "</template>"
+       );
 
-        var content = jQuery( "#template" ).contents();
+       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" );
-       }
-);
+       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( "contents() for <object />", function( assert ) {
+       assert.expect( 2 );
+
+       var svgObject = jQuery( "<object id='svg-object' data='" + baseURL + "1x1.svg'></object>" );
+       var done = assert.async();
+
+       svgObject.on( "load", function() {
+               var contents = jQuery( "#svg-object" ).contents();
+               assert.equal( contents.length, 1, "Check object contents" );
+               assert.equal( contents.find( "svg" ).length, 1, "Find svg within object" );
+               done();
+       } );
+
+       jQuery( "#qunit-fixture" ).append( svgObject );
+} );
+
+QUnit.test( "contents() for <frame />", function( assert ) {
+       assert.expect( 2 );
+
+       var iframe = jQuery( "<iframe id='frame-contents' src='" + baseURL + "frame.html'></iframe>" );
+       var done = assert.async();
+
+       iframe.on( "load", function() {
+               var container = jQuery( "#frame-contents" ).contents();
+               var contents = container.find( "#test-frame" ).contents();
+               assert.equal( contents.length, 1, "Check frame contents" );
+               assert.equal( contents.find( "body" ).length, 1, "Find body within frame" );
+               done();
+       } );
+
+       jQuery( "#qunit-fixture" ).append( iframe );
+} );
 
 QUnit.test( "sort direction", function( assert ) {
        assert.expect( 12 );