summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shadow-dom/declarative/declarative-shadow-dom-basic.html
blob: 8bc6bec5f50f741e8b48e5c2f1020707c8d56408 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!DOCTYPE html>
<meta charset="utf-8">
<title>Declarative Shadow DOM</title>
<link rel="author" href="mailto:masonf@chromium.org">
<link rel="help" href="https://github.com/whatwg/dom/issues/831">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<div id="host" style="display:none">
  <template shadowrootmode="open">
    <slot id="s1" name="slot1"></slot>
  </template>
  <div id="c1" slot="slot1"></div>
</div>

<script>
test(() => {
  const host = document.querySelector('#host');
  const c1 = host.querySelector('#c1');
  assert_true(!!c1);
  assert_equals(host.querySelector('#s1'), null, "Should be inside shadow root");
  assert_equals(host.querySelector('template'), null, "No leftover template node");
  assert_true(!!host.shadowRoot,"No shadow root found");
  const s1 = host.shadowRoot.querySelector('#s1');
  assert_equals(c1.assignedSlot, s1);
  assert_array_equals(s1.assignedNodes(), [c1]);
}, 'Declarative Shadow DOM: Basic test');

test(() => {
  assert_true(HTMLTemplateElement.prototype.hasOwnProperty("shadowRootMode"),'Unable to feature detect');
}, 'Declarative Shadow DOM: Feature detection');

test(() => {
  const t = document.createElement('template');
  t.setAttribute('shadowrootmode','open');
  assert_equals(t.shadowRootMode,'open','The shadowRootMode IDL should reflect the content attribute');
  t.setAttribute('shadowrootmode','closed');
  assert_equals(t.shadowRootMode,'closed','"open" and "closed" should both be valid values');
  t.setAttribute('shadowrootmode','OpEn');
  assert_equals(t.shadowRootMode,'open','Case insensitive');
  t.setAttribute('shadowrootmode','INVALID');
  assert_equals(t.shadowRootMode,'','Invalid values map to empty string');
  t.removeAttribute('shadowrootmode');
  assert_equals(t.shadowRootMode,'','No shadowrootmode attribute maps to empty string');
}, 'Shadowrootmode reflection');

test(() => {
  const t = document.createElement('template');
  t.shadowRootMode = 'blah';
  assert_equals(t.shadowRootMode, '');
  t.getAttribute('shadowrootmode', 'blah');
  t.shadowRootMode = 'CLOSED';
  assert_equals(t.shadowRootMode, 'closed');
  t.getAttribute('shadowrootmode', 'CLOSED');
}, 'Shadowrootmode reflection, setter');

test(() => {
  const div = document.createElement('div');
  div.setHTMLUnsafe(`
    <div id="host">
      <template shadowrootmode="open">
        <slot id="s1" name="slot1"></slot>
      </template>
      <div id="c1" slot="slot1"></div>
    </div>
    `);
  const host = div.querySelector('#host');
  const c1 = host.querySelector('#c1');
  assert_true(!!c1);
  assert_equals(host.querySelector('#s1'), null, "Should be inside shadow root");
  assert_equals(host.querySelector('template'), null, "No leftover template node");
  assert_true(!!host.shadowRoot,"No shadow root found");
  const s1 = host.shadowRoot.querySelector('#s1');
  assert_equals(c1.assignedSlot, s1);
  assert_array_equals(s1.assignedNodes(), [c1]);
}, 'Declarative Shadow DOM: Fragment parser basic test');

test(() => {
  const div = document.createElement('div');
  div.setHTMLUnsafe(`
    <div id="host">
      <template shadowrootmode="invalid">
      </template>
    </div>
    `);
  const host = div.querySelector('#host');
  assert_equals(host.shadowRoot, null, "Shadow root was found");
  const tmpl = host.querySelector('template');
  assert_true(!!tmpl,"Template should still be present");
  const shadowrootAttr = tmpl.getAttribute('shadowrootmode');
  assert_equals(shadowrootAttr,"invalid","'shadowrootmode' attribute should still be present");
}, 'Declarative Shadow DOM: Invalid shadow root attribute');

test(() => {
  const div = document.createElement('div');
  div.setHTMLUnsafe(`
    <div id="host">
      <template shadowrootmode="closed">
      </template>
    </div>
    `);
  const host = div.querySelector('#host');
  assert_equals(host.shadowRoot, null, "Closed shadow root");
  assert_equals(host.querySelector('template'), null, "No template - converted to shadow root");
}, 'Declarative Shadow DOM: Closed shadow root attribute');

