Skip to content

Commit 22449eb

Browse files
committed
Manipulation: execute scripts from iframe in the iframe's context
Fixes gh-1757 Close gh-2696
1 parent 67fa2ea commit 22449eb

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/core.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,12 @@ jQuery.extend( {
258258
},
259259

260260
// Evaluates a script in a global context
261-
globalEval: function( code ) {
262-
var script = document.createElement( "script" );
261+
globalEval: function( code, context ) {
262+
context = context || document;
263+
var script = context.createElement( "script" );
263264

264265
script.text = code;
265-
document.head.appendChild( script ).parentNode.removeChild( script );
266+
context.head.appendChild( script ).parentNode.removeChild( script );
266267
},
267268

268269
// Convert dashed to camelCase; used by the css and data modules

src/manipulation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function domManip( collection, args, callback, ignored ) {
192192
jQuery._evalUrl( node.src );
193193
}
194194
} else {
195-
jQuery.globalEval( node.textContent.replace( rcleanScript, "" ) );
195+
jQuery.globalEval( node.textContent.replace( rcleanScript, "" ), doc );
196196
}
197197
}
198198
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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>
11+
window.parent.iframeCallback(
12+
window,
13+
document.body,
14+
"<script>window.scriptTest = true;<\x2fscript>"
15+
);
16+
</script>
17+
</body>
18+
</html>

test/unit/manipulation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,17 @@ testIframeWithCallback(
21622162
}
21632163
);
21642164

2165+
testIframeWithCallback(
2166+
"domManip executes scripts in iframes in the iframes' context",
2167+
"manipulation/scripts-context.html",
2168+
function( frameWindow, bodyElement, html, assert ) {
2169+
assert.expect( 2 );
2170+
jQuery( bodyElement ).append( html );
2171+
assert.ok( !window.scriptTest, "script executed in iframe context" );
2172+
assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
2173+
}
2174+
);
2175+
21652176
QUnit.test( "jQuery.clone - no exceptions for object elements #9587", function( assert ) {
21662177

21672178
assert.expect( 1 );

0 commit comments

Comments
 (0)