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
|
<!DOCTYPE html>
<title>Shadow DOM: Imperative Slot API</title>
<meta name="author" title="Yu Han" href="mailto:yuzhehan@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/shadow-dom.js"></script>
<div id="test_basic">
<div id="host1"></div>
<div id="host2"></div>
<div id="host3"></div>
</div>
<script>
test(() => {
let tTree = createTestTree(test_basic);
const shadow1 = tTree.host1.attachShadow({ mode: 'open', slotAssignment: 'manual'});
assert_not_equals(shadow1, null, 'slot assignment manual should work');
assert_equals(shadow1.slotAssignment, "manual", 'slotAssignment should return "manual"');
const shadow2 = tTree.host2.attachShadow({ mode: 'open', slotAssignment: 'named'});
assert_not_equals(shadow2, null, 'slot assignment named should work');
assert_equals(shadow2.slotAssignment, "named", 'slotAssignment should return "named"');
assert_throws_js(TypeError, () => {
tTree.host3.attachShadow({ mode: 'open', slotAssignment: 'exceptional' })},
'others should throw exception');
}, 'attachShadow can take slotAssignment parameter.');
</script>
<div id="test_assign">
<div id="host">
<template id="shadow_root" data-mode="open" data-slot-assignment="manual">
<slot id="s1"></slot>
<slot id="s2"></slot>
<slot id="s3"></slot>
</template>
<div id="c1"></div>
<div id="c2"></div>
<div id="c3"></div>
<div id="nested">
<div id="ns1"></div>
</div>
</div>
<div id="c4"></div>
<div id="host4">
<template id="shadow_root4" data-mode="open" data-slot-assignment="manual">
<slot id="s4" name="s4"></slot>
</template>
</div>
</div>
<script>
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(c1,c2); // Should work
assert_throws_js(TypeError, () => {
tTree.s1.assign([c1,c2])
}, 'sequence not allowed');
assert_throws_js(TypeError, () => {
tTree.s1.assign([])
}, 'including empty sequences');
}, 'slot.attach() should take variadic not sequence.');
test(() => {
let tTree = createTestTree(test_assign);
assert_array_equals(tTree.s2.assignedElements(), []);
assert_equals(tTree.c1.assignedSlot, null);
tTree.s1.assign(tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
tTree.s2.assign(tTree.c2, tTree.c3);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1]);
assert_array_equals(tTree.s2.assignedNodes(), [tTree.c2, tTree.c3]);
}, 'Imperative slot API can assign nodes in manual slot assignment.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c2, tTree.c3, tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c2, tTree.c3, tTree.c1]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s1);
assert_equals(tTree.c3.assignedSlot, tTree.s1);
tTree.s1.assign(tTree.c1, tTree.c2);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s1);
assert_equals(tTree.c3.assignedSlot, null);
tTree.s1.assign(tTree.c3, tTree.c2, tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c3, tTree.c2, tTree.c1]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s1);
assert_equals(tTree.c3.assignedSlot, tTree.s1);
}, 'Order of slottables is preserved in manual slot assignment.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c2, tTree.c3, tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c2, tTree.c3, tTree.c1]);
tTree.s2.assign(tTree.c2);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c3, tTree.c1]);
assert_array_equals(tTree.s2.assignedNodes(), [tTree.c2]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s2);
assert_equals(tTree.c3.assignedSlot, tTree.s1);
tTree.s3.assign(tTree.c3);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1]);
assert_array_equals(tTree.s2.assignedNodes(), [tTree.c2]);
assert_array_equals(tTree.s3.assignedNodes(), [tTree.c3]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s2);
assert_equals(tTree.c3.assignedSlot, tTree.s3);
}, 'Previously assigned slottable is moved to new slot when it\'s reassigned.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c1);
tTree.s2.assign(tTree.c2, tTree.c1);
tTree.s3.assign(tTree.c1, tTree.c3);
assert_array_equals(tTree.s1.assignedNodes(), []);
assert_array_equals(tTree.s2.assignedNodes(), [tTree.c2]);
assert_array_equals(tTree.s3.assignedNodes(), [tTree.c1, tTree.c3]);
assert_equals(tTree.c1.assignedSlot, tTree.s3);
assert_equals(tTree.c2.assignedSlot, tTree.s2);
assert_equals(tTree.c3.assignedSlot, tTree.s3);
}, 'Order and assignment of nodes are preserved during multiple assignment in a row.');
test(() => {
let tTree = createTestTree(test_assign);
// tTree.c4 is invalid for tTree.host slot assignment.
// No exception should be thrown here.
tTree.s1.assign(tTree.c1, tTree.c4, tTree.c2);
// All observable assignments should skip c4.
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s1);
assert_equals(tTree.c4.assignedSlot, null);
// Moving c4 into place should reveal the assignment.
tTree.host.append(tTree.c4);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c4, tTree.c2]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s1);
assert_equals(tTree.c4.assignedSlot, tTree.s1);
// Moving c4 into a different shadow host and back should
// also not break the assignment.
tTree.host4.append(tTree.c4)
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2]);
assert_equals(tTree.c4.assignedSlot, null);
tTree.host.append(tTree.c4);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c4, tTree.c2]);
assert_equals(tTree.c4.assignedSlot, tTree.s1);
}, 'Assigning invalid nodes should be allowed.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c1, tTree.c2, tTree.c3);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2, tTree.c3]);
tTree.host4.append(tTree.s1);
assert_array_equals(tTree.s1.assignedNodes(), []);
}, 'Moving a slot to a new host, the slot loses its previously assigned slottables.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c1, tTree.c2, tTree.c3);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2, tTree.c3]);
tTree.shadow_root.insertBefore(tTree.s2, tTree.s1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2, tTree.c3]);
assert_array_equals(tTree.s2.assignedNodes(), []);
tTree.shadow_root.insertBefore(tTree.s4, tTree.s1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2, tTree.c3]);
assert_array_equals(tTree.s4.assignedNodes(), []);
tTree.ns1.append(tTree.s1);
assert_array_equals(tTree.s1.assignedNodes(), []);
}, 'Moving a slot\'s tree order position within a shadow host has no impact on its assigned slottables.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c1, tTree.c2, tTree.c3);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2, tTree.c3]);
tTree.host4.append(tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c2, tTree.c3]);
assert_array_equals(tTree.s4.assignedNodes(), []);
assert_equals(tTree.c1.assignedSlot, null);
tTree.s4.assign(tTree.c1);
assert_array_equals(tTree.s4.assignedNodes(), [tTree.c1]);
assert_equals(tTree.c1.assignedSlot, tTree.s4);
}, 'Appending slottable to different host, it loses slot assignment. It can be re-assigned within a new host.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1]);
tTree.shadow_root4.insertBefore(tTree.s1, tTree.s4);
assert_array_equals(tTree.s1.assignedNodes(), []);
// Trigger slot assignment on previous shadow root.
assert_array_equals(tTree.s2.assignedNodes(), []);
tTree.shadow_root.insertBefore(tTree.s1, tTree.s2);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1]);
}, 'Previously assigned node should not be assigned if slot moved to a new shadow root. The node is re-assigned when moved back.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c1, tTree.c1, tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1]);
tTree.s1.assign(tTree.c1, tTree.c1, tTree.c2, tTree.c2, tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2]);
}, 'Assignment with the same node in parameters should be ignored, first one wins.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c1, tTree.c2, tTree.c3);
tTree.s1.remove();
assert_equals(tTree.c1.assignedSlot, null);
assert_equals(tTree.c2.assignedSlot, null);
assert_equals(tTree.c3.assignedSlot, null);
}, 'Removing a slot from DOM resets its slottable\'s slot assignment.');
test(() => {
let tTree = createTestTree(test_assign);
const isolatedDocNode = document.implementation.createHTMLDocument("").body;
isolatedDocNode.appendChild(tTree.c1);
const isolatedDocNode2 = document.implementation.createHTMLDocument("").body;
isolatedDocNode2.appendChild(tTree.s1);
tTree.s1.assign(tTree.c1, tTree.c2);
assert_array_equals(tTree.s1.assignedNodes(), [], 's1 not inside shadow root');
assert_equals(tTree.c1.assignedSlot, null);
assert_equals(tTree.c2.assignedSlot, null);
tTree.shadow_root.appendChild(tTree.s1);
tTree.host.appendChild(tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s1);
}, 'Nodes can be assigned even if slots or nodes aren\'t in the same tree.');
test(() => {
let tTree = createTestTree(test_assign);
tTree.s1.assign(tTree.c1, tTree.c2);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s1);
const isolatedDocNode = document.implementation.createHTMLDocument("").body;
isolatedDocNode.appendChild(tTree.c1);
const isolatedDocNode2 = document.implementation.createHTMLDocument("").body;
isolatedDocNode2.appendChild(tTree.s1);
assert_array_equals(tTree.s1.assignedNodes(), [], 's1 not inside shadow root');
assert_equals(tTree.c1.assignedSlot, null);
assert_equals(tTree.c2.assignedSlot, null);
tTree.shadow_root.appendChild(tTree.s1);
tTree.host.appendChild(tTree.c1);
assert_array_equals(tTree.s1.assignedNodes(), [tTree.c1, tTree.c2]);
assert_equals(tTree.c1.assignedSlot, tTree.s1);
assert_equals(tTree.c2.assignedSlot, tTree.s1);
}, 'Removing a node from the document does not break manually assigned slot linkage.');
test(() => {
const inputValues = [
['Attr', document.createAttribute('bar')],
['Comment', document.createComment('bar')],
['DocumentFragment', document.createDocumentFragment()],
['DocumentType', document.implementation.createDocumentType('html', '', '')]
];
for (const [label, input] of inputValues) {
assert_throws_js(TypeError, () => {
const slot = document.createElement('slot');
slot.assign(input);
}, label);
}
}, 'throw TypeError if the passed values are neither Element nor Text');
</script>
|