From: Oleg Gaidarenko Date: Tue, 22 Dec 2015 15:16:52 +0000 (+0300) Subject: Revert "Ajax: Always use script injection in globalEval" X-Git-Tag: 1.12.0~37 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c9546c6be5303a7e17cf78b92d904e6575466864;p=jquery.git Revert "Ajax: Always use script injection in globalEval" This reverts commit 37f0f7f42cd158e36665066d88708ca4aa0713ac. --- diff --git a/src/core.js b/src/core.js index 60d5cf221..5f4e4d9dc 100644 --- a/src/core.js +++ b/src/core.js @@ -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 diff --git a/src/effects.js b/src/effects.js index caaa7df64..27c9cf24c 100644 --- a/src/effects.js +++ b/src/effects.js @@ -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 index 000000000..ba47168b4 --- /dev/null +++ b/test/data/event/longLoadScript.php @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/test/data/event/syncReady.html b/test/data/event/syncReady.html index 0d2f8fdad..e0885707e 100644 --- a/test/data/event/syncReady.html +++ b/test/data/event/syncReady.html @@ -8,7 +8,7 @@ @@ -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...--> - +
diff --git a/test/data/longLoadScript.php b/test/data/longLoadScript.php deleted file mode 100644 index ba47168b4..000000000 --- a/test/data/longLoadScript.php +++ /dev/null @@ -1,4 +0,0 @@ - \ No newline at end of file diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 6a9a587c1..e7f38024f 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -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 ) { diff --git a/test/unit/core.js b/test/unit/core.js index ab5c11d34..10143408f 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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 ) { diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 6e805aebe..fb9c454fb 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -2348,45 +2348,25 @@ QUnit.test( "Ensure oldIE creates a new set on appendTo (#8894)", function( asse assert.strictEqual( jQuery( "

" ).appendTo( "

" ).end().length, jQuery( "

test

" ).appendTo( "
" ).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(""); + ok( false, "Exception ignored" ); + }, "Exception bubbled from inline script" ); - jQuery( "#qunit-fixture" ).html( "" ); - assert.ok( true, "Exception ignored" ); - } else { - assert.ok( true, "No jQuery.ajax" ); - assert.ok( true, "No jQuery.ajax" ); - } - }; - - jQuery( "#qunit-fixture" ).html( "" ); -} ); + if ( jQuery.ajax ) { + throws(function() { + jQuery("#qunit-fixture").html(""); + 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 ) {