blob: e798d332e38b2e85703e7752486fe221376212db [file] [log] [blame]
Darshan Kadu98224e62018-11-23 19:33:151<!DOCTYPE html>
2<html>
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>
Ryosuke Niwa32886e62018-11-29 09:01:3211<script src="./resources/custom-elements-helpers.js"></script>
Darshan Kadu98224e62018-11-23 19:33:1512</head>
13<body>
14<div id="log"></div>
15<script>
16
17async function custom_element_reactions_in_parser(test, call_function)
18{
19 const window = await create_window_in_test(test);
20 const document = window.document;
21
22 document.open();
Philip Jägenstedtf87822f2019-10-23 10:29:0423
Darshan Kadu98224e62018-11-23 19:33:1524 let executed = false;
25 let exception = null;
26 class CustomElement extends window.HTMLElement {
27 attributeChangedCallback(name, oldValue, newValue) {
28 try {
29 call_function(document, window);
30 } catch (error) {
31 exception = error;
32 }
33 executed = true;
34 }
35 }
36 CustomElement.observedAttributes = ['title'];
37 window.customElements.define('some-element', CustomElement);
Philip Jägenstedtf87822f2019-10-23 10:29:0438
Darshan Kadu98224e62018-11-23 19:33:1539 document.write('<!DOCTYPE html><html><body><some-element title="some title"></some-element></body></html>');
40 document.close();
41
42 assert_true(executed, 'Must immediately process custom element reactions for setting attributes');
43 return {frameElement, window, document, exception};
44}
45
46promise_test(async function () {
47 const result = await custom_element_reactions_in_parser(this, (document) => document.open());
Boris Zbarsky4ba5bc32020-02-19 05:56:3548 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
Darshan Kadu98224e62018-11-23 19:33:1549}, 'document.open() must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
Philip Jägenstedtf87822f2019-10-23 10:29:0450
Darshan Kadu98224e62018-11-23 19:33:1551promise_test(async function () {
52 const result = await custom_element_reactions_in_parser(this, (document) => document.open('text/html'));
Boris Zbarsky4ba5bc32020-02-19 05:56:3553 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
Darshan Kadu98224e62018-11-23 19:33:1554}, 'document.open("text/html") must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
Philip Jägenstedtf87822f2019-10-23 10:29:0455
Darshan Kadu98224e62018-11-23 19:33:1556// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-open-window
57promise_test(async function () {
58 let load_promise = new Promise((resolve) => window.onmessage = (event) => resolve(event.data));
59 const result = await custom_element_reactions_in_parser(this, (document, window) => document.open('resources/navigation-destination.html', '_self', ''));
60 assert_equals(result.exception, null);
61 assert_equals(await load_promise, 'didNavigate');
62}, 'document.open(URL) must NOT throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
63
64promise_test(async function () {
65 const result = await custom_element_reactions_in_parser(this, (document) => document.close());
Boris Zbarsky4ba5bc32020-02-19 05:56:3566 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
Darshan Kadu98224e62018-11-23 19:33:1567}, 'document.close() must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
68
69promise_test(async function () {
70 const result = await custom_element_reactions_in_parser(this, (document) => document.write('<b>some text</b>'));
Boris Zbarsky4ba5bc32020-02-19 05:56:3571 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
Darshan Kadu98224e62018-11-23 19:33:1572 assert_equals(result.document.querySelector('b'), null, 'Must not insert new content');
73 assert_false(result.document.documentElement.innerHTML.includes('some text'), 'Must not insert new content');
74}, 'document.write must 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) => document.writeln('<b>some text</b>'));
Boris Zbarsky4ba5bc32020-02-19 05:56:3578 assert_throws_dom('InvalidStateError', result.window.DOMException, () => { throw result.exception; }, 'Must throw an InvalidStateError');
Darshan Kadu98224e62018-11-23 19:33:1579 assert_equals(result.document.querySelector('b'), null, 'Must not insert new content');
80 assert_false(result.document.documentElement.innerHTML.includes('some text'), 'Must not insert new content');
81}, 'document.writeln must throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
82
83promise_test(async function () {
84 const another_window = await create_window_in_test(this);
85 const result = await custom_element_reactions_in_parser(this, (document) => another_window.document.open());
86 assert_equals(result.exception, null);
87}, 'document.open() of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
88
89promise_test(async function () {
90 const another_window = await create_window_in_test(this);
91 const result = await custom_element_reactions_in_parser(this, (document) => another_window.document.open('text/html'));
92 assert_equals(result.exception, null);
93}, 'document.open("text/html") of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
94
95promise_test(async function () {
96 const another_window = await create_window_in_test(this);
97 const result = await custom_element_reactions_in_parser(this, (document) => another_window.document.close());
98 assert_equals(result.exception, null);
99}, 'document.close() of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
100
101promise_test(async function () {
102 const another_window = await create_window_in_test(this);
103 const result = await custom_element_reactions_in_parser(this, (document) => another_window.document.write('<b>some text</b>'));
104 assert_equals(result.exception, null);
105 assert_equals(another_window.document.querySelector('b').outerHTML, '<b>some text</b>');
106}, 'document.write of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
107
108promise_test(async function () {
109 const another_window = await create_window_in_test(this);
110 const result = await custom_element_reactions_in_parser(this, (document) => another_window.document.writeln('<b>some text</b>'));
111 assert_equals(result.exception, null);
112 assert_equals(another_window.document.querySelector('b').outerHTML, '<b>some text</b>');
113}, 'document.writeln of another document must not throw an InvalidStateError when processing custom element reactions for a synchronous constructed custom element');
114
115</script>
116</body>
117</html>