]> source.dussan.org Git - jquery.git/commitdiff
Manipulation: execute scripts from iframe in the iframe's context
authorTimmy Willison <timmywillisn@gmail.com>
Fri, 6 Nov 2015 23:36:38 +0000 (18:36 -0500)
committerTimmy Willison <timmywillisn@gmail.com>
Mon, 9 Nov 2015 19:14:30 +0000 (14:14 -0500)
Fixes gh-1757
Close gh-2696

src/core.js
src/manipulation.js
test/data/manipulation/scripts-context.html [new file with mode: 0644]
test/unit/manipulation.js

index 8e43547ac073490a69096e6d097218d172bdc9e7..9095213eb3b7b08722df28ef4f5da5feba74dabf 100644 (file)
@@ -258,11 +258,12 @@ jQuery.extend( {
        },
 
        // Evaluates a script in a global context
-       globalEval: function( code ) {
-               var script = document.createElement( "script" );
+       globalEval: function( code, context ) {
+               context = context || document;
+               var script = context.createElement( "script" );
 
                script.text = code;
-               document.head.appendChild( script ).parentNode.removeChild( script );
+               context.head.appendChild( script ).parentNode.removeChild( script );
        },
 
        // Convert dashed to camelCase; used by the css and data modules
index ceb970cbd5957d38053f9c1b16d4935a000354d9..eaf2e099832e2437641ea522ec24316c8f979d3d 100644 (file)
@@ -192,7 +192,7 @@ function domManip( collection, args, callback, ignored ) {
                                                                jQuery._evalUrl( node.src );
                                                        }
                                                } else {
-                                                       jQuery.globalEval( node.textContent.replace( rcleanScript, "" ) );
+                                                       jQuery.globalEval( node.textContent.replace( rcleanScript, "" ), doc );
                                                }
                                        }
                                }
diff --git a/test/data/manipulation/scripts-context.html b/test/data/manipulation/scripts-context.html
new file mode 100644 (file)
index 0000000..6958453
--- /dev/null
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html>
+       <head>
+               <meta charset=utf-8 />
+               <title>body</title>
+       </head>
+       <body>
+               <div id="qunit-fixture"></div>
+               <script src="../../jquery.js"></script>
+               <script>
+                       window.parent.iframeCallback(
+                               window,
+                               document.body,
+                               "<script>window.scriptTest = true;<\x2fscript>"
+                       );
+               </script>
+       </body>
+</html>
index 37da1206268be4a9d1bc05d037cf6357d2c2f210..a56956fa11f79a8fe588cfd90eaff683ce060431 100644 (file)
@@ -2162,6 +2162,17 @@ testIframeWithCallback(
        }
 );
 
+testIframeWithCallback(
+       "domManip executes scripts in iframes in the iframes' context",
+       "manipulation/scripts-context.html",
+       function( frameWindow, bodyElement, html, assert ) {
+               assert.expect( 2 );
+               jQuery( bodyElement ).append( html );
+               assert.ok( !window.scriptTest, "script executed in iframe context" );
+               assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
+       }
+);
+
 QUnit.test( "jQuery.clone - no exceptions for object elements #9587", function( assert ) {
 
        assert.expect( 1 );