unified

Project:syntax-tree/hast-util-select

Package:hast-util-select@3.0.1

  1. Dependencies:14·Dependents:23
  2. querySelector, querySelectorAll, and matches in hast
  1. hast 78
  2. css 9
  3. selector 4
  4. select 2
  5. match 2
  6. matches 2

hast-util-select

Build Coverage Downloads Size Sponsors Backers Chat

hast utility with equivalents querySelector, querySelectorAll, and matches.

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. This information is not stored in hast, so selectors like that don’t work.

View the list of supported selectors »

Install

npm:

npm install hast-util-select

API

select.matches(selector, node[, space])

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 (p b, p > b), neither are selectors like :first-child, etc. This only checks that the given element matches the selector.

Usage
var h = require('hastscript')
var matches = require('hast-util-select').matches

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
// ...
Parameters
Returns

boolean — Whether the node matches the selector.

select.select(selector, tree[, space])

Select the first node matching selector in the given tree (could be the tree itself). Searches the tree in preorder.

Usage
var h = require('hastscript')
var select = require('hast-util-select').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' } ] }
Parameters
Returns

Element? — The found element, if any.

select.selectAll(selector, tree[, space])

Select all nodes matching selector in the given tree (could include the tree itself). Searches the tree in preorder.

Usage
var h = require('hastscript')
var selectAll = require('hast-util-select').selectAll

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' } ] } ]
Parameters
Returns

Array.<Element> — All found elements, if any.

Support

Unsupported

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 © Titus Wormer