Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@movable/optimal-select
Advanced tools
This is a fork of the main library. Please consider using that instead.
A library which creates efficient and robust CSS selectors for HTML elements.
The closest you can get if you're looking for a fingerprint of an HTML element
Aside of the prebundled versions the library is also available via npm:
npm install --save optimal-select
import { select } from 'optimal-select' // global: 'OptimalSelect'
document.addEventListener('click', (e) => {
var selector = select(e.target)
console.log(selector)
})
By default following attributes are ignored for robustness towards changes:
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, {
// default reference
root: document,
skip (traverseNode) {
// ignore select information of the direct parent
return traverseNode === element.parentNode
},
// define order of attribute processing
priority: ['id', 'class', 'href', 'src'],
// define patterns which should't be included
ignore: {
class (className) {
// disregard short classnames
return className.length < 3
},
attribute (name, value, defaultPredicate) {
// exclude HTML5 data attributes
return (/data-*/).test(name) || defaultPredicate(name, value)
},
// define simplified ignore patterns as a boolean/string/number/regex
tag: 'div'
}
})
As shown the root
property allows to define the container element (default: document
).
The skip
value allows to define a function
, a single node
or an array
of nodes which should be ignored as the selector is created (default: null
). With the priority
value can the order of processed attributes be customized. Finally individual filter functions can be defined through ignore
.
getQuerySelector (input, [options]) // alias: 'select'
Convenience function which automatically uses either getSingleSelector
or getMultiSelector
getSingleSelector(element, [options])
Retrieve a unique CSS selector of the element Element is a DOM ode
getMultiSelector(elements, [options])
Retrieve a unique CSS selector of the elements Elements is an array with a list of DOM nodes
optimize(selector, elements, [options])
Improve the CSS selector
getCommonAncestor(elements, [options])
Retrieve the closest ancestor of the elements
getCommonProperties(elements, [options])
Retrieve a set of common properties of the elements
The latest version of optimal-select
allows the generation and optimization of selectors on virtual environments. It uses the basic structure the htmlparser2 DOM provides and adds some utilities to create the same results as the browser (note: the withDOMLv1
option has to be enabled). Other libraries like cheerio are built on top of these and therefore compatible.
In contrast to the browser does server environments not have a global context which defines their scope. Therefore one can either be specified explicit through a node using the context
options field or automatically extracted from the provided input element. Checkout the example for more details.
getMultiSelector
strict
option for optimizations of multiple elements:not
- selector to exclude other elements matching
(for multiple element matching consider the :not selector to exclude exceptions)To build your own version run npm run dev
for development (incl. watch) or npm run build
for production (minified).
FAQs
Get efficient & robust CSS selectors for HTML elements
The npm package @movable/optimal-select receives a total of 3 weekly downloads. As such, @movable/optimal-select popularity was classified as not popular.
We found that @movable/optimal-select demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.