forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshadow-select.js
More file actions
26 lines (25 loc) · 877 Bytes
/
shadow-select.js
File metadata and controls
26 lines (25 loc) · 877 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
/**
* Find the first element 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 {Element|Null}
*/
export default function shadowSelect(selectors) {
// Spread to avoid mutating the input
const selectorArr = Array.isArray(selectors) ? [...selectors] : [selectors];
return selectRecursive(selectorArr, document);
}
/* Find an element in shadow or light DOM trees, using an axe selector */
function selectRecursive(selectors, doc) {
const selectorStr = selectors.shift();
const elm = selectorStr ? doc.querySelector(selectorStr) : null;
if (selectors.length === 0) {
return elm;
}
if (!elm?.shadowRoot) {
return null;
}
return selectRecursive(selectors, elm.shadowRoot);
}