-
Notifications
You must be signed in to change notification settings - Fork 25
/
dom.js
252 lines (199 loc) · 6.51 KB
/
dom.js
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
// needed for legacy-Edge, after it's removed use CSS.escape directly
export function cssEscape(val) {
if (window.CSS && window.CSS.escape) {
return window.CSS.escape(val);
}
val = val.replace(/\$/g, '\\$');
return val;
}
export function elemIdListAdd(elem, attrName, value) {
if (elem === undefined || elem === null || !elem.getAttribute || !elem.setAttribute) {
throw new TypeError('elemIdListAdd: "elem" must be a valid DOM Element');
}
if (typeof(attrName) !== 'string') {
throw new TypeError('elemIdListAdd: "attrName" must be a valid string');
}
if (typeof(value) !== 'string') {
throw new TypeError('elemIdListAdd: "value" must be a valid ID string');
}
const parts = elem.hasAttribute(attrName) ? elem.getAttribute(attrName).split(' ') : [];
if (parts.indexOf(value) > -1) return;
parts.push(value);
elem.setAttribute(attrName, parts.join(' '));
}
export function elemIdListRemove(elem, attrName, value) {
if (elem === undefined || elem === null || !elem.getAttribute || !elem.setAttribute) {
throw new TypeError('elemIdListRemove: "elem" must be a valid DOM Element');
}
if (typeof(attrName) !== 'string') {
throw new TypeError('elemIdListRemove: "attrName" must be a valid string');
}
if (typeof(value) !== 'string') {
throw new TypeError('elemIdListRemove: "value" must be a valid ID string');
}
const existingValue = elem.getAttribute(attrName) || '';
const parts = existingValue.split(' ');
const index = parts.indexOf(value);
if (index === -1) return;
if (parts.length === 1) {
elem.removeAttribute(attrName);
} else {
parts.splice(index, 1);
elem.setAttribute(attrName, parts.join(' '));
}
}
export function findComposedAncestor(node, predicate) {
while (node) {
if (predicate(node) === true) {
return node;
}
node = getComposedParent(node);
}
return null;
}
export function getBoundingAncestor(node) {
return findComposedAncestor(node, (node) => {
if (node === document.body) return false;
// explicitly ignore slot element, required for Edge
if (node.tagName === 'SLOT') return false;
if (node === document.documentElement) return true;
if (node.nodeType === Node.ELEMENT_NODE) {
const overflow = window.getComputedStyle(node, null).getPropertyValue('overflow');
// treat auto, scroll, hidden, clip as bounding
return (overflow !== 'visible');
}
return false;
});
}
export function getComposedChildren(node) {
if (!node) {
return null;
}
if (node.nodeType !== 1 && node.nodeType !== 9 && node.nodeType !== 11) {
return null;
}
let nodes;
const children = [];
if (node.tagName === 'CONTENT') {
nodes = node.getDistributedNodes();
} else if (node.tagName === 'SLOT') {
nodes = node.assignedNodes({ flatten: true });
} else {
if (node.shadowRoot) {
node = node.shadowRoot;
}
nodes = node.children || node.childNodes;
}
for (let i = 0; i < nodes.length; i++) {
if (nodes[i].nodeType === 1) {
children.push(nodes[i]);
}
}
return children;
}
export function getComposedParent(node) {
if (node.getDestinationInsertionPoints) {
const insertionPoints = node.getDestinationInsertionPoints();
if (insertionPoints && insertionPoints.length > 0) {
return insertionPoints[0];
}
}
if (node.assignedSlot) {
return node.assignedSlot;
}
if (node.parentNode) {
return node.parentNode;
} else if (node.host) {
return node.host;
}
return null;
}
export function getNextAncestorSibling(node, predicate = () => true) {
let parentNode = getComposedParent(node);
while (parentNode) {
const nextParentSibling = parentNode.nextElementSibling;
if (nextParentSibling && predicate(nextParentSibling)) return nextParentSibling;
parentNode = getComposedParent(parentNode);
}
return null;
}
export function getPreviousAncestorSibling(node, predicate = () => true) {
let parentNode = getComposedParent(node);
while (parentNode) {
const previousParentSibling = parentNode.previousElementSibling;
if (previousParentSibling && predicate(previousParentSibling)) return previousParentSibling;
parentNode = getComposedParent(parentNode);
}
return null;
}
export function getOffsetParent(node) {
if (!window.ShadowRoot) {
return node.offsetParent;
}
if (
!getComposedParent(node) ||
node.tagName === 'BODY' ||
window.getComputedStyle(node).position === 'fixed'
) {
return null;
}
let firstTableElement = null;
let currentNode = getComposedParent(node);
while (currentNode) {
if (currentNode instanceof ShadowRoot) {
currentNode = getComposedParent(currentNode);
} else if (currentNode instanceof DocumentFragment) {
return firstTableElement;
} else if (currentNode.tagName === 'BODY') {
return firstTableElement || currentNode;
}
const position = window.getComputedStyle(currentNode).position;
const tagName = currentNode.tagName;
if (position && position !== 'static') {
return currentNode;
} else if (firstTableElement === null && position === 'static' && (tagName === 'TD' || tagName === 'TH' || tagName === 'TABLE')) {
firstTableElement = currentNode;
}
currentNode = getComposedParent(currentNode);
}
return firstTableElement;
}
export function isComposedAncestor(ancestorNode, node) {
return findComposedAncestor(node, (node) => {
return (node === ancestorNode);
}) !== null;
}
export function isVisible(node) {
/* this helper is different from checking offsetParent because offsetParent
returns null for fixed position elements regardless of visibility */
if (!node) return false;
if (!node.host) {
if (node.style === undefined) return true;
if (node.style.display === 'none') return false;
if (node.style.visibility === 'hidden') return false;
const computedStyle = window.getComputedStyle(node, null);
if (computedStyle.getPropertyValue('display') === 'none') return false;
if (computedStyle.getPropertyValue('visibility') === 'hidden') return false;
}
const parentNode = getComposedParent(node);
if (parentNode) return isVisible(parentNode);
return true;
}
export function querySelectorComposed(node, selector) {
if (!node || (node.nodeType !== 1 && node.nodeType !== 9 && node.nodeType !== 11)) {
throw new TypeError('Invalid node. Must be nodeType document, element or document fragment');
}
if (typeof selector !== 'string') {
throw new TypeError('Invalid selector');
}
const elem = node.querySelector(selector);
if (elem) return elem;
const allElems = node.querySelectorAll('*');
for (const elem of allElems) {
if (elem.shadowRoot) {
const nestedElem = querySelectorComposed(elem.shadowRoot, selector);
if (nestedElem) return nestedElem;
}
}
return null;
}