forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-shadow-selector.js
More file actions
29 lines (27 loc) · 800 Bytes
/
Copy pathget-shadow-selector.js
File metadata and controls
29 lines (27 loc) · 800 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
/**
* Gets a unique CSS selector
* @param {HTMLElement} node The element to get the selector for
* @param {Object} optional options
* @returns {String|Array<String>} Unique CSS selector for the node
*/
export default function getShadowSelector(generateSelector, elm, options = {}) {
if (!elm) {
return '';
}
let doc = (elm.getRootNode && elm.getRootNode()) || document;
// Not a DOCUMENT_FRAGMENT - shadow DOM
if (doc.nodeType !== 11) {
return generateSelector(elm, options, doc);
}
const stack = [];
while (doc.nodeType === 11) {
if (!doc.host) {
return '';
}
stack.unshift({ elm, doc });
elm = doc.host;
doc = elm.getRootNode();
}
stack.unshift({ elm, doc });
return stack.map(item => generateSelector(item.elm, options, item.doc));
}