Skip to content

Commit

Permalink
Test moving <script>s between documents
Browse files Browse the repository at this point in the history
This follows the changes in whatwg/html#2673, but also tests the issue at whatwg/html#2137 in favor of the current spec.
  • Loading branch information
domenic authored and hiroshige-g committed Dec 18, 2019
1 parent db7735a commit d64d5b6
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
@@ -0,0 +1,138 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Moving script elements between documents</title>
<link rel="author" href="mailto:d@domenic.me" title="Domenic Denicola">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#execute-the-script-block">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<!-- Background:
- https://www.w3.org/Bugs/Public/show_bug.cgi?id=11323
- https://github.com/whatwg/html/issues/2137
- https://github.com/whatwg/html/issues/2469
- https://github.com/whatwg/html/pull/2673
-->

<body>
<script>
"use strict";
window.didExecute = false;

async_test(t => {
t.add_cleanup(() => {
window.didExecute = false;
});

const iframe = document.createElement("iframe");
iframe.onload = t.step_func_done(() => {
iframe.contentWindow.didExecute = false;
iframe.contentDocument.write("<streaming-element>");
document.body.appendChild(iframe.contentDocument.querySelector("streaming-element"));
iframe.contentDocument.write(`<script id="s1">window.didExecute = true;<` + "/script>");
iframe.contentDocument.write("</streaming-element>");

const s = document.querySelector("#s1");
s.onload = t.unreached_func("onload");
s.onerror = t.unreached_func("onerror");

iframe.contentDocument.close();

assert_false(window.didExecute, "The script must not have executed in this window");
assert_equals(iframe.contentWindow.didExecute, undefined,
"The script must not have executed in the iframe window");
});

document.body.appendChild(iframe);
}, "Moving to another document during parsing, inline script");

async_test(t => {
t.add_cleanup(() => {
window.didExecute = false;
});

const iframe = document.createElement("iframe");
iframe.onload = t.step_func(() => {
iframe.contentWindow.didExecute = false;
iframe.contentDocument.write("<streaming-element>");
document.body.appendChild(iframe.contentDocument.querySelector("streaming-element"));
iframe.contentDocument.write(`<script id="s2" src="resources/flag-setter.js"><` + "/script>");
iframe.contentDocument.write("</streaming-element>");

const s = document.querySelector("#s2");
s.onload = t.unreached_func("onload");
s.onerror = t.unreached_func("onerror");

iframe.contentDocument.close();

t.step_timeout(() => {
assert_false(window.didExecute, "The script must not have executed in this window");
assert_equals(iframe.contentWindow.didExecute, undefined,
"The script must not have executed in the iframe window");
t.done();
}, 3000);
});

document.body.appendChild(iframe);
}, "Moving to another document during parsing, external script");

async_test(t => {
t.add_cleanup(() => {
window.didExecute = false;
});

const iframe = document.createElement("iframe");
iframe.onload = t.step_func(() => {
const s = document.createElement("script");
s.src = "resources/slow-flag-setter.py";
s.onload = t.unreached_func("onload");
s.onerror = t.unreached_func("onerror");

// Start the fetch
document.body.appendChild(s);

// Need to delay since the "prepare a script" algorithm also contains related checks; we want to
// test the "execute a script block" algorithm for when the fetch comes back.
t.step_timeout(() => {
iframe.contentDocument.body.appendChild(s);
}, 0);

t.step_timeout(() => {
assert_false(window.didExecute, "The script must not have executed in this window");
assert_equals(iframe.contentWindow.didExecute, undefined,
"The script must not have executed in the iframe window");
t.done();
}, 3000);
});

document.body.appendChild(iframe);
}, "Moving to another Window's document during fetching");

async_test(t => {
t.add_cleanup(() => {
window.didExecute = false;
});

const s = document.createElement("script");
s.src = "resources/slow-flag-setter.py";
s.onload = t.unreached_func("onload");
s.onerror = t.unreached_func("onerror");

// Start the fetch
document.body.appendChild(s);

// Need to delay since the "prepare a script" algorithm also contains related checks; we want to
// test the "execute a script block" algorithm for when the fetch comes back.
t.step_timeout(() => {
const doc2 = document.implementation.createHTMLDocument("title");
doc2.body.appendChild(s);
}, 0);

t.step_timeout(() => {
assert_false(window.didExecute, "The script must not have executed in this window");
t.done();
}, 3000);
}, "Moving to a document where scripting is disabled during fetching");
</script>
@@ -0,0 +1,3 @@
"use strict";

window.didExecute = true;
@@ -0,0 +1,10 @@

import time

def main(request, response):
time.sleep(1)

headers = [("Content-Type", "text/javascript")]
body = "window.didExecute = true;"

return headers, body

0 comments on commit d64d5b6

Please sign in to comment.