Commit 37f0f7f4 authored by Oleg Gaidarenko's avatar Oleg Gaidarenko

Ajax: Always use script injection in globalEval

Fixes #14757
Ref bbdfbb4ee859fe9319b348d88120ddc2c9efbd63
parent 76294e1e
...@@ -275,17 +275,18 @@ jQuery.extend({ ...@@ -275,17 +275,18 @@ jQuery.extend({
}, },
// Evaluates a script in a global context // 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 ) { globalEval: function( data ) {
if ( data && jQuery.trim( data ) ) { // Inspired by code by Andrea Giammarchi
// We use execScript on Internet Explorer // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
// We use an anonymous function so that context is window var head = document.head || jQuery( "head" )[ 0 ] || document.documentElement,
// rather than jQuery in Firefox script = document.createElement( "script" );
( window.execScript || function( data ) {
window[ "eval" ].call( window, data ); script.text = data;
} )( data );
} // Support: IE6
// Circumvent bugs with base elements (#2709 and #4378) by prepending
head.insertBefore( script, head.firstChild );
head.removeChild( script );
}, },
// Convert dashed to camelCase; used by the css and data modules // Convert dashed to camelCase; used by the css and data modules
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<body> <body>
<script type="text/javascript"> <script type="text/javascript">
jQuery( document ).ready(function () { jQuery( document ).ready(function() {
window.parent.iframeCallback( jQuery('#container').length === 1 ); window.parent.iframeCallback( jQuery('#container').length === 1 );
}); });
</script> </script>
...@@ -17,7 +17,7 @@ jQuery( document ).ready(function () { ...@@ -17,7 +17,7 @@ jQuery( document ).ready(function () {
oldIE into thinking the dom is ready, but it's not... oldIE into thinking the dom is ready, but it's not...
leaving this check here for future trailblazers to attempt leaving this check here for future trailblazers to attempt
fixing this...--> 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> <div id="container" style="height: 300px"></div>
</body> </body>
</html> </html>
...@@ -1468,24 +1468,18 @@ module( "ajax", { ...@@ -1468,24 +1468,18 @@ module( "ajax", {
} }
}); });
test( "#11743 - jQuery.ajax() - script, throws exception", 1, function() { asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function() {
throws(function() { var onerror = window.onerror;
jQuery.ajax({ window.onerror = function() {
url: "data/badjson.js", ok( true, "Exception thrown" );
dataType: "script", window.onerror = onerror;
"throws": true, start();
// TODO find a way to test this asynchronously, too };
async: false, jQuery.ajax({
// Global events get confused by the exception url: "data/badjson.js",
global: false, dataType: "script",
success: function() { "throws": true
ok( false, "Success." ); });
},
error: function() {
ok( false, "Error." );
}
});
}, "exception bubbled" );
}); });
jQuery.each( [ "method", "type" ], function( _, globalOption ) { jQuery.each( [ "method", "type" ], function( _, globalOption ) {
......
...@@ -214,6 +214,19 @@ test( "globalEval", function() { ...@@ -214,6 +214,19 @@ test( "globalEval", function() {
equal( window.globalEvalTest, 3, "Test context (this) is the window object" ); equal( window.globalEvalTest, 3, "Test context (this) is the window object" );
}); });
test( "globalEval execution after script injection (#7862)", 1, function() {
var now,
script = document.createElement( "script" );
script.src = "data/longLoadScript.php?sleep=2";
now = jQuery.now();
document.body.appendChild( script );
jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";");
ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
});
test("noConflict", function() { test("noConflict", function() {
expect(7); expect(7);
......
...@@ -2263,23 +2263,33 @@ test( "Ensure oldIE creates a new set on appendTo (#8894)", function() { ...@@ -2263,23 +2263,33 @@ test( "Ensure oldIE creates a new set on appendTo (#8894)", function() {
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" ); 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" );
}); });
test( "html() - script exceptions bubble (#11743)", function() { asyncTest( "html() - script exceptions bubble (#11743)", 2, function() {
expect( 2 ); var onerror = window.onerror;
throws(function() { setTimeout(function() {
jQuery("#qunit-fixture").html("<script>undefined(); ok( false, 'Exception not thrown' );</script>"); window.onerror = onerror;
ok( false, "Exception ignored" );
}, "Exception bubbled from inline script" );
if ( jQuery.ajax ) { start();
throws(function() { }, 1000 );
jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
ok( false, "Exception ignored" ); window.onerror = function() {
}, "Exception thrown in remote script" ); ok( true, "Exception thrown" );
} else {
ok( true, "No jQuery.ajax" ); if ( jQuery.ajax ) {
} window.onerror = function() {
ok( true, "Exception thrown in remote script" );
};
jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
ok( true, "Exception ignored" );
} else {
ok( true, "No jQuery.ajax" );
ok( true, "No jQuery.ajax" );
}
};
jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
}); });
test( "checked state is cloned with clone()", function() { test( "checked state is cloned with clone()", function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment