Created
October 19, 2022 05:52
-
-
Save shobhit/38115185b2868e1061407a6a2a14ce93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getCssSelectorShort = (el) => { | |
let path = [], parent; | |
while (parent = el.parentNode) { | |
let tag = el.tagName, siblings; | |
path.unshift( | |
el.id ? `#${el.id}` : ( | |
siblings = parent.children, | |
[].filter.call(siblings, sibling => sibling.tagName === tag).length === 1 ? tag : | |
`${tag}:nth-child(${1+[].indexOf.call(siblings, el)})` | |
) | |
); | |
el = parent; | |
}; | |
return `${path.join(' > ')}`.toLowerCase(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment