Gathering detailed insights and metrics for hast-util-select
Gathering detailed insights and metrics for hast-util-select
Gathering detailed insights and metrics for hast-util-select
Gathering detailed insights and metrics for hast-util-select
hast-util-parse-selector
hast utility to create an element from a simple CSS selector
xast-util-select
Port of `(hast-util-select)[https://github.com/syntax-tree/hast-util-select]` for use with `xast` nodes.
hast-util-from-selector
hast utility to parse CSS selectors to hast nodes
typed-hast-util-parse-selector
utility to add `querySelector`, `querySelectorAll`, and `matches` support for hast
npm install hast-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
41 Stars
152 Commits
1 Forks
8 Watchers
1 Branches
10 Contributors
Updated on Mar 15, 2025
Latest Version
6.0.4
Package Id
hast-util-select@6.0.4
Unpacked Size
69.28 kB
Size
16.65 kB
File Count
36
NPM Version
11.1.0
Node Version
23.1.0
Published on
Feb 19, 2025
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
15
hast utility with equivalents for
matches
, querySelector
, and querySelectorAll
.
This package lets you find nodes in a tree,
similar to how matches
,
querySelector
,
and
querySelectorAll
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 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 utility is similar to
unist-util-select
,
which can find and match any unist node.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install hast-util-select
In Deno with esm.sh
:
1import {matches, select, selectAll} from 'https://esm.sh/hast-util-select@6'
In browsers with esm.sh
:
1<script type="module"> 2 import {matches, select, selectAll} from 'https://esm.sh/hast-util-select@6?bundle' 3</script>
1import {h} from 'hastscript' 2import {matches, select, selectAll} from 'hast-util-select' 3 4const tree = h('section', [ 5 h('p', 'Alpha'), 6 h('p', 'Bravo'), 7 h('h1', 'Charlie'), 8 h('p', 'Delta'), 9 h('p', 'Echo'), 10 h('p', 'Foxtrot'), 11 h('p', 'Golf') 12]) 13 14console.log(matches('section', tree)) // `true` 15 16console.log(select('h1 ~ :nth-child(even)', tree)) 17// The paragraph with `Delta` 18 19console.log(selectAll('h1 ~ :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[, space])
Check that the given node
matches selector
.
This only checks the element itself,
not the surrounding tree.
Thus,
nesting in selectors is not supported (p b
, p > b
),
neither are selectors like :first-child
,
etc.
This only checks that the given element matches the selector.
selector
(string
, example: 'h1'
, 'a, b'
)
— CSS selectornode
(Node
, optional)
— node that might match selector
,
should be an elementspace
(Space
, default: 'html'
)
— name of namespaceWhether node
matches selector
(boolean
).
1import {h} from 'hastscript' 2import {matches} from 'hast-util-select' 3 4matches('b, i', h('b')) // => true 5matches(':any-link', h('a')) // => false 6matches(':any-link', h('a', {href: '#'})) // => true 7matches('.classy', h('a', {className: ['classy']})) // => true 8matches('#id', h('a', {id: 'id'})) // => true 9matches('[lang|=en]', h('a', {lang: 'en'})) // => true 10matches('[lang|=en]', h('a', {lang: 'en-GB'})) // => true
select(selector, tree[, space])
Select the first element that matches selector
in the given tree
.
Searches the tree in preorder.
selector
(string
, example: 'h1'
, 'a, b'
)
— CSS selector, such as (h1
, a, b
)tree
(Node
, optional)
— tree to searchspace
(Space
, default: 'html'
)
— name of namespaceFirst element in tree
that matches selector
or undefined
if nothing is
found.
This could be tree
itself.
1import {h} from 'hastscript' 2import {select} from 'hast-util-select' 3 4console.log( 5 select( 6 'h1 ~ :nth-child(even)', 7 h('section', [ 8 h('p', 'Alpha'), 9 h('p', 'Bravo'), 10 h('h1', 'Charlie'), 11 h('p', 'Delta'), 12 h('p', 'Echo') 13 ]) 14 ) 15)
Yields:
1{ type: 'element', 2 tagName: 'p', 3 properties: {}, 4 children: [ { type: 'text', value: 'Delta' } ] }
selectAll(selector, tree[, space])
Select all elements that match selector
in the given tree
.
Searches the tree in preorder.
selector
(string
, example: 'h1'
, 'a, b'
)
— CSS selectortree
(Node
, optional)
— tree to searchspace
(Space
, default: 'html'
)
— name of namespaceElements in tree
that match selector
.
This could include tree
itself.
1import {h} from 'hastscript' 2import {selectAll} from 'hast-util-select' 3 4console.log( 5 selectAll( 6 'h1 ~ :nth-child(even)', 7 h('section', [ 8 h('p', 'Alpha'), 9 h('p', 'Bravo'), 10 h('h1', 'Charlie'), 11 h('p', 'Delta'), 12 h('p', 'Echo'), 13 h('p', 'Foxtrot'), 14 h('p', 'Golf') 15 ]) 16 ) 17)
Yields:
1[ { type: 'element', 2 tagName: 'p', 3 properties: {}, 4 children: [ { type: 'text', value: 'Delta' } ] }, 5 { type: 'element', 6 tagName: 'p', 7 properties: {}, 8 children: [ { type: 'text', value: 'Foxtrot' } ] } ]
Space
Namespace (TypeScript type).
1type Space = 'html' | 'svg'
*
(universal selector),
(multiple selector)p
(type selector).class
(class selector)#id
(id selector)article p
(combinator: descendant selector)article > p
(combinator: child selector)h1 + p
(combinator: next-sibling selector)h1 ~ p
(combinator: subsequent sibling selector)[attr]
(attribute existence)[attr… i]
(attribute case-insensitive)[attr… s]
(attribute case-sensitive) (useless, default)[attr=value]
(attribute equality)[attr~=value]
(attribute contains in space-separated list)[attr|=value]
(attribute equality or prefix)[attr^=value]
(attribute begins with)[attr$=value]
(attribute ends with)[attr*=value]
(attribute contains):dir()
(functional pseudo-class):has()
(functional pseudo-class; also supports a:has(> b)
):is()
(functional pseudo-class):lang()
(functional pseudo-class):not()
(functional pseudo-class):any-link
(pseudo-class):blank
(pseudo-class):checked
(pseudo-class):disabled
(pseudo-class):empty
(pseudo-class):enabled
(pseudo-class):optional
(pseudo-class):read-only
(pseudo-class):read-write
(pseudo-class):required
(pseudo-class):root
(pseudo-class):scope
(pseudo-class)::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)||
(column combinator)ns|E
(namespace type selector)*|E
(any namespace type selector)|E
(no namespace type selector)[ns|attr]
(namespace attribute)[*|attr]
(any namespace attribute)[|attr]
(no namespace attribute):nth-child(n of S)
(functional pseudo-class, note: scoping to
parents is not supported):nth-last-child(n of S)
(functional pseudo-class, note: scoping to
parents is not supported):active
(pseudo-class):autofill
(pseudo-class):buffering
(pseudo-class):closed
(pseudo-class):current
(pseudo-class):current()
(functional pseudo-class):default
(pseudo-class):defined
(pseudo-class):focus
(pseudo-class):focus-visible
(pseudo-class):focus-within
(pseudo-class):fullscreen
(pseudo-class):future
(pseudo-class):host()
(functional pseudo-class):host-context()
(functional pseudo-class):hover
(pseudo-class):in-range
(pseudo-class):indeterminate
(pseudo-class):invalid
(pseudo-class):link
(pseudo-class):local-link
(pseudo-class):modal
(pseudo-class):muted
(pseudo-class):nth-col()
(functional pseudo-class):nth-last-col()
(functional pseudo-class):open
(pseudo-class):out-of-range
(pseudo-class):past
(pseudo-class):paused
(pseudo-class):placeholder-shown
(pseudo-class):playing
(pseudo-class):seeking
(pseudo-class):stalled
(pseudo-class):target
(pseudo-class):target-within
(pseudo-class):user-invalid
(pseudo-class):valid
(pseudo-class):visited
(pseudo-class):volume-locked
(pseudo-class):where()
(functional pseudo-class)::before
(pseudo-elements: none are supported)matches
:any()
and :matches()
are renamed to :is()
in CSS.This package is fully typed with TypeScript.
It exports the additional type Space
.
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,
hast-util-select@6
,
compatible with Node.js 16.
This package does not change the syntax tree so there are no openings for cross-site scripting (XSS) attacks.
unist-util-select
— select unist nodes with CSS-like selectorshast-util-find-and-replace
— find and replace text in a hast treehast-util-parse-selector
— create an element from a simple CSS selectorhast-util-from-selector
— create an element from a complex CSS selectorSee 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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
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
Reason
SAST tool is not run on all commits -- score normalized to 0
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