test(() => {
  const div = document.createElement('div');
  div.setHTMLUnsafe(`
    <div id="host">
      <template shadowrootmode="open">
        <slot id="s1" name="slot1"></slot>
    </div>
    `);
  const host = div.querySelector('#host');
  assert_equals(host.querySelector('#s1'), null, "Should be inside shadow root");
  assert_equals(host.querySelector('template'), null, "No leftover template node");
  assert_true(!!host.shadowRoot,"No shadow root found");
  const s1 = host.shadowRoot.querySelector('#s1');
  assert_true(!!s1,"Slot should be inside the shadow root");
}, 'Declarative Shadow DOM: Missing closing tag');

test(() => {
  const div = document.createElement('div');
  div.setHTMLUnsafe(`
    <div id="host">
      <template shadowrootmode="open" shadowrootdelegatesfocus>
      </template>
    </div>
    `);
  var host = div.querySelector('#host');
  assert_true(!!host.shadowRoot,"No shadow root found");
  assert_true(host.shadowRoot.delegatesFocus,"delegatesFocus should be true");
  div.setHTMLUnsafe(`
    <div id="host">
      <template shadowrootmode="open">
      </template>
    </div>
    `);
  host = div.querySelector('#host');
  assert_true(!!host.shadowRoot,"No shadow root found");
  assert_false(host.shadowRoot.delegatesFocus,"delegatesFocus should be false without the shadowrootdelegatesfocus attribute");
}, 'Declarative Shadow DOM: delegates focus attribute');

test(() => {
  const div = document.createElement('div');
  div.setHTMLUnsafe(`
    <div id="host">
      <template shadowrootmode="open" shadowrootclonable>
      </template>
    </div>
    `);
  var host = div.querySelector('#host');
  assert_true(!!host.shadowRoot,"No shadow root found");
  assert_true(host.shadowRoot.clonable,"clonable should be true");
  div.setHTMLUnsafe(`
    <div id="host">
      <template shadowrootmode="open">
      </template>
    </div>
    `);
  host = div.querySelector('#host');
  assert_true(!!host.shadowRoot,"No shadow root found");
  assert_false(host.shadowRoot.clonable,"clonable should be false without the shadowrootclonable attribute");
}, 'Declarative Shadow DOM: clonable attribute');
</script>

<div id="multi-host" style="display:none">
  <template shadowrootmode="open">
    <span>root 1</span>
  </template>
  <template shadowrootmode="closed">
    <span>root 2</span>
  </template>
</div>
<script>
test(() => {
  const host = document.querySelector('#multi-host');
  const leftover = host.querySelector('template');
  assert_true(!!leftover, "The second (duplicate) template should be left in the DOM");
  assert_true(leftover instanceof HTMLTemplateElement);
  assert_equals(leftover.getAttribute('shadowrootmode'),"closed");
  assert_equals(leftover.shadowRootMode,"closed");
  assert_true(!!host.shadowRoot,"No open shadow root found - first root should remain");
  const innerSpan = host.shadowRoot.querySelector('span');
  assert_equals(innerSpan.textContent, 'root 1', "Content should come from first declarative shadow root");
}, 'Declarative Shadow DOM: Multiple roots');

</script>

<template id="template-containing-shadow">
  <div class="innerdiv">
    <template shadowrootmode=open shadowrootclonable>Content</template>
  </div>
</template>
<script>
test(() => {
  const template = document.querySelector('#template-containing-shadow');
  const container1 = document.createElement('div');
  container1.style.display = 'none';
  document.body.appendChild(container1);
  container1.appendChild(template.content.cloneNode(true));
  let innerDiv = container1.querySelector('div.innerdiv');
  const shadowRoot1 = innerDiv.shadowRoot;
  assert_true(!!shadowRoot1,"Inner div should have a shadow root");
  assert_equals(innerDiv.querySelector('template'), null, "No leftover template node");

  const container2 = document.createElement('div');
  container2.style.display = 'none';
  document.body.appendChild(container2);
  container2.appendChild(template.content.cloneNode(true));
  innerDiv = container2.querySelector('div.innerdiv');
  const shadowRoot2 = innerDiv.shadowRoot;
  assert_true(!!shadowRoot2,"Inner div should have a shadow root");
  assert_equals(innerDiv.querySelector('template'), null, "No leftover template node");

  assert_not_equals(shadowRoot1,shadowRoot2,'Should not get back the same shadow root');

  // Make sure importNode also works.
  const container3 = document.createElement('div');
  container3.style.display = 'none';
  document.body.appendChild(container3);
  container3.appendChild(document.importNode(template.content,true));
  innerDiv = container3.querySelector('div.innerdiv');
  const shadowRoot3 = innerDiv.shadowRoot;
  assert_true(!!shadowRoot3,"Inner div should have a shadow root");
  assert_equals(innerDiv.querySelector('template'), null, "No leftover template node");
  assert_not_equals(shadowRoot1,shadowRoot3,'Should not get back the same shadow root');

}, 'Declarative Shadow DOM: template containing declarative shadow root (with shadowrootclonable)');
</script>

