Skip to content

Commit 91acd85

Browse files
committed
Revert "Ajax: Always use script injection in globalEval"
This reverts commit bbdfbb4.
1 parent 9d1d4c2 commit 91acd85

File tree

6 files changed

+50
-60
lines changed

6 files changed

+50
-60
lines changed

src/core.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,26 @@ jQuery.extend( {
262262
},
263263

264264
// Evaluates a script in a global context
265-
globalEval: function( code, context ) {
266-
context = context || document;
267-
var script = context.createElement( "script" );
268-
269-
script.text = code;
270-
context.head.appendChild( script ).parentNode.removeChild( script );
265+
globalEval: function( code ) {
266+
var script,
267+
indirect = eval;
268+
269+
code = jQuery.trim( code );
270+
271+
if ( code ) {
272+
// If the code includes a valid, prologue position
273+
// strict mode pragma, execute code by injecting a
274+
// script tag into the document.
275+
if ( code.indexOf("use strict") === 1 ) {
276+
script = document.createElement("script");
277+
script.text = code;
278+
document.head.appendChild( script ).parentNode.removeChild( script );
279+
} else {
280+
// Otherwise, avoid the DOM node creation, insertion
281+
// and removal by using an indirect global eval
282+
indirect( code );
283+
}
284+
}
271285
},
272286

273287
// 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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,9 +1624,17 @@ if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().re
16241624
jQuery.ajax( {
16251625
url: "data/badjson.js",
16261626
dataType: "script",
1627-
throws: true
1628-
} );
1629-
} );
1627+
throws: true,
1628+
// Global events get confused by the exception
1629+
global: false,
1630+
success: function() {
1631+
ok( false, "Success." );
1632+
},
1633+
error: function() {
1634+
ok( false, "Error." );
1635+
}
1636+
});
1637+
});
16301638

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

test/unit/core.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,6 @@ QUnit.test( "globalEval with 'use strict'", function( assert ) {
170170
assert.equal( window.strictEvalTest, 1, "Test variable declarations are global (strict mode)" );
171171
} );
172172

173-
QUnit.test( "globalEval execution after script injection (#7862)", function( assert ) {
174-
assert.expect( 1 );
175-
176-
var now,
177-
script = document.createElement( "script" );
178-
179-
script.src = "data/longLoadScript.php?sleep=2";
180-
181-
now = jQuery.now();
182-
document.body.appendChild( script );
183-
184-
jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";" );
185-
assert.ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
186-
} );
187-
188173
// This is not run in AMD mode
189174
if ( jQuery.noConflict ) {
190175
QUnit.test( "noConflict", function( assert ) {

test/unit/manipulation.js

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,45 +2297,28 @@ QUnit.test( "Ensure oldIE creates a new set on appendTo (#8894)", function( asse
22972297
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" );
22982298
} );
22992299

2300-
QUnit.asyncTest( "html() - script exceptions bubble (#11743)", 2, function( assert ) {
2300+
QUnit.test( "html() - script exceptions bubble (#11743)", function( assert ) {
2301+
assert.expect( 3 );
23012302

2302-
// Support: Android 2.3 only
2303-
// Android 2.3 doesn't fire the window.onerror handler, just accept the reality there.
2304-
if ( /android 2\.3/i.test( navigator.userAgent ) ) {
2305-
assert.ok( true, "Test skipped, Android 2.3 doesn't fire window.onerror for " +
2306-
"errors in dynamically included scripts" );
2307-
assert.ok( true, "Test skipped, Android 2.3 doesn't fire window.onerror for " +
2308-
"errors in dynamically included scripts" );
2309-
QUnit.start();
2310-
return;
2311-
}
2303+
assert.throws(function() {
2304+
jQuery("#qunit-fixture").html("<script>undefined(); ok( false, 'Exception not thrown' );</script>");
2305+
assert.ok( false, "Exception ignored" );
2306+
}, "Exception bubbled from inline script" );
23122307

2313-
var onerror = window.onerror;
2308+
if ( jQuery.ajax ) {
2309+
var onerror = window.onerror;
2310+
window.onerror = function() {
2311+
ok( true, "Exception thrown in remote script" );
2312+
};
23142313

2315-
setTimeout( function() {
2314+
jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
2315+
assert.ok( true, "Exception ignored" );
23162316
window.onerror = onerror;
2317-
2318-
QUnit.start();
2319-
}, 1000 );
2320-
2321-
window.onerror = function() {
2322-
assert.ok( true, "Exception thrown" );
2323-
2324-
if ( jQuery.ajax ) {
2325-
window.onerror = function() {
2326-
assert.ok( true, "Exception thrown in remote script" );
2327-
};
2328-
2329-
jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
2330-
assert.ok( true, "Exception ignored" );
2331-
} else {
2332-
assert.ok( true, "No jQuery.ajax" );
2333-
assert.ok( true, "No jQuery.ajax" );
2334-
}
2335-
};
2336-
2337-
jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
2338-
} );
2317+
} else {
2318+
assert.ok( true, "No jQuery.ajax" );
2319+
assert.ok( true, "No jQuery.ajax" );
2320+
}
2321+
});
23392322

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

0 commit comments

Comments
 (0)