optimal select
A library which creates efficient and robust CSS selectors for HTML elements.
Features
- support UMD (Browser + Node)
- allow single and multiple element inputs
- configurations allow to define custom ignore patterns
- micro library (~ 5.5kb + no external dependency)
- shortest path and fastest selection in comparison
How To Use
import { select } from 'optimal-select'
document.addEventListener('click', (e) => {
var selector = select(e.target)
console.log(selector)
})
By default following attributes are ignored for robustness towards changes:
- style (inline styles often temporary and used for dynamic visualizations)
- data-reactid (reacts element identifier which depends on the current DOM structure)
- data-react-checksum (react string rendered markup which depends on the current DOM structure)
To define custom filters you can pass the 'ignore' property as a secondary optional parameter.
You can then specify a validation function for the different types (id
, class
, attribute
, tag
).
var selector = select(element, {
ignore: {
class (className) {
return className.length < 3
},
attribute (name, value, defaultPredicate) {
return (/data-*/).test(name) || defaultPredicate(name, value)
},
tag: 'div'
}
})
TODO
Development
To build your own version run npm run dev
for development (incl. watch) or npm run build
for production (minified).