]> source.dussan.org Git - jquery.git/commitdiff
Ajax: Always use script injection in globalEval
authorOleg Gaidarenko <markelog@gmail.com>
Sun, 15 Jun 2014 21:45:24 +0000 (01:45 +0400)
committerOleg Gaidarenko <markelog@gmail.com>
Sun, 15 Jun 2014 23:21:39 +0000 (03:21 +0400)
Fixes #14757
Ref bbdfbb4ee859fe9319b348d88120ddc2c9efbd63

src/core.js
test/data/event/longLoadScript.php [deleted file]
test/data/event/syncReady.html
test/data/longLoadScript.php [new file with mode: 0644]
test/unit/ajax.js
test/unit/core.js
test/unit/manipulation.js

index 028d0ac02568fac1c15eb3d9eba3e6531ac32309..9dacc48a845542ef28adc583c196a86ccddc7d3e 100644 (file)
@@ -275,17 +275,18 @@ jQuery.extend({
        },
 
        // Evaluates a script in a global context
-       // Workarounds based on findings by Jim Driscoll
-       // http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
        globalEval: function( data ) {
-               if ( data && jQuery.trim( data ) ) {
-                       // We use execScript on Internet Explorer
-                       // We use an anonymous function so that context is window
-                       // rather than jQuery in Firefox
-                       ( window.execScript || function( data ) {
-                               window[ "eval" ].call( window, data );
-                       } )( data );
-               }
+               // Inspired by code by Andrea Giammarchi
+               // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
+               var head = document.head || jQuery( "head" )[ 0 ] || document.documentElement,
+                       script = document.createElement( "script" );
+
+               script.text = data;
+
+               // Support: IE6
+               // Circumvent bugs with base elements (#2709 and #4378) by prepending
+               head.insertBefore( script, head.firstChild );
+               head.removeChild( script );
        },
 
        // Convert dashed to camelCase; used by the css and data modules
diff --git a/test/data/event/longLoadScript.php b/test/data/event/longLoadScript.php
deleted file mode 100644 (file)
index ba47168..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-<?php
-sleep((int)$_GET['sleep']);
-header('Content-type: text/javascript');
-?>
\ No newline at end of file
index e0885707edab0acb6ea4853de1dabeda43c5bce3..0d2f8fdadff0952e27aaeb941f043c09e7732823 100644 (file)
@@ -8,7 +8,7 @@
 <body>
 
 <script type="text/javascript">
-jQuery( document ).ready(function () {
+jQuery( document ).ready(function() {
        window.parent.iframeCallback( jQuery('#container').length === 1 );
 });
 </script>
@@ -17,7 +17,7 @@ jQuery( document ).ready(function () {
        oldIE into thinking the dom is ready, but it's not...
        leaving this check here for future trailblazers to attempt
        fixing this...-->
-<script type="text/javascript" src="longLoadScript.php?sleep=1"></script>
+<script type="text/javascript" src="../longLoadScript.php?sleep=1"></script>
 <div id="container" style="height: 300px"></div>
 </body>
 </html>
diff --git a/test/data/longLoadScript.php b/test/data/longLoadScript.php
new file mode 100644 (file)
index 0000000..ba47168
--- /dev/null
@@ -0,0 +1,4 @@
+<?php
+sleep((int)$_GET['sleep']);
+header('Content-type: text/javascript');
+?>
\ No newline at end of file
index 97372c5a07c5aaf467e6dfb96ecc0d6cbd6ca5de..d2059bd36d214c2aa68a6a6b28f0fc7206480f38 100644 (file)
@@ -1468,24 +1468,18 @@ module( "ajax", {
                }
        });
 
-       test( "#11743 - jQuery.ajax() - script, throws exception", 1, function() {
-               throws(function() {
-                       jQuery.ajax({
-                               url: "data/badjson.js",
-                               dataType: "script",
-                               "throws": true,
-                               // TODO find a way to test this asynchronously, too
-                               async: false,
-                               // Global events get confused by the exception
-                               global: false,
-                               success: function() {
-                                       ok( false, "Success." );
-                               },
-                               error: function() {
-                                       ok( false, "Error." );
-                               }
-                       });
-               }, "exception bubbled" );
+       asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function() {
+               var onerror = window.onerror;
+               window.onerror = function() {
+                       ok( true, "Exception thrown" );
+                       window.onerror = onerror;
+                       start();
+               };
+               jQuery.ajax({
+                       url: "data/badjson.js",
+                       dataType: "script",
+                       "throws": true
+               });
        });
 
        jQuery.each( [ "method", "type" ], function( _, globalOption ) {
index ef49999e64c71d763381e7573d9a9528140baddd..d59c4b0fa51eb267ae71ebe49b33c44112977004 100644 (file)
@@ -214,6 +214,19 @@ test( "globalEval", function() {
        equal( window.globalEvalTest, 3, "Test context (this) is the window object" );
 });
 
+test( "globalEval execution after script injection (#7862)", 1, function() {
+       var now,
+               script = document.createElement( "script" );
+
+       script.src = "data/longLoadScript.php?sleep=2";
+
+       now = jQuery.now();
+       document.body.appendChild( script );
+
+       jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";");
+       ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
+});
+
 test("noConflict", function() {
        expect(7);
 
index ad90df92318a8311b9cdf8917ad6f220ee6efb7c..e2bfd75b1a6d66dff7dd6b380bbd61716df24936 100644 (file)
@@ -2263,23 +2263,33 @@ test( "Ensure oldIE creates a new set on appendTo (#8894)", function() {
        strictEqual( jQuery("<p/>").appendTo("<div/>").end().length, jQuery("<p>test</p>").appendTo("<div/>").end().length, "Elements created with createElement and with createDocumentFragment should be treated alike" );
 });
 
-test( "html() - script exceptions bubble (#11743)", function() {
+asyncTest( "html() - script exceptions bubble (#11743)", 2, function() {
 
-       expect( 2 );
+       var onerror = window.onerror;
 
-       throws(function() {
-               jQuery("#qunit-fixture").html("<script>undefined(); ok( false, 'Exception not thrown' );</script>");
-               ok( false, "Exception ignored" );
-       }, "Exception bubbled from inline script" );
+       setTimeout(function() {
+               window.onerror = onerror;
 
-       if ( jQuery.ajax ) {
-               throws(function() {
-                       jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
-                       ok( false, "Exception ignored" );
-               }, "Exception thrown in remote script" );
-       } else {
-               ok( true, "No jQuery.ajax" );
-       }
+               start();
+       }, 1000 );
+
+       window.onerror = function() {
+               ok( true, "Exception thrown" );
+
+               if ( jQuery.ajax ) {
+                       window.onerror = function() {
+                               ok( true, "Exception thrown in remote script" );
+                       };
+
+                       jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
+                       ok( true, "Exception ignored" );
+               } else {
+                       ok( true, "No jQuery.ajax" );
+                       ok( true, "No jQuery.ajax" );
+               }
+       };
+
+       jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
 });
 
 test( "checked state is cloned with clone()", function() {