unified

Project: syntax-tree/unist-util-select

Package: unist-util-select@4.0.2

  1. unist utility to select nodes with CSS-like selectors
  1. util 145
  2. utility 141
  3. unist 132
  4. tree 44
  5. ast 37
  6. unist-util 25
  7. node 24
  8. find 11
  9. attribute 9
  10. walk 8
  11. expression 8
  12. visit 6
  13. filter 5
  14. selector 4
  15. type 3
  16. select 2
  17. match 2

unist-util-select

Build Coverage Downloads Size Sponsors Backers Chat

unist utility with equivalents for querySelector, querySelectorAll, and matches.

Contents

What is this?

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:

npm install unist-util-select

In Deno with esm.sh:

import {matches, select, selectAll} from "https://esm.sh/unist-util-select@5"

In browsers with esm.sh:

<script type="module">
  import {matches, select, selectAll} from "https://esm.sh/unist-util-select@5?bundle"
</script>

Use

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)) // => true

console.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
Returns

Whether node matches selector (boolean).

Example
import {u} from 'unist-builder'
import {matches} from 'unist-util-select'

matches('strong, em', u('strong', [u('text', 'important')])) // => true
matches('[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.

Parameters
Returns

First node in tree that matches selector or undefined if nothing is found.

This could be tree itself.

Example
import {u} from 'unist-builder'
import {select} from 'unist-util-select'

console.log(
  select(
    'code ~ :nth-child(even)',
    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')])
    ])
  )
)

Yields:

{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.

Parameters
Returns

Nodes in tree that match selector.

This could include tree itself.

Example
import {u} from 'unist-builder'
import {selectAll} from 'unist-util-select'

console.log(
  selectAll(
    'code ~ :nth-child(even)',
    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')])
    ])
  )
)

Yields:

[
  {type: 'paragraph', children: [{type: 'text', value: 'Delta'}]},
  {type: 'paragraph', children: [{type: 'text', value: 'Foxtrot'}]}
]

Support

Notes

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.

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.

License

MIT © Eugene Sharygin