Gathering detailed insights and metrics for unist-util-select
Gathering detailed insights and metrics for unist-util-select
Gathering detailed insights and metrics for unist-util-select
Gathering detailed insights and metrics for unist-util-select
utility to select unist nodes with CSS-like selectors
npm install unist-util-select
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
67 Stars
154 Commits
10 Forks
8 Watchers
1 Branches
13 Contributors
Updated on May 24, 2025
Latest Version
5.1.0
Package Id
unist-util-select@5.1.0
Unpacked Size
44.37 kB
Size
11.60 kB
File Count
19
NPM Version
10.2.0
Node Version
21.0.0
Published on
Nov 06, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
unist utility with equivalents for querySelector
, querySelectorAll
,
and matches
.
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.
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.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install unist-util-select
In Deno with esm.sh
:
1import {matches, select, selectAll} from "https://esm.sh/unist-util-select@5"
In browsers with esm.sh
:
1<script type="module"> 2 import {matches, select, selectAll} from "https://esm.sh/unist-util-select@5?bundle" 3</script>
1import {u} from 'unist-builder' 2import {matches, select, selectAll} from 'unist-util-select' 3 4const tree = u('blockquote', [ 5 u('paragraph', [u('text', 'Alpha')]), 6 u('paragraph', [u('text', 'Bravo')]), 7 u('code', 'Charlie'), 8 u('paragraph', [u('text', 'Delta')]), 9 u('paragraph', [u('text', 'Echo')]), 10 u('paragraph', [u('text', 'Foxtrot')]), 11 u('paragraph', [u('text', 'Golf')]) 12]) 13 14console.log(matches('blockquote, list', tree)) // => true 15 16console.log(select('code ~ :nth-child(even)', tree)) 17// The paragraph with `Delta` 18 19console.log(selectAll('code ~ :nth-child(even)', tree)) 20// The paragraphs with `Delta` and `Foxtrot`
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.
selector
(string
)
— CSS selector, such as (heading
, link, linkReference
).node
(Node
, optional)
— node that might match selector
Whether node
matches selector
(boolean
).
1import {u} from 'unist-builder' 2import {matches} from 'unist-util-select' 3 4matches('strong, em', u('strong', [u('text', 'important')])) // => true 5matches('[lang]', u('code', {lang: 'js'}, 'console.log(1)')) // => true
select(selector, tree)
Select the first node that matches selector
in the given tree
.
Searches the tree in preorder.
selector
(string
)
— CSS selector, such as (heading
, link, linkReference
).tree
(Node
, optional)
— tree to searchFirst node in tree
that matches selector
or undefined
if nothing is found.
This could be tree
itself.
1import {u} from 'unist-builder' 2import {select} from 'unist-util-select' 3 4console.log( 5 select( 6 'code ~ :nth-child(even)', 7 u('blockquote', [ 8 u('paragraph', [u('text', 'Alpha')]), 9 u('paragraph', [u('text', 'Bravo')]), 10 u('code', 'Charlie'), 11 u('paragraph', [u('text', 'Delta')]), 12 u('paragraph', [u('text', 'Echo')]) 13 ]) 14 ) 15)
Yields:
1{type: 'paragraph', children: [{type: 'text', value: 'Delta'}]}
selectAll(selector, tree)
Select all nodes that match selector
in the given tree
.
Searches the tree in preorder.
selector
(string
)
— CSS selector, such as (heading
, link, linkReference
).tree
(Node
, optional)
— tree to searchNodes in tree
that match selector
.
This could include tree
itself.
1import {u} from 'unist-builder' 2import {selectAll} from 'unist-util-select' 3 4console.log( 5 selectAll( 6 'code ~ :nth-child(even)', 7 u('blockquote', [ 8 u('paragraph', [u('text', 'Alpha')]), 9 u('paragraph', [u('text', 'Bravo')]), 10 u('code', 'Charlie'), 11 u('paragraph', [u('text', 'Delta')]), 12 u('paragraph', [u('text', 'Echo')]), 13 u('paragraph', [u('text', 'Foxtrot')]), 14 u('paragraph', [u('text', 'Golf')]) 15 ]) 16 ) 17)
Yields:
1[ 2 {type: 'paragraph', children: [{type: 'text', value: 'Delta'}]}, 3 {type: 'paragraph', children: [{type: 'text', value: 'Foxtrot'}]} 4]
*
(universal selector),
(multiple selector)paragraph
(type selector)blockquote paragraph
(combinator: descendant selector)blockquote > paragraph
(combinator: child selector)code + paragraph
(combinator: adjacent sibling 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)matches
:any()
and :matches()
are renamed to :is()
in CSSThis package is fully typed with TypeScript. It exports no additional types.
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-util-is
— check if a node passes a testunist-util-visit
— recursively walk over nodesunist-util-visit-parents
— like visit
, but with a stack of parentsunist-builder
— create unist treesSee contributing.md
in syntax-tree/.github
for
ways to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
MIT © Eugene Sharygin
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More