Skip to content

Commit

Permalink
Revert "Ajax: Always use script injection in globalEval"
Browse files Browse the repository at this point in the history
This reverts commit 37f0f7f.
  • Loading branch information
markelog committed Dec 22, 2015
1 parent e5256a6 commit c9546c6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 82 deletions.
20 changes: 10 additions & 10 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions test/data/event/syncReady.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>

<script type="text/javascript">
jQuery( document ).ready(function() {
jQuery( document ).ready(function () {
window.parent.iframeCallback( jQuery('#container').length === 1 );
});
</script>
Expand All @@ -17,7 +17,7 @@
oldIE into thinking the dom is ready, but it's not...
leaving this check here for future trailblazers to attempt
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>
</body>
</html>
42 changes: 19 additions & 23 deletions test/unit/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
26 changes: 16 additions & 10 deletions test/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
50 changes: 15 additions & 35 deletions test/unit/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2348,45 +2348,25 @@ QUnit.test( "Ensure oldIE creates a new set on appendTo (#8894)", function( asse
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" );
} );

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("<script>undefined(); ok( false, 'Exception not thrown' );</script>");
ok( false, "Exception ignored" );
}, "Exception bubbled from inline script" );

jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
assert.ok( true, "Exception ignored" );
} else {
assert.ok( true, "No jQuery.ajax" );
assert.ok( true, "No jQuery.ajax" );
}
};

jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
} );
if ( jQuery.ajax ) {
throws(function() {
jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
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 ) {

Expand Down

0 comments on commit c9546c6

Please sign in to comment.