
Research
Security News
Malicious PyPI Package Exploits Deezer API for Coordinated Music Piracy
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
element-closest
Advanced tools
The element-closest npm package is a polyfill for the Element.closest() method, which allows you to find the closest ancestor of an element that matches a given CSS selector. This is particularly useful for ensuring compatibility with older browsers that do not support the native Element.closest() method.
Find closest ancestor
This feature allows you to find the closest ancestor of an element that matches a specified CSS selector. In this example, the closest ancestor of the element with the ID 'childElement' that has the class 'ancestorClass' is returned.
document.querySelector('#childElement').closest('.ancestorClass');
The matches-selector package provides a polyfill for the Element.matches() method, which checks if an element matches a given CSS selector. While it does not provide the same functionality as element-closest, it is often used in conjunction with it to ensure broader compatibility with older browsers.
The dom-closest package is another polyfill for the Element.closest() method. It offers similar functionality to element-closest, allowing you to find the closest ancestor of an element that matches a given CSS selector. It is a direct alternative to element-closest.
Element.prototype.closest returns the first element that matches a CSS selector by traversing up the DOM tree starting from (and including) the receiver element.
element.closest(selectorString) //=> element
This is especially useful for delegating events.
document.addEventListener('click', function (event) {
// find the anchor element that contains or is the click target itself:
var link = event.target.closest('a');
if (link) {
// do something with the anchor
event.preventDefault();
}
});
For the closest
polyfill to work, this also polyfills matches, which is widely supported but often vendor-prefixed.
element.matches(selectorString) //=> boolean
matches
is especially useful for shorthanding hasAttribute
or classList.contains
with selectors.
var widget = document.querySelector('.custom-widget');
if (widget.matches('[data-active]') || widget.matches('.widget--active')) {
// do something with the active widget
}
The legacy version extends support to
Those are really old browsers, and I don’t see a usecase for IE8 compatibility, which is why it is packaged separately. closest
is especially useful when delegating events, and Internet Explorer 8 does not support addEventListener, and attachEvent is an insufficient fallback, as it fires events in the reverse order they are added (read: the opposite order of what you expect and addEventListener
).
FAQs
Return the closest element matching a selector up the DOM tree
We found that element-closest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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 researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.
Security News
Newly introduced telemetry in devenv 1.4 sparked a backlash over privacy concerns, leading to the removal of its AI-powered feature after strong community pushback.