summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/scoped-registry/scoped-registry-define-upgrade-criteria.tentative.html
blob: d3d5d4fb969d2c9c42b67ed131257ed78d6c1301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<title>Tests which nodes are upgraded after adding a scoped custom element definition</title>
<meta name="author" title="Xiaocheng Hu" href="mailto:xiaochengh@chromium.org">
<link rel="help" href="https://wicg.github.io/webcomponents/proposals/Scoped-Custom-Element-Registries">
<link rel="help" href="https://github.com/WICG/webcomponents/issues/923">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<body>

<script>
function attachShadowForTest(t, registry) {
  const host = document.createElement('div');
  const shadow = host.attachShadow({mode: 'open', registry});
  document.body.appendChild(host);
  t.add_cleanup(() => host.remove());
  return shadow;
}

function createIFrameForTest(t) {
  const iframe = document.createElement('iframe');
  document.body.appendChild(iframe);
  if (!iframe.contentDocument.body) {
    iframe.contentDocument.body = iframe.contentDocument.createElement('body');
  }
  t.add_cleanup(() => iframe.remove());
  return iframe;
}

let definitionCount = 0;
function nextCustomElementName() {
  return `test-element-${++definitionCount}`;
}

test(t => {
  const name = nextCustomElementName();
  document.body.appendChild(document.createElement(name));

  const registry = new CustomElementRegistry;
  const shadow = attachShadowForTest(t, registry);
  shadow.appendChild(document.createElement(name));

  class TestElement extends HTMLElement {};
  customElements.define(name, TestElement);

  assert_true(document.querySelector(name) instanceof TestElement);
  assert_false(shadow.querySelector(name) instanceof TestElement);
}, 'Adding definition to global registry should not affect shadow roots using scoped registry');

test(t => {
  const name = nextCustomElementName();

  const shadow1 = attachShadowForTest(t, customElements);
  shadow1.appendChild(document.createElement(name));

  const shadow2 = attachShadowForTest(t);
  shadow2.appendChild(document.createElement(name));

  class TestElement extends HTMLElement {};
  customElements.define(name, TestElement);

  assert_true(shadow1.querySelector(name) instanceof TestElement);
  assert_true(shadow2.querySelector(name) instanceof TestElement);
}, 'Adding definition to global registry should affect shadow roots also using global registry');

test(t => {
  const name = nextCustomElementName();

  const registry = new CustomElementRegistry;
  const shadow1 = attachShadowForTest(t, registry);
  shadow1.appendChild(document.createElement(name));

  const shadow2 = attachShadowForTest(t, registry);
  shadow2.appendChild(document.createElement(name));

  class TestElement extends HTMLElement {};
  registry.define(name, TestElement);

  assert_true(shadow1.querySelector(name) instanceof TestElement);
  assert_true(shadow2.querySelector(name) instanceof TestElement);
}, 'Adding definition to scoped registry should affect all associated shadow roots');

test(t => {
  const name = nextCustomElementName();
  document.body.appendChild(document.createElement(name));

  const registry = new CustomElementRegistry;
  const shadow = attachShadowForTest(t, registry);
  shadow.appendChild(document.createElement(name));

  class TestElement extends HTMLElement {};
  registry.define(name, TestElement);

  assert_false(document.querySelector(name) instanceof TestElement);
  assert_true(shadow.querySelector(name) instanceof TestElement);
}, 'Adding definition to scoped registry should not affect document tree scope');

test(t => {
  const name = nextCustomElementName();

  const registry = new CustomElementRegistry;
  const shadow1 = attachShadowForTest(t, registry);
  shadow1.appendChild(document.createElement(name));

  const shadow2 = attachShadowForTest(t, new CustomElementRegistry);
  shadow2.appendChild(document.createElement(name));

  const shadow3 = attachShadowForTest(t);
  shadow3.appendChild(document.createElement(name));

  class TestElement extends HTMLElement {};
  registry.define(name, TestElement);

  assert_true(shadow1.querySelector(name) instanceof TestElement);
  assert_false(shadow2.querySelector(name) instanceof TestElement);
  assert_false(shadow3.querySelector(name) instanceof TestElement);
}, 'Adding definition to scoped registry should not affect shadow roots using other registries');

