diff options
author | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2020-02-10 19:17:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-10 19:17:22 +0100 |
commit | 4592595b478be979141ce35c693dbc6b65647173 (patch) | |
tree | 03020ecb8c12dc18efcdda7987c9d73595ae1202 /test | |
parent | 18db87172cffbe48b92e30b70249e304863a70f9 (diff) | |
download | jquery-4592595b478be979141ce35c693dbc6b65647173.tar.gz jquery-4592595b478be979141ce35c693dbc6b65647173.zip |
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
Diffstat (limited to 'test')
-rw-r--r-- | test/data/core/globaleval-context.html | 15 | ||||
-rw-r--r-- | test/data/manipulation/set-global-scripttest.js | 2 | ||||
-rw-r--r-- | test/data/testinit.js | 20 | ||||
-rw-r--r-- | test/unit/core.js | 13 | ||||
-rw-r--r-- | test/unit/manipulation.js | 21 |
5 files changed, 67 insertions, 4 deletions
diff --git a/test/data/core/globaleval-context.html b/test/data/core/globaleval-context.html new file mode 100644 index 000000000..1b75e3a0a --- /dev/null +++ b/test/data/core/globaleval-context.html @@ -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> diff --git a/test/data/manipulation/set-global-scripttest.js b/test/data/manipulation/set-global-scripttest.js new file mode 100644 index 000000000..7fdbf9d72 --- /dev/null +++ b/test/data/manipulation/set-global-scripttest.js @@ -0,0 +1,2 @@ +window.scriptTest = true; +parent.finishTest(); diff --git a/test/data/testinit.js b/test/data/testinit.js index 61ffcbc8f..29a8a9f9a 100644 --- a/test/data/testinit.js +++ b/test/data/testinit.js @@ -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(); + } } ); }; diff --git a/test/unit/core.js b/test/unit/core.js index bc5d2fc25..9ea4702b3 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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 ); diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index b71a54799..b347fe59c 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -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 ); |