Skip to content

Commit bbdfbb4

Browse files
committed
Ajax: Always use script injection in globalEval
Fixes #14757 Closes jquerygh-1449
1 parent e488d98 commit bbdfbb4

File tree

6 files changed

+41
-47
lines changed

6 files changed

+41
-47
lines changed

src/core.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,25 +256,10 @@ jQuery.extend({
256256

257257
// Evaluates a script in a global context
258258
globalEval: function( code ) {
259-
var script,
260-
indirect = eval;
261-
262-
code = jQuery.trim( code );
263-
264-
if ( code ) {
265-
// If the code includes a valid, prologue position
266-
// strict mode pragma, execute code by injecting a
267-
// script tag into the document.
268-
if ( code.indexOf("use strict") === 1 ) {
269-
script = document.createElement("script");
270-
script.text = code;
271-
document.head.appendChild( script ).parentNode.removeChild( script );
272-
} else {
273-
// Otherwise, avoid the DOM node creation, insertion
274-
// and removal by using an indirect global eval
275-
indirect( code );
276-
}
277-
}
259+
var script = document.createElement( "script" );
260+
261+
script.text = code;
262+
document.head.appendChild( script ).parentNode.removeChild( script );
278263
},
279264

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

test/data/event/syncReady.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,15 +1433,7 @@ module( "ajax", {
14331433
jQuery.ajax({
14341434
url: "data/badjson.js",
14351435
dataType: "script",
1436-
throws: true,
1437-
// Global events get confused by the exception
1438-
global: false,
1439-
success: function() {
1440-
ok( false, "Success." );
1441-
},
1442-
error: function() {
1443-
ok( false, "Error." );
1444-
}
1436+
throws: true
14451437
});
14461438
});
14471439

test/unit/core.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ test( "globalEval with 'use strict'", function() {
222222
equal( window.strictEvalTest, 1, "Test variable declarations are global (strict mode)" );
223223
});
224224

225+
test( "globalEval execution after script injection (#7862)", 1, function() {
226+
var now,
227+
script = document.createElement( "script" );
228+
229+
script.src = "data/longLoadScript.php?sleep=2";
230+
231+
now = jQuery.now();
232+
document.body.appendChild( script );
233+
234+
jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";");
235+
ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
236+
});
237+
225238
test("noConflict", function() {
226239
expect(7);
227240

test/unit/manipulation.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,28 +2194,32 @@ test( "Ensure oldIE creates a new set on appendTo (#8894)", function() {
21942194
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" );
21952195
});
21962196

2197-
test( "html() - script exceptions bubble (#11743)", function() {
2197+
asyncTest( "html() - script exceptions bubble (#11743)", 2, function() {
2198+
var onerror = window.onerror;
21982199

2199-
expect( 3 );
2200+
setTimeout(function() {
2201+
window.onerror = onerror;
22002202

2201-
throws(function() {
2202-
jQuery("#qunit-fixture").html("<script>undefined(); ok( false, 'Exception not thrown' );</script>");
2203-
ok( false, "Exception ignored" );
2204-
}, "Exception bubbled from inline script" );
2203+
start();
2204+
}, 1000 );
22052205

2206-
if ( jQuery.ajax ) {
2207-
var onerror = window.onerror;
2208-
window.onerror = function() {
2209-
ok( true, "Exception thrown in remote script" );
2210-
};
2206+
window.onerror = function() {
2207+
ok( true, "Exception thrown" );
22112208

2212-
jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
2213-
ok( true, "Exception ignored" );
2214-
window.onerror = onerror;
2215-
} else {
2216-
ok( true, "No jQuery.ajax" );
2217-
ok( true, "No jQuery.ajax" );
2218-
}
2209+
if ( jQuery.ajax ) {
2210+
window.onerror = function() {
2211+
ok( true, "Exception thrown in remote script" );
2212+
};
2213+
2214+
jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
2215+
ok( true, "Exception ignored" );
2216+
} else {
2217+
ok( true, "No jQuery.ajax" );
2218+
ok( true, "No jQuery.ajax" );
2219+
}
2220+
};
2221+
2222+
jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
22192223
});
22202224

22212225
test( "checked state is cloned with clone()", function() {

0 commit comments

Comments
 (0)