forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-ancestry.js
More file actions
34 lines (29 loc) · 989 Bytes
/
Copy pathget-ancestry.js
File metadata and controls
34 lines (29 loc) · 989 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
32
33
34
import escapeSelector from './escape-selector';
import getShadowSelector from './get-shadow-selector';
function generateAncestry(node) {
const nodeName = escapeSelector(node.nodeName.toLowerCase());
const parentElement = node.parentElement;
const parentNode = node.parentNode;
let nthChild = '';
if (
nodeName !== 'head' &&
nodeName !== 'body' &&
parentNode?.children.length > 1
) {
const index = Array.prototype.indexOf.call(parentNode.children, node) + 1;
nthChild = `:nth-child(${index})`;
}
if (!parentElement) {
return nodeName + nthChild;
}
return generateAncestry(parentElement) + ' > ' + nodeName + nthChild;
}
/**
* 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 getAncestry(elm, options) {
return getShadowSelector(generateAncestry, elm, options);
}