]> source.dussan.org Git - jquery.git/commitdiff
Revert "Ajax: Always use script injection in globalEval"
authorOleg Gaidarenko <markelog@gmail.com>
Tue, 22 Dec 2015 15:16:52 +0000 (18:16 +0300)
committerOleg Gaidarenko <markelog@gmail.com>
Tue, 22 Dec 2015 15:16:52 +0000 (18:16 +0300)
This reverts commit 37f0f7f42cd158e36665066d88708ca4aa0713ac.

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

index 60d5cf221d3452fae58b5791bd592ed26e29118a..5f4e4d9dc526588596c936a6ddfa0eb6d353ef2e 100644 (file)
@@ -286,17 +286,17 @@ 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 ) {
-
-               // 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;
-
-               head.appendChild( script );
-               head.removeChild( script );
+               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 );
+               }
        },
 
        // Convert dashed to camelCase; used by the css and data modules
index caaa7df64f143446c02e2ea6ccfb3064f000fa0d..27c9cf24c317ba8d6ccc0d2e9889b93a09e8040d 100644 (file)
@@ -607,12 +607,12 @@ jQuery.fx.interval = 13;
 
 jQuery.fx.start = function() {
        if ( !timerId ) {
-               timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
+               timerId = window.setInterval( jQuery.fx.tick, jQuery.fx.interval );
        }
 };
 
 jQuery.fx.stop = function() {
-       clearInterval( timerId );
+       window.clearInterval( timerId );
        timerId = null;
 };
 
diff --git a/test/data/event/longLoadScript.php b/test/data/event/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 0d2f8fdadff0952e27aaeb941f043c09e7732823..e0885707edab0acb6ea4853de1dabeda43c5bce3 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
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 6a9a587c15bba599882e6e0d04575e3f4e39c7cb..e7f38024f155497cb3f94807f709a04d6f75e546 100644 (file)
@@ -1417,29 +1417,25 @@ QUnit.module( "ajax", {
                };
        } );
 
-       QUnit.asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function( assert ) {
-
-               // Support: Android 2.3 only
-               // Android 2.3 doesn't fire the window.onerror handler, just accept the reality there.
-               if ( /android 2\.3/i.test( navigator.userAgent ) ) {
-                       assert.ok( true, "Test skipped, Android 2.3 doesn't fire window.onerror for " +
-                               "errors in dynamically included scripts" );
-                       QUnit.start();
-                       return;
-               }
-
-               var onerror = window.onerror;
-               window.onerror = function() {
-                       assert.ok( true, "Exception thrown" );
-                       window.onerror = onerror;
-                       QUnit.start();
-               };
-               jQuery.ajax( {
-                       url: "data/badjson.js",
-                       dataType: "script",
-                       throws: true
-               } );
-       } );
+       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" );
+       });
 
        jQuery.each( [ "method", "type" ], function( _, globalOption ) {
                function request( assert, option ) {
index ab5c11d34cdc9df7b4fd08767b1e85846610439f..10143408f8c0aa81013dee2e8c136407203b7bff 100644 (file)
@@ -209,20 +209,26 @@ QUnit.test( "globalEval", function( assert ) {
        assert.equal( window.globalEvalTest, 3, "Test context (this) is the window object" );
 } );
 
-QUnit.test( "globalEval execution after script injection (#7862)", function( assert ) {
-       assert.expect( 1 );
+if ( jQuery.noConflict ) {
+       QUnit.test( "noConflict", function( assert ) {
+               assert.expect( 7 );
 
-       var now,
-               script = document.createElement( "script" );
+               var $$ = jQuery;
+
+               assert.strictEqual( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
+               assert.strictEqual( window[ "jQuery" ], $$, "Make sure jQuery wasn't touched." );
+               assert.strictEqual( window[ "$" ], original$, "Make sure $ was reverted." );
 
-       script.src = "data/longLoadScript.php?sleep=2";
+               jQuery = $ = $$;
 
-       now = jQuery.now();
-       document.body.appendChild( script );
+               assert.strictEqual( jQuery.noConflict( true ), $$, "noConflict returned the jQuery object" );
+               assert.strictEqual( window[ "jQuery" ], originaljQuery, "Make sure jQuery was reverted." );
+               assert.strictEqual( window[ "$" ], original$, "Make sure $ was reverted." );
+               assert.ok( $$().pushStack( [] ), "Make sure that jQuery still works." );
 
-       jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";" );
-       assert.ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
-} );
+               window[ "jQuery" ] = jQuery = $$;
+       } );
+}
 
 // This is not run in AMD mode
 if ( jQuery.noConflict ) {
index 6e805aebe552770b2e13b9adbd9715f8f109b16c..fb9c454fbe4e75a8184bda5f34f79d6eef9474d7 100644 (file)
@@ -2348,45 +2348,25 @@ QUnit.test( "Ensure oldIE creates a new set on appendTo (#8894)", function( asse
        assert.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" );
 } );
 
-QUnit.asyncTest( "html() - script exceptions bubble (#11743)", 2, function( assert ) {
 
-       // Support: Android 2.3 only
-       // Android 2.3 doesn't fire the window.onerror handler, just accept the reality there.
-       if ( /android 2\.3/i.test( navigator.userAgent ) ) {
-               assert.ok( true, "Test skipped, Android 2.3 doesn't fire window.onerror for " +
-                       "errors in dynamically included scripts" );
-               assert.ok( true, "Test skipped, Android 2.3 doesn't fire window.onerror for " +
-                       "errors in dynamically included scripts" );
-               QUnit.start();
-               return;
-       }
-
-       var onerror = window.onerror;
-
-       setTimeout( function() {
-               window.onerror = onerror;
-
-               QUnit.start();
-       }, 1000 );
+test( "html() - script exceptions bubble (#11743)", function() {
 
-       window.onerror = function() {
-               assert.ok( true, "Exception thrown" );
+       expect( 2 );
 
-               if ( jQuery.ajax ) {
-                       window.onerror = function() {
-                               assert.ok( true, "Exception thrown in remote script" );
-                       };
+       throws(function() {
+               jQuery("#qunit-fixture").html("<script>undefined(); ok( false, 'Exception not thrown' );</script>");
+               ok( false, "Exception ignored" );
+       }, "Exception bubbled from inline script" );
 
-                       jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
-                       assert.ok( true, "Exception ignored" );
-               } else {
-                       assert.ok( true, "No jQuery.ajax" );
-                       assert.ok( true, "No jQuery.ajax" );
-               }
-       };
-
-       jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
-} );
+       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" );
+       }
+});
 
 QUnit.test( "checked state is cloned with clone()", function( assert ) {