All Downloads are FREE. Search and download functionalities are using the official Maven repository.

js.getElementCssSelector.js Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
function previousElementSibling (element) {
  if (element.previousElementSibling !== 'undefined') {
    return element.previousElementSibling;
  } else {
    // Loop through ignoring anything not an element
    while (element = element.previousSibling) {
      if (element.nodeType === 1) {
        return element;
      }
    }
  }
}
function getCssPath (element) {
  // Empty on non-elements
  if (!(element instanceof HTMLElement)) { return ''; }
  let path = [];
  while (element.nodeType === Node.ELEMENT_NODE) {
    let selector = element.nodeName;
    if (element.id) { selector += ('#' + element.id); }
    else {
      // Walk backwards until there is no previous sibling
      let sibling = element;
      // Will hold nodeName to join for adjacent selection
      let siblingSelectors = [];
      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(' > ');
}
return getCssPath(arguments[0]);




© 2015 - 2024 Weber Informatics LLC | Privacy Policy