Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,10 @@ jQuery.extend( {
return true;
},

// Evaluates a script in a global context
globalEval: function( code, options ) {
DOMEval( code, { nonce: options && options.nonce } );
// Evaluates a script in a provided context; falls back to the global one
// if not specified.
globalEval: function( code, options, doc ) {
DOMEval( code, { nonce: options && options.nonce }, doc );
},

each: function( obj, callback ) {
Expand Down
2 changes: 1 addition & 1 deletion src/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function domManip( collection, args, callback, ignored ) {
if ( jQuery._evalUrl && !node.noModule ) {
jQuery._evalUrl( node.src, {
nonce: node.nonce || node.getAttribute( "nonce" )
} );
}, doc );
}
} else {
DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );
Expand Down
4 changes: 2 additions & 2 deletions src/manipulation/_evalUrl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jQuery from "../ajax.js";

jQuery._evalUrl = function( url, options ) {
jQuery._evalUrl = function( url, options, doc ) {
return jQuery.ajax( {
url: url,

Expand All @@ -18,7 +18,7 @@ jQuery._evalUrl = function( url, options ) {
"text script": function() {}
},
dataFilter: function( response ) {
jQuery.globalEval( response, options );
jQuery.globalEval( response, options, doc );
}
} );
};
Expand Down
15 changes: 15 additions & 0 deletions test/data/core/globaleval-context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>body</title>
</head>
<body>
<div id="qunit-fixture"></div>
<script src="../../jquery.js"></script>
<script src="../iframeTest.js"></script>
<script>
startIframeTest();
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions test/data/manipulation/set-global-scripttest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
window.scriptTest = true;
parent.finishTest();
20 changes: 16 additions & 4 deletions test/data/testinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,24 @@ this.testIframe = function( title, fileName, func, wrapper ) {
args.unshift( assert );

setTimeout( function() {
var result;

this.iframeCallback = undefined;

func.apply( this, args );
func = function() {};
$iframe.remove();
done();
result = func.apply( this, args );

function finish() {
func = function() {};
$iframe.remove();
done();
}

// Wait for promises returned by `func`.
if ( result && result.then ) {
result.then( finish );
} else {
finish();
}
} );
};

Expand Down
13 changes: 13 additions & 0 deletions test/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ QUnit.test( "globalEval execution after script injection (#7862)", function( ass
assert.ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
} );

testIframe(
"globalEval with custom document context",
"core/globaleval-context.html",
function( assert, framejQuery, frameWindow, frameDocument ) {
assert.expect( 2 );

jQuery.globalEval( "window.scriptTest = true;", {}, frameDocument );
assert.ok( !window.scriptTest, "script executed in iframe context" );
assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
}
);


QUnit.test( "noConflict", function( assert ) {
assert.expect( 7 );

Expand Down
21 changes: 21 additions & 0 deletions test/unit/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,27 @@ testIframe(
}
);

testIframe(
"domManip executes external scripts in iframes in the iframes' context",
"manipulation/scripts-context.html",
function( assert, framejQuery, frameWindow, frameDocument ) {
assert.expect( 2 );

Globals.register( "finishTest" );

return new Promise( function( resolve ) {
window.finishTest = resolve;
jQuery( frameDocument.body ).append(
"<script src='" + url( "manipulation/set-global-scripttest.js" ) + "'></script>" );
assert.ok( !window.scriptTest, "script executed in iframe context" );
assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
} );
},

// The AJAX module is needed for jQuery._evalUrl.
QUnit[ jQuery.ajax ? "test" : "skip" ]
);

QUnit.test( "jQuery.clone - no exceptions for object elements #9587", function( assert ) {

assert.expect( 1 );
Expand Down