hast-util-select
hast utility with equivalents for matches
, querySelector
,
and querySelectorAll
.
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Support
- Unsupported
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
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.
When should I use this?
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.
Install
This package is ESM only. In Node.js (version 16+), install with npm:
npm install hast-util-select
In Deno with esm.sh
:
import {matches, select, selectAll} from "https://esm.sh/hast-util-select@6"
In browsers with esm.sh
:
<script type="module">
import {matches, select, selectAll} from "https://esm.sh/hast-util-select@6?bundle"
</script>
Use
import {h} from 'hastscript'
import {matches, select, selectAll} from 'hast-util-select'
const tree = h('section', [
h('p', 'Alpha'),
h('p', 'Bravo'),
h('h1', 'Charlie'),
h('p', 'Delta'),
h('p', 'Echo'),
h('p', 'Foxtrot'),
h('p', 'Golf')
])
console.log(matches('section', tree)) // `true`
console.log(select('h1 ~ :nth-child(even)', tree))
// The paragraph with `Delta`
console.log(selectAll('h1 ~ :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[, 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.
Parameters
selector
(string
) — CSS selector, such as (h1
,a, b
)node
(Node
, optional) — node that might matchselector
, should be an elementspace
(Space
, default:'html'
) — name of namespace
Returns
Whether node
matches selector
(boolean
).
Example
import {h} from 'hastscript'
import {matches} from 'hast-util-select'
matches('b, i', h('b')) // => true
matches(':any-link', h('a')) // => false
matches(':any-link', h('a', {href: '#'})) // => true
matches('.classy', h('a', {className: ['classy']})) // => true
matches('#id', h('a', {id: 'id'})) // => true
matches('[lang|=en]', h('a', {lang: 'en'})) // => true
matches('[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.
Parameters
selector
(string
) — CSS selector, such as (h1
,a, b
)tree
(Node
, optional) — tree to searchspace
(Space
, default:'html'
) — name of namespace
Returns
First element in tree
that matches selector
or undefined
if nothing is
found.
This could be tree
itself.
Example
import {h} from 'hastscript'
import {select} from 'hast-util-select'
console.log(
select(
'h1 ~ :nth-child(even)',
h('section', [
h('p', 'Alpha'),
h('p', 'Bravo'),
h('h1', 'Charlie'),
h('p', 'Delta'),
h('p', 'Echo')
])
)
)
Yields:
{ type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Delta' } ] }
selectAll(selector, tree[, space])
Select all elements that match selector
in the given tree
.
Searches the tree in preorder.
Parameters
selector
(string
) — CSS selector, such as (h1
,a, b
)tree
(Node
, optional) — tree to searchspace
(Space
, default:'html'
) — name of namespace
Returns
Elements in tree
that match selector
.
This could include tree
itself.
Example
import {h} from 'hastscript'
import {selectAll} from 'hast-util-select'
console.log(
selectAll(
'h1 ~ :nth-child(even)',
h('section', [
h('p', 'Alpha'),
h('p', 'Bravo'),
h('h1', 'Charlie'),
h('p', 'Delta'),
h('p', 'Echo'),
h('p', 'Foxtrot'),
h('p', 'Golf')
])
)
)
Yields:
[ { type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Delta' } ] },
{ type: 'element',
tagName: 'p',
properties: {},
children: [ { type: 'text', value: 'Foxtrot' } ] } ]
Space
Namespace (TypeScript type).
Type
type Space = 'html' | 'svg'
Support
- <input checked="" disabled="" type="checkbox">
*
(universal selector) - <input checked="" disabled="" type="checkbox">
,
(multiple selector) - <input checked="" disabled="" type="checkbox">
p
(type selector) - <input checked="" disabled="" type="checkbox">
.class
(class selector) - <input checked="" disabled="" type="checkbox">
#id
(id selector) - <input checked="" disabled="" type="checkbox">
article p
(combinator: descendant selector) - <input checked="" disabled="" type="checkbox">
article > p
(combinator: child selector) - <input checked="" disabled="" type="checkbox">
h1 + p
(combinator: next-sibling selector) - <input checked="" disabled="" type="checkbox">
h1 ~ p
(combinator: subsequent sibling selector) - <input checked="" disabled="" type="checkbox">
[attr]
(attribute existence) - <input checked="" disabled="" type="checkbox">
[attr… i]
(attribute case-insensitive) - <input checked="" disabled="" type="checkbox">
[attr… s]
(attribute case-sensitive) (useless, default) - <input checked="" disabled="" type="checkbox">
[attr=value]
(attribute equality) - <input checked="" disabled="" type="checkbox">
[attr~=value]
(attribute contains in space-separated list) - <input checked="" disabled="" type="checkbox">
[attr|=value]
(attribute equality or prefix) - <input checked="" disabled="" type="checkbox">
[attr^=value]
(attribute begins with) - <input checked="" disabled="" type="checkbox">
[attr$=value]
(attribute ends with) - <input checked="" disabled="" type="checkbox">
[attr*=value]
(attribute contains) - <input checked="" disabled="" type="checkbox">
:dir()
(functional pseudo-class) - <input checked="" disabled="" type="checkbox">
:has()
(functional pseudo-class; also supportsa:has(> b)
) - <input checked="" disabled="" type="checkbox">
:is()
(functional pseudo-class) - <input checked="" disabled="" type="checkbox">
:lang()
(functional pseudo-class) - <input checked="" disabled="" type="checkbox">
:not()
(functional pseudo-class) - <input checked="" disabled="" type="checkbox">
:any-link
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:blank
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:checked
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:disabled
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:empty
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:enabled
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:optional
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:read-only
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:read-write
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:required
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:root
(pseudo-class) - <input checked="" disabled="" type="checkbox">
:scope
(pseudo-class): - <input checked="" disabled="" type="checkbox"> *
:first-child
(pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:first-of-type
(pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:last-child
(pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:last-of-type
(pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:only-child
(pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:only-of-type
(pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:nth-child()
(functional pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:nth-last-child()
(functional pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:nth-last-of-type()
(functional pseudo-class) - <input checked="" disabled="" type="checkbox"> *
:nth-of-type()
(functional pseudo-class)
Unsupported
- <input disabled="" type="checkbox"> †
||
(column combinator) - <input disabled="" type="checkbox"> †
ns|E
(namespace type selector) - <input disabled="" type="checkbox"> †
*|E
(any namespace type selector) - <input disabled="" type="checkbox"> †
|E
(no namespace type selector) - <input disabled="" type="checkbox"> †
[ns|attr]
(namespace attribute) - <input disabled="" type="checkbox"> †
[*|attr]
(any namespace attribute) - <input disabled="" type="checkbox"> †
[|attr]
(no namespace attribute) - <input disabled="" type="checkbox"> ‖
:nth-child(n of S)
(functional pseudo-class, note: scoping to parents is not supported) - <input disabled="" type="checkbox"> ‖
:nth-last-child(n of S)
(functional pseudo-class, note: scoping to parents is not supported) - <input disabled="" type="checkbox"> †
:active
(pseudo-class) - <input disabled="" type="checkbox"> †
:autofill
(pseudo-class) - <input disabled="" type="checkbox"> †
:buffering
(pseudo-class) - <input disabled="" type="checkbox"> §
:closed
(pseudo-class) - <input disabled="" type="checkbox"> †
:current
(pseudo-class) - <input disabled="" type="checkbox"> †
:current()
(functional pseudo-class) - <input disabled="" type="checkbox"> †
:default
(pseudo-class) - <input disabled="" type="checkbox"> †
:defined
(pseudo-class) - <input disabled="" type="checkbox"> †
:focus
(pseudo-class) - <input disabled="" type="checkbox"> †
:focus-visible
(pseudo-class) - <input disabled="" type="checkbox"> †
:focus-within
(pseudo-class) - <input disabled="" type="checkbox"> †
:fullscreen
(pseudo-class) - <input disabled="" type="checkbox"> †
:future
(pseudo-class) - <input disabled="" type="checkbox"> §
:host()
(functional pseudo-class) - <input disabled="" type="checkbox"> §
:host-context()
(functional pseudo-class) - <input disabled="" type="checkbox"> †
:hover
(pseudo-class) - <input disabled="" type="checkbox"> ‡
:in-range
(pseudo-class) - <input disabled="" type="checkbox"> †
:indeterminate
(pseudo-class) - <input disabled="" type="checkbox"> ‡
:invalid
(pseudo-class) - <input disabled="" type="checkbox"> †
:link
(pseudo-class) - <input disabled="" type="checkbox"> †
:local-link
(pseudo-class) - <input disabled="" type="checkbox"> †
:modal
(pseudo-class) - <input disabled="" type="checkbox"> †
:muted
(pseudo-class) - <input disabled="" type="checkbox"> †
:nth-col()
(functional pseudo-class) - <input disabled="" type="checkbox"> †
:nth-last-col()
(functional pseudo-class) - <input disabled="" type="checkbox"> §
:open
(pseudo-class) - <input disabled="" type="checkbox"> ‡
:out-of-range
(pseudo-class) - <input disabled="" type="checkbox"> †
:past
(pseudo-class) - <input disabled="" type="checkbox"> †
:paused
(pseudo-class) - <input disabled="" type="checkbox"> †
:placeholder-shown
(pseudo-class) - <input disabled="" type="checkbox"> †
:playing
(pseudo-class) - <input disabled="" type="checkbox"> †
:seeking
(pseudo-class) - <input disabled="" type="checkbox"> †
:stalled
(pseudo-class) - <input disabled="" type="checkbox"> †
:target
(pseudo-class) - <input disabled="" type="checkbox"> †
:target-within
(pseudo-class) - <input disabled="" type="checkbox"> †
:user-invalid
(pseudo-class) - <input disabled="" type="checkbox"> ‡
:valid
(pseudo-class) - <input disabled="" type="checkbox"> †
:visited
(pseudo-class) - <input disabled="" type="checkbox"> †
:volume-locked
(pseudo-class) - <input disabled="" type="checkbox"> §
:where()
(functional pseudo-class) - <input disabled="" type="checkbox"> †
::before
(pseudo-elements: none are supported)
Notes
- * — not supported in
matches
- † — needs a user, browser, interactivity, scripting, or whole CSS to make sense
- ‡ — not very interested in writing / including the code for this
- § — too new, the spec is still changing
- ‖ — pr wanted!
:any()
and:matches()
are renamed to:is()
in CSS.
Types
This package is fully typed with TypeScript.
It exports the additional type Space
.
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, hast-util-select@^6
,
compatible with Node.js 16.
Security
This package does not change the syntax tree so there are no openings for cross-site scripting (XSS) attacks.
Related
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 selector
Contribute
See 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.