Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 1x 5x 5x 2x 1x 1x 1x 5x 5x 2x 3x 3x 3x 5x 5x 3x 2x 3x 5x 5x 1x 1x | const previousPolyfill = require('./previous-sibling-polyfill')
/**
* Gets the element node that is a sibling to this element node (a direct child of the same parent) and is immediately
* previous to it in the DOM tree. It's a fix for IE that does not support :nth-child pseudoselector
* @param {HTMLElement} element - DOM node
* @return {string} Unique CSS selector for the given DOM node
*/
function previousElementSibling(element)
{
Eif(element.previousElementSibling !== 'undefined')
return element.previousElementSibling
else
return previousPolyfill(element)
}
/**
* Get a unique CSS selector for a given DOM node
* @param {HTMLElement} element - DOM node
* @return {string} Unique CSS selector for the given DOM node
*/
function getPath(element)
{
// False on non-elements
if(!(element instanceof HTMLElement))
return false
const path = []
while(element !== null && element.nodeType === Node.ELEMENT_NODE) // If element is null it's the end of partial. It's a loose element which has, sofar, not been attached to a parent in the node tree.
{
let selector = element.nodeName
if(element.id)
{
selector += `#${element.id}`
}
else
{
let
sibling = element, // Walk backwards until there is no previous sibling
siblingSelectors = [] // Will hold nodeName to join for adjacent selection
while(sibling !== null && sibling.nodeType === Node.ELEMENT_NODE)
{
siblingSelectors.unshift(sibling.nodeName)
sibling = previousElementSibling(sibling)
}
// :first-child does not apply to HTML
if(siblingSelectors[0] !== 'HTML')
siblingSelectors[0] = siblingSelectors[0] + ':first-child'
selector = siblingSelectors.join(' + ')
}
path.unshift(selector)
element = element.parentNode
}
return path.join(' > ')
}
module.exports = getPath
|