]> source.dussan.org Git - jquery.git/commitdiff
Fixes #13714. jQuery.globalEval gotcha w/ strings that contain valid, prologue positi...
authorRick Waldron <waldron.rick@gmail.com>
Wed, 3 Apr 2013 15:26:07 +0000 (11:26 -0400)
committerRick Waldron <waldron.rick@gmail.com>
Wed, 3 Apr 2013 15:26:07 +0000 (11:26 -0400)
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
src/core.js
test/unit/core.js

index cae3a90e39f5c7ed8aff871fd0f91857512209a5..e376274596aae09fb42152430873dccb7f1a96bd 100644 (file)
@@ -515,10 +515,25 @@ jQuery.extend({
        noop: function() {},
 
        // Evaluates a script in a global context
-       globalEval: function( data ) {
-               var indirect = eval;
-               if ( jQuery.trim( data ) ) {
-                       indirect( data + ";" );
+       globalEval: function( code ) {
+               var script,
+                               indirect = eval;
+
+               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 );
+                       }
                }
        },
 
index 7c840993b6f33c5f2ef6a9a69bad70dee9535639..6cdd3b93dccb2a88c63d93a4221b62feeb917316 100644 (file)
@@ -215,6 +215,14 @@ test( "globalEval", function() {
        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() {
        expect(7);