<template id="template-containing-deep-shadow">
  <div><div><div><div><div>
    <div class="innerdiv">
      <template shadowrootmode=open shadowrootclonable>Content</template>
    </div>
  </div></div></div></div></div>
</template>
<script>
test(() => {
  const template = document.querySelector('#template-containing-deep-shadow');
  const host = document.createElement('div');
  host.style.display = 'none';
  document.body.appendChild(host);
  host.appendChild(template.content.cloneNode(true));
  assert_true(!!host.querySelector('div.innerdiv').shadowRoot,"Inner div should have a shadow root");
}, 'Declarative Shadow DOM: template containing (deeply nested) declarative shadow root');
</script>

<template id="template-containing-template">
  <div>
    <template id="inner-template">
      <div class="innerdiv">
        <template shadowrootmode=open shadowrootclonable>Content</template>
      </div>
    </template>
  </div>
</template>
<script>
test(() => {
  const template = document.querySelector('#template-containing-template');
  const host = document.createElement('div');
  host.style.display = 'none';
  document.body.appendChild(host);
  host.appendChild(template.content.cloneNode(true));
  const innerTemplate = host.querySelector('#inner-template');
  assert_true(!!innerTemplate.content.querySelector('div.innerdiv').shadowRoot,"Inner div should have a shadow root");
}, 'Declarative Shadow DOM: template containing a template containing declarative shadow root');
</script>

<template id="template-containing-ua-shadow">
  <div class="innerdiv">
    <template shadowrootmode=open shadowrootclonable>
      <video></video> <!--Assumed to have UA shadow root-->
    </template>
  </div>
</template>
<script>
test(() => {
  const template = document.querySelector('#template-containing-ua-shadow');
  const host = document.createElement('div');
  host.style.display = 'none';
  document.body.appendChild(host);
  // Mostly make sure clone of template *does* clone the
  // shadow root, and doesn't crash on cloning the <video>.
  host.appendChild(template.content.cloneNode(true));
  let innerDiv = host.querySelector('div.innerdiv');
  const shadowRoot = innerDiv.shadowRoot;
  assert_true(!!shadowRoot,"Inner div should have a shadow root");
  assert_equals(innerDiv.querySelector('template'), null, "No leftover template node");
  assert_true(!!innerDiv.shadowRoot.querySelector('video'),'Video element should be present');
}, 'Declarative Shadow DOM: template containing declarative shadow root and UA shadow root');
</script>

<template id="template-containing-ua-shadow-closed">
  <div class="innerdiv">
    <template shadowrootmode=closed>
      <video></video> <!--Assumed to have UA shadow root-->
    </template>
  </div>
</template>
<script>
test(() => {
  const template = document.querySelector('#template-containing-ua-shadow-closed');
  const host = document.createElement('div');
  host.style.display = 'none';
  document.body.appendChild(host);
  host.appendChild(template.content.cloneNode(true));
  let innerDiv = host.querySelector('div.innerdiv');
  assert_true(!innerDiv.shadowRoot,"Inner div should have a closed shadow root");
}, 'Declarative Shadow DOM: template containing closed declarative shadow root and UA shadow root');
</script>

<template id="root-element-shadow">
    <template shadowrootmode=open>Content</template>
</template>
<script>
test(() => {
  // Root element of this template is a <template shadowroot>:
  const template = document.querySelector('#root-element-shadow');
  const host = document.createElement('div');
  host.appendChild(template.content.cloneNode(true));
  assert_equals(host.shadowRoot, null, "Shadow root should not be present");
  const tmpl = host.querySelector('template');
  assert_true(!!tmpl,"Template should still be present");
  assert_equals(tmpl.getAttribute('shadowrootmode'),"open","'shadowrootmode' attribute should still be present");
}, 'Declarative Shadow DOM: template root element');
</script>