shadow-dom.js
6.69 KB
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
"use strict";
const NODE_TYPE = require("../node-type");
const { nodeRoot } = require("./node");
const { HTML_NS } = require("./namespaces");
const { domSymbolTree } = require("./internal-constants");
const { signalSlotList, queueMutationObserverMicrotask } = require("./mutation-observers");
// Valid host element for ShadowRoot.
// Defined in: https://dom.spec.whatwg.org/#dom-element-attachshadow
const VALID_HOST_ELEMENT_NAME = new Set([
"article",
"aside",
"blockquote",
"body",
"div",
"footer",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"main",
"nav",
"p",
"section",
"span"
]);
function isValidHostElementName(name) {
return VALID_HOST_ELEMENT_NAME.has(name);
}
// Use an approximation by checking the presence of nodeType instead of instead of using the isImpl from
// "../generated/Node" to avoid introduction of circular dependencies.
function isNode(nodeImpl) {
return Boolean(nodeImpl && "nodeType" in nodeImpl);
}
// Use an approximation by checking the value of nodeType and presence of nodeType host instead of instead
// of using the isImpl from "../generated/ShadowRoot" to avoid introduction of circular dependencies.
function isShadowRoot(nodeImpl) {
return Boolean(nodeImpl && nodeImpl.nodeType === NODE_TYPE.DOCUMENT_FRAGMENT_NODE && "host" in nodeImpl);
}
// https://dom.spec.whatwg.org/#concept-slotable
function isSlotable(nodeImpl) {
return nodeImpl && (nodeImpl.nodeType === NODE_TYPE.ELEMENT_NODE || nodeImpl.nodeType === NODE_TYPE.TEXT_NODE);
}
function isSlot(nodeImpl) {
return nodeImpl && nodeImpl.localName === "slot" && nodeImpl._namespaceURI === HTML_NS;
}
// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-ancestor
function isShadowInclusiveAncestor(ancestor, node) {
while (isNode(node)) {
if (node === ancestor) {
return true;
}
if (isShadowRoot(node)) {
node = node.host;
} else {
node = domSymbolTree.parent(node);
}
}
return false;
}
// https://dom.spec.whatwg.org/#retarget
function retarget(a, b) {
while (true) {
if (!isNode(a)) {
return a;
}
const aRoot = nodeRoot(a);
if (
!isShadowRoot(aRoot) ||
(isNode(b) && isShadowInclusiveAncestor(aRoot, b))
) {
return a;
}
a = nodeRoot(a).host;
}
}
// https://dom.spec.whatwg.org/#get-the-parent
function getEventTargetParent(eventTarget, event) {
// _getTheParent will be missing for Window, since it doesn't have an impl class and we don't want to pollute the
// user-visible global scope with a _getTheParent value. TODO: remove this entire function and use _getTheParent
// directly, once Window gets split into impl/wrapper.
return eventTarget._getTheParent ? eventTarget._getTheParent(event) : null;
}
// https://dom.spec.whatwg.org/#concept-shadow-including-root
function shadowIncludingRoot(node) {
const root = nodeRoot(node);
return isShadowRoot(root) ? shadowIncludingRoot(root.host) : root;
}
// https://dom.spec.whatwg.org/#assign-a-slot
function assignSlot(slotable) {
const slot = findSlot(slotable);
if (slot) {
assignSlotable(slot);
}
}
// https://dom.spec.whatwg.org/#assign-slotables
function assignSlotable(slot) {
const slotables = findSlotable(slot);
let shouldFireSlotChange = false;
if (slotables.length !== slot._assignedNodes.length) {
shouldFireSlotChange = true;
} else {
for (let i = 0; i < slotables.length; i++) {
if (slotables[i] !== slot._assignedNodes[i]) {
shouldFireSlotChange = true;
break;
}
}
}
if (shouldFireSlotChange) {
signalSlotChange(slot);
}
slot._assignedNodes = slotables;
for (const slotable of slotables) {
slotable._assignedSlot = slot;
}
}
// https://dom.spec.whatwg.org/#assign-slotables-for-a-tree
function assignSlotableForTree(root) {
for (const slot of domSymbolTree.treeIterator(root)) {
if (isSlot(slot)) {
assignSlotable(slot);
}
}
}
// https://dom.spec.whatwg.org/#find-slotables
function findSlotable(slot) {
const result = [];
const root = nodeRoot(slot);
if (!isShadowRoot(root)) {
return result;
}
for (const slotable of domSymbolTree.treeIterator(root.host)) {
const foundSlot = findSlot(slotable);
if (foundSlot === slot) {
result.push(slotable);
}
}
return result;
}
// https://dom.spec.whatwg.org/#find-flattened-slotables
function findFlattenedSlotables(slot) {
const result = [];
const root = nodeRoot(slot);
if (!isShadowRoot(root)) {
return result;
}
const slotables = findSlotable(slot);
if (slotables.length === 0) {
for (const child of domSymbolTree.childrenIterator(slot)) {
if (isSlotable(child)) {
slotables.push(child);
}
}
}
for (const node of slotables) {
if (isSlot(node) && isShadowRoot(nodeRoot(node))) {
const temporaryResult = findFlattenedSlotables(node);
result.push(...temporaryResult);
} else {
result.push(node);
}
}
return result;
}
// https://dom.spec.whatwg.org/#find-a-slot
function findSlot(slotable, openFlag) {
const { parentNode: parent } = slotable;
if (!parent) {
return null;
}
const shadow = parent._shadowRoot;
if (!shadow || (openFlag && shadow.mode !== "open")) {
return null;
}
for (const child of domSymbolTree.treeIterator(shadow)) {
if (isSlot(child) && child.name === slotable._slotableName) {
return child;
}
}
return null;
}
// https://dom.spec.whatwg.org/#signal-a-slot-change
function signalSlotChange(slot) {
if (!signalSlotList.some(entry => entry === slot)) {
signalSlotList.push(slot);
}
queueMutationObserverMicrotask();
}
// https://dom.spec.whatwg.org/#concept-shadow-including-descendant
function* shadowIncludingInclusiveDescendantsIterator(node) {
yield node;
if (node._shadowRoot) {
yield* shadowIncludingInclusiveDescendantsIterator(node._shadowRoot);
}
for (const child of domSymbolTree.childrenIterator(node)) {
yield* shadowIncludingInclusiveDescendantsIterator(child);
}
}
// https://dom.spec.whatwg.org/#concept-shadow-including-descendant
function* shadowIncludingDescendantsIterator(node) {
if (node._shadowRoot) {
yield* shadowIncludingInclusiveDescendantsIterator(node._shadowRoot);
}
for (const child of domSymbolTree.childrenIterator(node)) {
yield* shadowIncludingInclusiveDescendantsIterator(child);
}
}
module.exports = {
isValidHostElementName,
isNode,
isSlotable,
isSlot,
isShadowRoot,
isShadowInclusiveAncestor,
retarget,
getEventTargetParent,
shadowIncludingRoot,
assignSlot,
assignSlotable,
assignSlotableForTree,
findSlot,
findFlattenedSlotables,
signalSlotChange,
shadowIncludingInclusiveDescendantsIterator,
shadowIncludingDescendantsIterator
};