mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
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
This commit is contained in:
parent
18db87172c
commit
4592595b47
@ -231,9 +231,10 @@ jQuery.extend( {
|
|||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Evaluates a script in a global context
|
// Evaluates a script in a provided context; falls back to the global one
|
||||||
globalEval: function( code, options ) {
|
// if not specified.
|
||||||
DOMEval( code, { nonce: options && options.nonce } );
|
globalEval: function( code, options, doc ) {
|
||||||
|
DOMEval( code, { nonce: options && options.nonce }, doc );
|
||||||
},
|
},
|
||||||
|
|
||||||
each: function( obj, callback ) {
|
each: function( obj, callback ) {
|
||||||
|
@ -163,7 +163,7 @@ function domManip( collection, args, callback, ignored ) {
|
|||||||
if ( jQuery._evalUrl && !node.noModule ) {
|
if ( jQuery._evalUrl && !node.noModule ) {
|
||||||
jQuery._evalUrl( node.src, {
|
jQuery._evalUrl( node.src, {
|
||||||
nonce: node.nonce || node.getAttribute( "nonce" )
|
nonce: node.nonce || node.getAttribute( "nonce" )
|
||||||
} );
|
}, doc );
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );
|
DOMEval( node.textContent.replace( rcleanScript, "" ), node, doc );
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import jQuery from "../ajax.js";
|
import jQuery from "../ajax.js";
|
||||||
|
|
||||||
jQuery._evalUrl = function( url, options ) {
|
jQuery._evalUrl = function( url, options, doc ) {
|
||||||
return jQuery.ajax( {
|
return jQuery.ajax( {
|
||||||
url: url,
|
url: url,
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ jQuery._evalUrl = function( url, options ) {
|
|||||||
"text script": function() {}
|
"text script": function() {}
|
||||||
},
|
},
|
||||||
dataFilter: function( response ) {
|
dataFilter: function( response ) {
|
||||||
jQuery.globalEval( response, options );
|
jQuery.globalEval( response, options, doc );
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
};
|
};
|
||||||
|
15
test/data/core/globaleval-context.html
Normal file
15
test/data/core/globaleval-context.html
Normal file
@ -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
test/data/manipulation/set-global-scripttest.js
Normal file
2
test/data/manipulation/set-global-scripttest.js
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
window.scriptTest = true;
|
||||||
|
parent.finishTest();
|
@ -254,12 +254,24 @@ this.testIframe = function( title, fileName, func, wrapper ) {
|
|||||||
args.unshift( assert );
|
args.unshift( assert );
|
||||||
|
|
||||||
setTimeout( function() {
|
setTimeout( function() {
|
||||||
|
var result;
|
||||||
|
|
||||||
this.iframeCallback = undefined;
|
this.iframeCallback = undefined;
|
||||||
|
|
||||||
func.apply( this, args );
|
result = func.apply( this, args );
|
||||||
|
|
||||||
|
function finish() {
|
||||||
func = function() {};
|
func = function() {};
|
||||||
$iframe.remove();
|
$iframe.remove();
|
||||||
done();
|
done();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for promises returned by `func`.
|
||||||
|
if ( result && result.then ) {
|
||||||
|
result.then( finish );
|
||||||
|
} else {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
} );
|
} );
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -197,6 +197,19 @@ QUnit.test( "globalEval execution after script injection (#7862)", function( ass
|
|||||||
assert.ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
|
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 ) {
|
QUnit.test( "noConflict", function( assert ) {
|
||||||
assert.expect( 7 );
|
assert.expect( 7 );
|
||||||
|
|
||||||
|
@ -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 ) {
|
QUnit.test( "jQuery.clone - no exceptions for object elements #9587", function( assert ) {
|
||||||
|
|
||||||
assert.expect( 1 );
|
assert.expect( 1 );
|
||||||
|
Loading…
Reference in New Issue
Block a user