Skip to content

Commit 0cf1fef

Browse files
domenichiroshige-g
authored andcommitted
Test moving <script>s between documents
This follows the changes in whatwg/html#2673, but also tests the issue at whatwg/html#2137 in favor of the current spec.
1 parent 643c701 commit 0cf1fef

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>Moving script elements between documents</title>
4+
<link rel="author" href="mailto:[email protected]" title="Domenic Denicola">
5+
<link rel="help" href="https://html.spec.whatwg.org/multipage/#execute-the-script-block">
6+
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
10+
<!-- Background:
11+
12+
- https://www.w3.org/Bugs/Public/show_bug.cgi?id=11323
13+
- https://github.com/whatwg/html/issues/2137
14+
- https://github.com/whatwg/html/issues/2469
15+
- https://github.com/whatwg/html/pull/2673
16+
17+
-->
18+
19+
<body>
20+
<script>
21+
"use strict";
22+
window.didExecute = false;
23+
24+
async_test(t => {
25+
t.add_cleanup(() => {
26+
window.didExecute = false;
27+
});
28+
29+
const iframe = document.createElement("iframe");
30+
iframe.onload = t.step_func_done(() => {
31+
iframe.contentWindow.didExecute = false;
32+
iframe.contentDocument.write("<streaming-element>");
33+
document.body.appendChild(iframe.contentDocument.querySelector("streaming-element"));
34+
iframe.contentDocument.write(`<script id="s1">window.didExecute = true;<` + "/script>");
35+
iframe.contentDocument.write("</streaming-element>");
36+
37+
const s = document.querySelector("#s1");
38+
s.onload = t.unreached_func("onload");
39+
s.onerror = t.unreached_func("onerror");
40+
41+
iframe.contentDocument.close();
42+
43+
assert_false(window.didExecute, "The script must not have executed in this window");
44+
assert_equals(iframe.contentWindow.didExecute, undefined,
45+
"The script must not have executed in the iframe window");
46+
});
47+
48+
document.body.appendChild(iframe);
49+
}, "Moving to another document during parsing, inline script");
50+
51+
async_test(t => {
52+
t.add_cleanup(() => {
53+
window.didExecute = false;
54+
});
55+
56+
const iframe = document.createElement("iframe");
57+
iframe.onload = t.step_func(() => {
58+
iframe.contentWindow.didExecute = false;
59+
iframe.contentDocument.write("<streaming-element>");
60+
document.body.appendChild(iframe.contentDocument.querySelector("streaming-element"));
61+
iframe.contentDocument.write(`<script id="s2" src="resources/flag-setter.js"><` + "/script>");
62+
iframe.contentDocument.write("</streaming-element>");
63+
64+
const s = document.querySelector("#s2");
65+
s.onload = t.unreached_func("onload");
66+
s.onerror = t.unreached_func("onerror");
67+
68+
iframe.contentDocument.close();
69+
70+
t.step_timeout(() => {
71+
assert_false(window.didExecute, "The script must not have executed in this window");
72+
assert_equals(iframe.contentWindow.didExecute, undefined,
73+
"The script must not have executed in the iframe window");
74+
t.done();
75+
}, 3000);
76+
});
77+
78+
document.body.appendChild(iframe);
79+
}, "Moving to another document during parsing, external script");
80+
81+
async_test(t => {
82+
t.add_cleanup(() => {
83+
window.didExecute = false;
84+
});
85+
86+
const iframe = document.createElement("iframe");
87+
iframe.onload = t.step_func(() => {
88+
const s = document.createElement("script");
89+
s.src = "resources/slow-flag-setter.py";
90+
s.onload = t.unreached_func("onload");
91+
s.onerror = t.unreached_func("onerror");
92+
93+
// Start the fetch
94+
document.body.appendChild(s);
95+
96+
// Need to delay since the "prepare a script" algorithm also contains related checks; we want to
97+
// test the "execute a script block" algorithm for when the fetch comes back.
98+
t.step_timeout(() => {
99+
iframe.contentDocument.body.appendChild(s);
100+
}, 0);
101+
102+
t.step_timeout(() => {
103+
assert_false(window.didExecute, "The script must not have executed in this window");
104+
assert_equals(iframe.contentWindow.didExecute, undefined,
105+
"The script must not have executed in the iframe window");
106+
t.done();
107+
}, 3000);
108+
});
109+
110+
document.body.appendChild(iframe);
111+
}, "Moving to another Window's document during fetching");
112+
113+
async_test(t => {
114+
t.add_cleanup(() => {
115+
window.didExecute = false;
116+
});
117+
118+
const s = document.createElement("script");
119+
s.src = "resources/slow-flag-setter.py";
120+
s.onload = t.unreached_func("onload");
121+
s.onerror = t.unreached_func("onerror");
122+
123+
// Start the fetch
124+
document.body.appendChild(s);
125+
126+
// Need to delay since the "prepare a script" algorithm also contains related checks; we want to
127+
// test the "execute a script block" algorithm for when the fetch comes back.
128+
t.step_timeout(() => {
129+
const doc2 = document.implementation.createHTMLDocument("title");
130+
doc2.body.appendChild(s);
131+
}, 0);
132+
133+
t.step_timeout(() => {
134+
assert_false(window.didExecute, "The script must not have executed in this window");
135+
t.done();
136+
}, 3000);
137+
}, "Moving to a document where scripting is disabled during fetching");
138+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
window.didExecute = true;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import time
3+
4+
def main(request, response):
5+
time.sleep(1)
6+
7+
headers = [("Content-Type", "text/javascript")]
8+
body = "window.didExecute = true;"
9+
10+
return headers, body

0 commit comments

Comments
 (0)