unified

Project: syntax-tree/unist-util-select

Package: unist-util-select@3.0.1

  1. Dependencies: 5·Dependents: 131
  2. unist utility to select nodes with CSS-like selectors
  1. util 115
  2. utility 113
  3. unist 106
  4. tree 42
  5. ast 29
  6. unist-util 24
  7. node 21
  8. attribute 9
  9. find 9
  10. walk 7
  11. visit 5
  12. selector 4
  13. filter 4
  14. type 3
  15. select 2
  16. match 2

unist-util-select

Build Coverage Downloads Size Sponsors Backers Chat

unist utility with equivalents querySelector, querySelectorAll, and matches.

Note that the DOM has references to their parent nodes, meaning that document.body.matches(':last-child') can be evaluated. This information is not stored in unist, so selectors like that don’t work.

View the list of supported selectors »

Install

npm:

npm install unist-util-select

API

select.matches(selector, node)

Check that the given node matches selector. Returns boolean, whether the node matches or not.

This only checks the element itself, not the surrounding tree. Thus, nesting in selectors is not supported (paragraph strong, paragraph > strong), nor are selectors like :first-child, etc. This only checks that the given element matches the selector.

var u = require('unist-builder')
var matches = require('unist-util-select').matches

matches('strong, em', u('strong', [u('text', 'important')])) // => true
matches('[lang]', u('code', {lang: 'js'}, 'console.log(1)')) // => true

select.select(selector, tree)

Select the first node matching selector in the given tree (could be the tree itself). Returns the found node, if any.

var u = require('unist-builder')
var select = require('unist-util-select').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' } ] }

select.selectAll(selector, tree)

Select all nodes matching selector in the given tree (could include the tree itself). Returns all found nodes, if any.

var u = require('unist-builder')
var selectAll = require('unist-util-select').selectAll

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

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, organisation, or community you agree to abide by its terms.

License

MIT © Eugene Sharygin