Skip to content

Commit c9546c6

Browse files
committed
Revert "Ajax: Always use script injection in globalEval"
This reverts commit 37f0f7f.
1 parent e5256a6 commit c9546c6

File tree

7 files changed

+64
-82
lines changed

7 files changed

+64
-82
lines changed

src/core.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,17 @@ jQuery.extend( {
286286
},
287287

288288
// Evaluates a script in a global context
289+
// Workarounds based on findings by Jim Driscoll
290+
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
289291
globalEval: function( data ) {
290-
291-
// Inspired by code by Andrea Giammarchi
292-
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
293-
var head = document.head || jQuery( "head" )[ 0 ] || document.documentElement,
294-
script = document.createElement( "script" );
295-
296-
script.text = data;
297-
298-
head.appendChild( script );
299-
head.removeChild( script );
292+
if ( data && jQuery.trim( data ) ) {
293+
// We use execScript on Internet Explorer
294+
// We use an anonymous function so that context is window
295+
// rather than jQuery in Firefox
296+
( window.execScript || function( data ) {
297+
window[ "eval" ].call( window, data );
298+
} )( data );
299+
}
300300
},
301301

302302
// Convert dashed to camelCase; used by the css and data modules

src/effects.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,12 @@ jQuery.fx.interval = 13;
607607

608608
jQuery.fx.start = function() {
609609
if ( !timerId ) {
610-
timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
610+
timerId = window.setInterval( jQuery.fx.tick, jQuery.fx.interval );
611611
}
612612
};
613613

614614
jQuery.fx.stop = function() {
615-
clearInterval( timerId );
615+
window.clearInterval( timerId );
616616
timerId = null;
617617
};
618618

test/data/event/syncReady.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99

1010
<script type="text/javascript">
11-
jQuery( document ).ready(function() {
11+
jQuery( document ).ready(function () {
1212
window.parent.iframeCallback( jQuery('#container').length === 1 );
1313
});
1414
</script>
@@ -17,7 +17,7 @@
1717
oldIE into thinking the dom is ready, but it's not...
1818
leaving this check here for future trailblazers to attempt
1919
fixing this...-->
20-
<script type="text/javascript" src="../longLoadScript.php?sleep=1"></script>
20+
<script type="text/javascript" src="longLoadScript.php?sleep=1"></script>
2121
<div id="container" style="height: 300px"></div>
2222
</body>
2323
</html>

test/unit/ajax.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,29 +1417,25 @@ QUnit.module( "ajax", {
14171417
};
14181418
} );
14191419

1420-
QUnit.asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function( assert ) {
1421-
1422-
// Support: Android 2.3 only
1423-
// Android 2.3 doesn't fire the window.onerror handler, just accept the reality there.
1424-
if ( /android 2\.3/i.test( navigator.userAgent ) ) {
1425-
assert.ok( true, "Test skipped, Android 2.3 doesn't fire window.onerror for " +
1426-
"errors in dynamically included scripts" );
1427-
QUnit.start();
1428-
return;
1429-
}
1430-
1431-
var onerror = window.onerror;
1432-
window.onerror = function() {
1433-
assert.ok( true, "Exception thrown" );
1434-
window.onerror = onerror;
1435-
QUnit.start();
1436-
};
1437-
jQuery.ajax( {
1438-
url: "data/badjson.js",
1439-
dataType: "script",
1440-
throws: true
1441-
} );
1442-
} );
1420+
test( "#11743 - jQuery.ajax() - script, throws exception", 1, function() {
1421+
throws(function() {
1422+
jQuery.ajax({
1423+
url: "data/badjson.js",
1424+
dataType: "script",
1425+
"throws": true,
1426+
// TODO find a way to test this asynchronously, too
1427+
async: false,
1428+
// Global events get confused by the exception
1429+
global: false,
1430+
success: function() {
1431+
ok( false, "Success." );
1432+
},
1433+
error: function() {
1434+
ok( false, "Error." );
1435+
}
1436+
});
1437+
}, "exception bubbled" );
1438+
});
14431439

14441440
jQuery.each( [ "method", "type" ], function( _, globalOption ) {
14451441
function request( assert, option ) {

test/unit/core.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,26 @@ QUnit.test( "globalEval", function( assert ) {
209209
assert.equal( window.globalEvalTest, 3, "Test context (this) is the window object" );
210210
} );
211211

212-
QUnit.test( "globalEval execution after script injection (#7862)", function( assert ) {
213-
assert.expect( 1 );
212+
if ( jQuery.noConflict ) {
213+
QUnit.test( "noConflict", function( assert ) {
214+
assert.expect( 7 );
214215

215-
var now,
216-
script = document.createElement( "script" );
216+
var $$ = jQuery;
217+
218+
assert.strictEqual( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
219+
assert.strictEqual( window[ "jQuery" ], $$, "Make sure jQuery wasn't touched." );
220+
assert.strictEqual( window[ "$" ], original$, "Make sure $ was reverted." );
217221

218-
script.src = "data/longLoadScript.php?sleep=2";
222+
jQuery = $ = $$;
219223

220-
now = jQuery.now();
221-
document.body.appendChild( script );
224+
assert.strictEqual( jQuery.noConflict( true ), $$, "noConflict returned the jQuery object" );
225+
assert.strictEqual( window[ "jQuery" ], originaljQuery, "Make sure jQuery was reverted." );
226+
assert.strictEqual( window[ "$" ], original$, "Make sure $ was reverted." );
227+
assert.ok( $$().pushStack( [] ), "Make sure that jQuery still works." );
222228

223-
jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";" );
224-
assert.ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
225-
} );
229+
window[ "jQuery" ] = jQuery = $$;
230+
} );
231+
}
226232

227233
// This is not run in AMD mode
228234
if ( jQuery.noConflict ) {

test/unit/manipulation.js

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,45 +2348,25 @@ QUnit.test( "Ensure oldIE creates a new set on appendTo (#8894)", function( asse
23482348
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" );
23492349
} );
23502350

2351-
QUnit.asyncTest( "html() - script exceptions bubble (#11743)", 2, function( assert ) {
23522351

2353-
// Support: Android 2.3 only
2354-
// Android 2.3 doesn't fire the window.onerror handler, just accept the reality there.
2355-
if ( /android 2\.3/i.test( navigator.userAgent ) ) {
2356-
assert.ok( true, "Test skipped, Android 2.3 doesn't fire window.onerror for " +
2357-
"errors in dynamically included scripts" );
2358-
assert.ok( true, "Test skipped, Android 2.3 doesn't fire window.onerror for " +
2359-
"errors in dynamically included scripts" );
2360-
QUnit.start();
2361-
return;
2362-
}
2363-
2364-
var onerror = window.onerror;
2365-
2366-
setTimeout( function() {
2367-
window.onerror = onerror;
2368-
2369-
QUnit.start();
2370-
}, 1000 );
2352+
test( "html() - script exceptions bubble (#11743)", function() {
23712353

2372-
window.onerror = function() {
2373-
assert.ok( true, "Exception thrown" );
2354+
expect( 2 );
23742355

2375-
if ( jQuery.ajax ) {
2376-
window.onerror = function() {
2377-
assert.ok( true, "Exception thrown in remote script" );
2378-
};
2356+
throws(function() {
2357+
jQuery("#qunit-fixture").html("<script>undefined(); ok( false, 'Exception not thrown' );</script>");
2358+
ok( false, "Exception ignored" );
2359+
}, "Exception bubbled from inline script" );
23792360

2380-
jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
2381-
assert.ok( true, "Exception ignored" );
2382-
} else {
2383-
assert.ok( true, "No jQuery.ajax" );
2384-
assert.ok( true, "No jQuery.ajax" );
2385-
}
2386-
};
2387-
2388-
jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
2389-
} );
2361+
if ( jQuery.ajax ) {
2362+
throws(function() {
2363+
jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
2364+
ok( false, "Exception ignored" );
2365+
}, "Exception thrown in remote script" );
2366+
} else {
2367+
ok( true, "No jQuery.ajax" );
2368+
}
2369+
});
23902370

23912371
QUnit.test( "checked state is cloned with clone()", function( assert ) {
23922372

0 commit comments

Comments
 (0)