GithubHelp home page GithubHelp logo

get-selector's Introduction

100% test coverage

get-selector

Generates a unique CSS selector that will match only the passed element.

Browser-based utility to generate a unique selector for a given element. There are more comprehensive (and therefore often slower) solutions out there that will retrieve the smallest selector possible, but that comes at the price of complexity and usually accuracy.

This one prioritizes speed and accuracy. If you have an invalid DOM, results will vary.


How it works

Check if the element has an id attribute

  • If it does, return it and we're done.
  • If the element does not have an id...

Traverse the element's ancestry

  • It searches for its closest ancestor with an id attribute.
  • If found, it builds the selector to that ancestor and no further.
  • If no ancestor with an id is found...

Build the selector to <html>

  • This ensures a unique selector composed of nth-child sub-selectors.

Installation

Install via npm.

$ npm i get-selector --save

Usage

Require and call when needed. The function will return false if the passed element is not valid. Otherwise it will return the selector as a string.

var getSelector = require('get-selector');

console.log(getSelector(someElement));
// Outputs something like:
// 	#list-item-two > a:nth-child(1)

Documentation

Build jsdoc-based documentation:

$ npm run docs

Tests and coverage

Run tests and coverage:

$ npm test
$ npm run coverage

Sample output

All output below assumes the following markup:

<div class="demo">
  <ul>
    <li></li>
    <li></li>
    <li>
      <a href="linkOne" class="linkOne"></a>
      <a href="linkTwo" class="linkTwo"></a>
      <a href="linkThree" class="linkThree"></a>
    </li>
  </ul>
  <ul>
    <li class="itemOne first">
      <a href="linkOne" class="linkOne"></a>
      <a href="linkTwo" class="linkTwo"></a>
      <a href="linkThree" class="linkThree"></a>
    </li>
    <li class="itemTwo" id="list-item-two">
      <a href="linkOne" class="list-item-two-link-one"></a>
      <a href="linkTwo"></a>
      <a href="linkThree"></a>
      <a></a>
      <a href="linkOne" class="classOne classTwo classThree"></a>
      <a href="linkTwo" target="someTarget2" rel="someRel" class="classOne classTwo classThree"></a>
      <a href="linkThree" target="someTarget" rel="someRel" class="classOne classTwo classThree" id="linkZero"></a>
    </li>
    <li class="itemThree last">
      <a href="linkOne" id="linkOne" class="classOne classTwo classThree"></a>
      <a href="linkTwo" id="linkTwo"></a>
      <a href="linkThree" id="linkThree"></a>
    </li>
  </ul>
</div>

Basic usage:

getSelector(document.querySelector('.demo li.last'))
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(2) > li:nth-child(3)

Invalid element:

getSelector(document.querySelector('#i-do-not-exist'))
false

Closest descendant with an ID attribute:

getSelector(document.querySelector('.demo .list-item-two-link-one'))
#list-item-two > a:nth-child(1)

Build selector from document Element if no ancestors have an id:

getSelector(document.querySelector('.demo .linkThree'))
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(1) > li:nth-child(3) > a:nth-child(3)

Build a unique selector for any DOM element:

.demo *
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(1)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(1) > li:nth-child(1)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(1) > li:nth-child(2)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(1) > li:nth-child(3)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(1) > li:nth-child(3) > a:nth-child(1)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(1) > li:nth-child(3) > a:nth-child(2)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(1) > li:nth-child(3) > a:nth-child(3)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(2)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(2) > li:nth-child(1)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(1)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(2)
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(3)
#list-item-two
#list-item-two > a:nth-child(1)
#list-item-two > a:nth-child(2)
#list-item-two > a:nth-child(3)
#list-item-two > a:nth-child(4)
#list-item-two > a:nth-child(5)
#list-item-two > a:nth-child(6)
#linkZero
html > body > main:nth-child(3) > div:nth-child(4) > ul:nth-child(2) > li:nth-child(3)
#linkOne
#linkTwo
#linkThree

get-selector's People

Contributors

tylerjpeterson avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

psalaets

get-selector's Issues

Ancestor with mixed-type siblings produces wrong selector

With this html

<div>
  <h2>header</h2>
  <div>
    <span></span>
  </div>
</div>

the selector generated for the span is
html > body > div:nth-child(1) > h2:nth-child(2) > span:nth-child(1)
but should be
html > body > div:nth-child(1) > div:nth-child(2) > span:nth-child(1)

The top level div having two different element types is tripping it up.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.