test(t => {
  const name = nextCustomElementName();
  const node = document.body.appendChild(document.createElement(name));

  const registry = new CustomElementRegistry;
  const shadow = attachShadowForTest(t, registry);
  shadow.appendChild(node);

  class TestElement extends HTMLElement {};
  customElements.define(name, TestElement);

  assert_false(node instanceof TestElement);
}, 'Adding definition to global registry should not upgrade nodes no longer using the registry');

test(t => {
  const name = nextCustomElementName();

  const registry = new CustomElementRegistry;
  const shadow1 = attachShadowForTest(t, registry);
  const node = shadow1.appendChild(document.createElement(name));

  const shadow2 = attachShadowForTest(t, new CustomElementRegistry);
  shadow2.appendChild(node);

  class TestElement extends HTMLElement {};
  registry.define(name, TestElement);

  assert_false(node instanceof TestElement);
}, 'Adding definition to scoped registry should not upgrade nodes no longer using the registry');

test(t => {
  const name = nextCustomElementName();

  const registry = new CustomElementRegistry;
  const shadow1 = attachShadowForTest(t, registry);
  const node1 = shadow1.appendChild(document.createElement(name));

  const iframe = createIFrameForTest(t);
  const host2 = iframe.contentDocument.createElement('div');
  const shadow2 = host2.attachShadow({mode: 'open', registry});
  const node2 = shadow2.appendChild(iframe.contentDocument.createElement(name));
  iframe.contentDocument.body.appendChild(host2);

  class TestElement extends HTMLElement {};
  registry.define(name, TestElement);

  assert_true(node1 instanceof TestElement);
  assert_true(node2 instanceof TestElement);
}, 'Adding definition to scoped registry affects associated shadow roots in all iframes');

test(t => {
  const name = nextCustomElementName();

  const newWindow = window.open('about:blank');
  t.add_cleanup(() => newWindow.close());

  const host = newWindow.document.createElement('div');
  const shadow = host.attachShadow({mode: 'open', registry: window.customElements});
  const node = shadow.appendChild(newWindow.document.createElement(name));
  newWindow.document.body.appendChild(host);

  class TestElement extends HTMLElement {};
  window.customElements.define(name, TestElement);

  assert_true(node instanceof TestElement);
}, 'Adding definition to scoped registry affects associated shadow roots in other frame trees');

test(t => {
  const name = nextCustomElementName();

  const registry = new CustomElementRegistry;
  const shadow = attachShadowForTest(t, registry);
  const node = shadow.appendChild(document.createElement(name));
  shadow.host.remove();

  class TestElement extends HTMLElement {};
  registry.define(name, TestElement);

  assert_false(node instanceof TestElement);
}, 'Adding definition to scoped registry should not upgrade disconnected elements');

test(t => {
  const name = nextCustomElementName();

  const registry = new CustomElementRegistry;
  const doc = document.implementation.createHTMLDocument();
  const host = doc.createElement('div');
  const shadow = host.attachShadow({mode: 'open', registry});
  const node = shadow.appendChild(doc.createElement(name));
  doc.body.appendChild(host);

  class TestElement extends HTMLElement {};
  registry.define(name, TestElement);

  assert_false(node instanceof TestElement);
}, 'Adding definition to scoped registry should not upgrade nodes in constructed documents');

test(t => {
  const name = nextCustomElementName();

  const iframe = createIFrameForTest(t);
  const registry = new CustomElementRegistry;
  const host = iframe.contentDocument.createElement('div');
  const shadow = host.attachShadow({mode: 'open', registry});
  const node = shadow.appendChild(iframe.contentDocument.createElement(name));
  iframe.contentDocument.body.appendChild(host);

  iframe.remove();

  class TestElement extends HTMLElement {};
  registry.define(name, TestElement);

  assert_false(node instanceof TestElement);
}, 'Adding definition to scoped registry should not upgrade nodes in detached frames');

promise_test(async t => {
  const name = nextCustomElementName();

  const newWindow = window.open('about:blank');
  t.add_cleanup(() => newWindow.close());

  const host = newWindow.document.createElement('div');
  const shadow = host.attachShadow({mode: 'open', registry: window.customElements});
  const node = shadow.appendChild(newWindow.document.createElement(name));
  newWindow.document.body.appendChild(host);

  newWindow.close();

  // `window.close()` is async. Wait a while until it's fully closed
  await new Promise(resolve => setTimeout(resolve, 500));

  class TestElement extends HTMLElement {};
  window.customElements.define(name, TestElement);

  assert_false(node instanceof TestElement);
}, 'Adding definition to scoped registry should not upgrade nodes in closed windows');

</script>

</body>