forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshadow-select-all.js
More file actions
31 lines (30 loc) · 997 Bytes
/
shadow-select-all.js
File metadata and controls
31 lines (30 loc) · 997 Bytes
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
/**
* Find elements to match a selector.
* Use an array of selectors to reach into shadow DOM trees
*
* @param {string|string[]} selector String or array of strings with a CSS selector
* @param {Document} doc Optional document node
* @returns {Node[]}
*/
export default function shadowSelectAll(selectors, doc = document) {
// Spread to avoid mutating the input
const selectorArr = Array.isArray(selectors) ? [...selectors] : [selectors];
if (selectors.length === 0) {
return [];
}
return selectAllRecursive(selectorArr, doc);
}
/* Find elements in shadow or light DOM trees, using an array of selectors */
function selectAllRecursive([selectorStr, ...restSelector], doc) {
const elms = doc.querySelectorAll(selectorStr);
if (restSelector.length === 0) {
return Array.from(elms);
}
const selected = [];
for (const elm of elms) {
if (elm?.shadowRoot) {
selected.push(...selectAllRecursive(restSelector, elm.shadowRoot));
}
}
return selected;
}