
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
context-selector
Advanced tools
Find elements relative other by CSS selector.
npm install context-selector
<div class="accord">
<div class="accord__item">
<button class="accord__btn" type="button">Expand</button>
<div class="accord__content">item 1</div>
</div>
<div class="accord__item">
<button class="accord__btn" type="button">Expand</button>
<div class="accord__content">item 2</div>
</div>
</div>
import ContextSelector from 'context-selector';
document.addEventListener('click', function(e) {
let btn = e.target.closest('.accord__btn');
if (btn) {
ContextSelector.one(btn, '.accord__item << .accord__content').classList.add('accord__content_active');
}
});
The library provides extended CSS syntax for searching items relative to context element.
The syntax consists of two parts separated by operator <<.
First part of the selector selects top context for the search element by second part of selector.
First part can take the following values:
$document or empty first part - selector becomes the normal selector on the document;$context - search by selector inside context element;Second part is simple CSS-selector. If second part is missing, top context returned by first part of the selector is returned.
There are several entry points for importing a library:
import ContextSelector from 'context-selector' - similarly with-shims;import ContextSelector from 'context-selector/standard' - easy import without polyfills for ie11;import ContextSelector from 'context-selector/with-shims' - import with shims for ie11;import ContextSelector from 'context-selector/with-polyfills' - import with polyfill for ie11;Differences shims from polyfills you can read in polyshim package.
When directly include the script from the dist folder to the browser, you can get an ContextSelector instance via window.ContextSelector.default.
one(context, selector) - search first element by extended selector
context {Element} - context elementselector {String} - extended CSS-selector@return {Element|null} - returns target element or nullall(context, selector) - search all elements by extended selector
context {Element} - context elementselector {String} - extended CSS-selector@return {Element[]} - returns array of target elementssetShim([setClosest]) - sets shims for non-cross-browser methods
setClosest {Function|null} - shim for Element.prototype.closestTo run tests, use:
npm test
If necessary, you can install launchers for other browsers and activate them in karma.conf.js:
npm i --save-dev karma-ie-launcher
FAQs
Find elements relative other by CSS selector
We found that context-selector demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.