Install @medv/finder via npm
masterInstall the package using npm to use the CSS selector generator in your project.
npm install @medv/finderrepository·master·Indexed 23 days ago
https://github.com/antonmedv/finderA lightweight (1.5kb) CSS selector generator that produces the shortest, unique, and most robust CSS selectors for given DOM elements. It includes a configurable finder() function with options to customize root boundaries, timeouts, and validation logic for IDs, class names, tag names, and attributes.
Install the package using npm to use the CSS selector generator in your project.
npm install @medv/finderImport finder from @medv/finder and pass an element (e.g., event.target) to it. It returns a string representing the shortest, unique, and stable CSS selector for that element.
import { finder } from '@medv/finder';
document.addEventListener('click', (event) => {
const selector = finder(event.target);
});By default, finder uses word-like class names. You can extend this behavior by providing a custom className function. It is recommended to wrap the imported className utility to maintain default behavior for standard names while adding your own logic.
import { finder, className } from '@medv/finder';
finder(event.target, {
className: name => className(name) || name.startsWith('my-class-'),
});By default, finder uses word-like attribute names and values. You can extend this by providing a custom attr function. Wrap the imported attr utility to combine default logic with custom rules (e.g., allowing specific data- attributes).
import { finder, attr } from '@medv/finder';
finder(event.target, {
attr: (name, value) => attr(name, value) || name.startsWith('data-my-attr-'),
});The finder function accepts an optional configuration object as its second argument to customize how selectors are generated. Common options include root for the search boundary and timeoutMs to control the search duration before falling back to nth-child selectors.
const selector = finder(event.target, {
root: document.body,
timeoutMs: 1000,
});You can pass an options object to finder() to customize how selectors are generated. The following options are available:
| Option | Type | Default | Description |
|---|---|---|---|
root | Node | document.body | The root node used to check for selector uniqueness. |
idName | Function | idName | Function to validate if an ID is suitable for a selector. |
className | Function | className | Function to validate if a class name is suitable. |
tagName | Function | tagName | Function to validate if a tag name is suitable. |
attr | Function | attr | Function to validate if an attribute name/value pair is suitable. |
timeoutMs | number | 1000 | Maximum time in milliseconds to spend searching for a selector. |
seedMinLength | number | 3 | Minimum path length before starting to yield candidates. |
optimizedMinLength | number | 2 | Minimum path length required for optimization attempts. |
maxNumberOfPathChecks | number | Infinity | Maximum number of unique path candidates to check. |
finder:The attr(name, value) function determines if an attribute name and value are suitable for use in a CSS selector.
By default, it accepts:
role, name, aria-label, rel, href.data-* attributes if the name is "word-like".# (e.g., for fragment identifiers) if the remainder is "word-like".className(name) function checks if a class name string is "word-like" and thus suitable for use in a selector.finder() to decide which attributes, classes, IDs, or tags areidName(name) function checks if an ID string is "word-like" and thus suitable for use in a selector.tagName(name) function determines if a tag name is suitable for use in a selector. The default implementation always returns true.