UNPKG

unique-selector

Version:

Given a DOM node, return a unique CSS selector matching only that element

34 lines (31 loc) 678 B
import { isElement } from './isElement'; /** * Returns the selectors based on the position of the element relative to its siblings * @param { Object } element * @return { Array } */ export function getNthChild( element ) { let counter = 0; let k; let sibling; const { parentNode } = element; if( Boolean( parentNode ) ) { const { childNodes } = parentNode; const len = childNodes.length; for ( k = 0; k < len; k++ ) { sibling = childNodes[ k ]; if( isElement( sibling ) ) { counter++; if( sibling === element ) { return `:nth-child(${counter})`; } } } } return null; }