This package lets you find nodes in a tree, similar to how querySelector,
querySelectorAll, and matches work with the DOM.
One notable difference between DOM and hast is that DOM nodes have references
to their parents, meaning that document.body.matches(':last-child') can
be evaluated to check whether the body is the last child of its parent.
This information is not stored in hast, so selectors like that don’t work.
When should I use this?
This utility works on any unist syntax tree and you can select all node types.
If you are working with hast, and only want to select elements, use
hast-util-select instead.
This is a small utility that is quite useful, but is rather slow if you use it a
lot.
For each call, it has to walk the entire tree.
In some cases, walking the tree once with unist-util-visit
is smarter, such as when you want to change certain nodes.
On the other hand, this is quite powerful and fast enough for many other cases.
Install
This package is ESM only.
In Node.js (version 16+), install with npm:
import {u} from'unist-builder'import {matches, select, selectAll} from'unist-util-select'const tree = u('blockquote', [
u('paragraph', [u('text', 'Alpha')]),
u('paragraph', [u('text', 'Bravo')]),
u('code', 'Charlie'),
u('paragraph', [u('text', 'Delta')]),
u('paragraph', [u('text', 'Echo')]),
u('paragraph', [u('text', 'Foxtrot')]),
u('paragraph', [u('text', 'Golf')])
])
console.log(matches('blockquote, list', tree)) // => trueconsole.log(select('code ~ :nth-child(even)', tree))
// The paragraph with `Delta`console.log(selectAll('code ~ :nth-child(even)', tree))
// The paragraphs with `Delta` and `Foxtrot`
API
This package exports the identifiers matches,
select, and selectAll.
There is no default export.
matches(selector, node)
Check that the given node matches selector.
This only checks the node itself, not the surrounding tree.
Thus, nesting in selectors is not supported (paragraph strong,
paragraph > strong), neither are selectors like :first-child, etc.
This only checks that the given node matches the selector.
Parameters
selector (string)
— CSS selector, such as (heading, link, linkReference).
node (Node, optional)
— node that might match selector
code ~ paragraph (combinator: general sibling selector)
[attr] (attribute existence, checks that the value on the tree is not
nullish)
[attr=value] (attribute equality, this stringifies values on the tree)
[attr^=value] (attribute begins with, only works on strings)
[attr$=value] (attribute ends with, only works on strings)
[attr*=value] (attribute contains, only works on strings)
[attr~=value] (attribute contains, checks if value is in the array,
if there’s an array on the tree, otherwise same as attribute equality)
:is() (functional pseudo-class)
:has() (functional pseudo-class; also supports a:has(> b))
:not() (functional pseudo-class)
:blank (pseudo-class, blank and empty are the same: a parent without
children, or a node without value)
:empty (pseudo-class, blank and empty are the same: a parent without
children, or a node without value)
:root (pseudo-class, matches the given node)
:scope (pseudo-class, matches the given node)
* :first-child (pseudo-class)
* :first-of-type (pseudo-class)
* :last-child (pseudo-class)
* :last-of-type (pseudo-class)
* :only-child (pseudo-class)
* :only-of-type (pseudo-class)
* :nth-child() (functional pseudo-class)
* :nth-last-child() (functional pseudo-class)
* :nth-last-of-type() (functional pseudo-class)
* :nth-of-type() (functional pseudo-class)
Notes
* — not supported in matches
:any() and :matches() are renamed to :is() in CSS
Types
This package is fully typed with TypeScript.
It exports no additional types.
Compatibility
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, unist-util-select@^5,
compatible with Node.js 16.
unist utility to select nodes with CSS-like selectors
The npm package unist-util-select receives a total of 140,577 weekly downloads. As such, unist-util-select popularity was classified as popular.
We found that unist-util-select demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 2 open source maintainers collaborating on the project.
Last updated on 06 Nov 2023
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.
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.