blob: 4a3c8ec43b0f23cd890a1daeeeb4c20941c96b2a [file] [log] [blame]
Ryosuke Niwaf7341962022-08-05 06:48:121<?xml version="1.0" encoding="utf-8"?>
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<title>Custom Elements: create an element for a token must increment and decrement document's throw-on-dynamic-markup-insertion counter</title>
5<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org" />
6<meta name="assert" content="Invoking document.open, document.write, document.writeln, and document.write must throw an exception when the HTML parser is creating a custom element for a token" />
7<meta name="help" content="https://html.spec.whatwg.org/multipage/parsing.html#create-an-element-for-the-token" />
8<meta name="help" content="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#throw-on-dynamic-markup-insertion-counter" />
9<script src="/resources/testharness.js"></script>
10<script src="/resources/testharnessreport.js"></script>
11<script src="resources/custom-elements-helpers.js"></script>
12</head>
13<body>
14<div id="log"></div>
15<script>
16<![CDATA[
17
18async function custom_element_reactions_in_parser(test, code)
19{
20 window.executed = false;
L. David Baron63e0e702023-06-30 16:43:5121 window.exception = null;
Ryosuke Niwaf7341962022-08-05 06:48:1222 const content_window = await create_window_in_test_async(test, 'application/xml', `<?xml version="1.0" encoding="utf-8"?>
23<html xmlns="http://www.w3.org/1999/xhtml">
24<head>
25<script>
26<![CDATA[
Ryosuke Niwaf7341962022-08-05 06:48:1227class CustomElement extends window.HTMLElement {
28 constructor() {
29 super();
L. David Baron63e0e702023-06-30 16:43:5130 let exception = null;
Ryosuke Niwaf7341962022-08-05 06:48:1231 try {
32 ${code}
33 } catch (error) {
34 exception = error;
35 }
L. David Baron63e0e702023-06-30 16:43:5136 top.executed = true;
37 top.exception = exception;
Ryosuke Niwaf7341962022-08-05 06:48:1238 }
39}
40CustomElement.observedAttributes = ['title'];
41customElements.define('some-element', CustomElement);
42]]` + `>
43</` + `script>
44</head>
45<body>
46<some-element title="some title"></some-element>
Ryosuke Niwaf7341962022-08-05 06:48:1247</body>
48</html>`);
49 let content_document;
50 try {
51 content_document = content_window.document;
52 } catch (error) { }
53 assert_true(executed, 'Must immediately process custom element reactions for setting attributes');
54 return {window: content_window, document: content_document, exception};
55}
56
57promise_test(async function () {
58 const result = await custom_element_reactions_in_parser(this, `document.open()`);
59 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
60}, 'document.open() must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
61
62promise_test(async function () {
63 const result = await custom_element_reactions_in_parser(this, `document.open('text/html')`);
64 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
65}, 'document.open("text/html") must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
66
67// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-open-window
68promise_test(async function () {
69 let load_promise = new Promise((resolve) => window.onmessage = (event) => resolve(event.data));
70 const url = top.location.href.substring(0, top.location.href.lastIndexOf('/')) + '/resources/navigation-destination.html';
71 const result = await custom_element_reactions_in_parser(this, `document.open('${url}', '_self', '')`);
72 assert_equals(result.exception, null);
73 assert_equals(await load_promise, 'didNavigate');
74}, 'document.open(URL) must NOT throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
75
76promise_test(async function () {
77 const result = await custom_element_reactions_in_parser(this, `document.close()`);
78 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
79}, 'document.close() must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
80
81promise_test(async function () {
82 const result = await custom_element_reactions_in_parser(this, `document.write('<b>some text</b>')`);
83 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
84 assert_equals(result.document.querySelector('b'), null, 'Must not insert new content');
85 assert_false(result.document.body.innerHTML.includes('some text'), 'Must not insert new content');
86}, 'document.write must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
87
88promise_test(async function () {
89 const result = await custom_element_reactions_in_parser(this, `document.writeln('<b>some text</b>')`);
90 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
91 assert_equals(result.document.querySelector('b'), null, 'Must not insert new content');
92 assert_false(result.document.body.innerHTML.includes('some text'), 'Must not insert new content');
93}, 'document.writeln must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
94
95promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:0896 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:1297 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.open()`);
98 assert_equals(result.exception, null);
99}, 'document.open() of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
100
101promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08102 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12103 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.open('text/html')`);
104 assert_equals(result.exception, null);
105}, 'document.open("text/html") of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
106
107promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08108 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12109 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.close()`);
110 assert_equals(result.exception, null);
111}, 'document.close() of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
112
113promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08114 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12115 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.write('<b>some text</b>')`);
116 assert_equals(result.exception, null);
117 assert_equals(another_window.document.querySelector('b').outerHTML, '<b>some text</b>');
118}, 'document.write of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
119
120promise_test(async function () {
Ryosuke Niwa853d9c32022-08-05 08:31:08121 window.another_window = await create_window_in_test(this);
Ryosuke Niwaf7341962022-08-05 06:48:12122 const result = await custom_element_reactions_in_parser(this, `top.another_window.document.writeln('<b>some text</b>')`);
123 assert_equals(result.exception, null);
124 assert_equals(another_window.document.querySelector('b').outerHTML, '<b>some text</b>');
125}, 'document.writeln of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
126
127]]>
128</script>
129</body>
130</html>