Skip to content

Commit 3dedc3f

Browse files
committed
Core: Fire iframe script in its context, add doc param in globalEval
1. Support passing custom document to jQuery.globalEval; the script will be invoked in the context of this document. 2. Fire external scripts appended to iframe contents in that iframe context; this was already supported & tested for inline scripts but not for external ones. Fixes gh-4518 Closes gh-4601 (cherry picked from commit 4592595)
1 parent d525ae3 commit 3dedc3f

File tree

8 files changed

+74
-10
lines changed

8 files changed

+74
-10
lines changed

src/core.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,10 @@ jQuery.extend( {
245245
return true;
246246
},
247247

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

253254
each: function( obj, callback ) {

src/manipulation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function domManip( collection, args, callback, ignored ) {
201201
if ( jQuery._evalUrl && !node.noModule ) {
202202
jQuery._evalUrl( node.src, {
203203
nonce: node.nonce || node.getAttribute( "nonce" )
204-
} );
204+
}, doc );
205205
}
206206
} else {
207207
DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );

src/manipulation/_evalUrl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ define( [
44

55
"use strict";
66

7-
jQuery._evalUrl = function( url, options ) {
7+
jQuery._evalUrl = function( url, options, doc ) {
88
return jQuery.ajax( {
99
url: url,
1010

@@ -22,7 +22,7 @@ jQuery._evalUrl = function( url, options ) {
2222
"text script": function() {}
2323
},
2424
dataFilter: function( response ) {
25-
jQuery.globalEval( response, options );
25+
jQuery.globalEval( response, options, doc );
2626
}
2727
} );
2828
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset=utf-8 />
5+
<title>body</title>
6+
</head>
7+
<body>
8+
<div id="qunit-fixture"></div>
9+
<script src="../../jquery.js"></script>
10+
<script src="../iframeTest.js"></script>
11+
<script>
12+
startIframeTest();
13+
</script>
14+
</body>
15+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
window.scriptTest = true;
2+
parent.finishTest();

test/data/testinit.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,24 @@ this.testIframe = function( title, fileName, func, wrapper ) {
262262
args.unshift( assert );
263263

264264
setTimeout( function() {
265+
var result;
266+
265267
this.iframeCallback = undefined;
266268

267-
func.apply( this, args );
268-
func = function() {};
269-
$iframe.remove();
270-
done();
269+
result = func.apply( this, args );
270+
271+
function finish() {
272+
func = function() {};
273+
$iframe.remove();
274+
done();
275+
}
276+
277+
// Wait for promises returned by `func`.
278+
if ( result && result.then ) {
279+
result.then( finish );
280+
} else {
281+
finish();
282+
}
271283
} );
272284
};
273285

test/unit/core.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ QUnit.test( "globalEval execution after script injection (#7862)", function( ass
197197
assert.ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
198198
} );
199199

200+
testIframe(
201+
"globalEval with custom document context",
202+
"core/globaleval-context.html",
203+
function( assert, framejQuery, frameWindow, frameDocument ) {
204+
assert.expect( 2 );
205+
206+
jQuery.globalEval( "window.scriptTest = true;", {}, frameDocument );
207+
assert.ok( !window.scriptTest, "script executed in iframe context" );
208+
assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
209+
}
210+
);
211+
212+
200213
QUnit.test( "noConflict", function( assert ) {
201214
assert.expect( 7 );
202215

test/unit/manipulation.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,27 @@ testIframe(
23152315
}
23162316
);
23172317

2318+
testIframe(
2319+
"domManip executes external scripts in iframes in the iframes' context",
2320+
"manipulation/scripts-context.html",
2321+
function( assert, framejQuery, frameWindow, frameDocument ) {
2322+
assert.expect( 2 );
2323+
2324+
Globals.register( "finishTest" );
2325+
2326+
return new Promise( function( resolve ) {
2327+
window.finishTest = resolve;
2328+
jQuery( frameDocument.body ).append(
2329+
"<script src='" + url( "manipulation/set-global-scripttest.js" ) + "'></script>" );
2330+
assert.ok( !window.scriptTest, "script executed in iframe context" );
2331+
assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
2332+
} );
2333+
},
2334+
2335+
// The AJAX module is needed for jQuery._evalUrl.
2336+
QUnit[ jQuery.ajax ? "test" : "skip" ]
2337+
);
2338+
23182339
QUnit.test( "jQuery.clone - no exceptions for object elements #9587", function( assert ) {
23192340

23202341
assert.expect( 1 );

0 commit comments

Comments
 (0)