Commit feea9394 authored by Rick Waldron's avatar Rick Waldron

Fixes #13714. jQuery.globalEval gotcha w/ strings that contain valid, prologue...

Fixes #13714. jQuery.globalEval gotcha w/ strings that contain valid, prologue position strict mode pragma
Signed-off-by: 's avatarRick Waldron <waldron.rick@gmail.com>
parent 05d55d08
...@@ -515,10 +515,25 @@ jQuery.extend({ ...@@ -515,10 +515,25 @@ jQuery.extend({
noop: function() {}, noop: function() {},
// Evaluates a script in a global context // Evaluates a script in a global context
globalEval: function( data ) { globalEval: function( code ) {
var indirect = eval; var script,
if ( jQuery.trim( data ) ) { indirect = eval;
indirect( data + ";" );
code = jQuery.trim( code ) + ";";
if ( code ) {
// If the code includes a valid, prologue position
// strict mode pragma, execute code by injecting a
// script tag into the document.
if ( code.indexOf("use strict") === 1 ) {
script = document.createElement("script");
script.text = code;
document.head.appendChild( script ).parentNode.removeChild( script );
} else {
// Otherwise, avoid the DOM node creation, insertion
// and removal by using an indirect global eval
indirect( code );
}
} }
}, },
......
...@@ -215,6 +215,14 @@ test( "globalEval", function() { ...@@ -215,6 +215,14 @@ 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 with 'use strict'", function() {
expect( 1 );
Globals.register("strictEvalTest");
jQuery.globalEval("'use strict'; var strictEvalTest = 1;");
equal( window.strictEvalTest, 1, "Test variable declarations are global (strict mode)" );
});
test("noConflict", function() { test("noConflict", function() {
expect(7); expect(7);
......
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