unist-util-select
unist utility to select nodes with CSS-like selectors
Last updated 8 months ago by wooorm .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install unist-util-select 
SYNC missed versions from official npm registry.

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

  • [x] * (universal selector)
  • [x] , (multiple selector)
  • [x] paragraph (type selector)
  • [x] blockquote paragraph (combinator: descendant selector)
  • [x] blockquote > paragraph (combinator: child selector)
  • [x] code + paragraph (combinator: adjacent sibling selector)
  • [x] code ~ paragraph (combinator: general sibling selector)
  • [x] [attr] (attribute existence, checks that the value on the tree is not nully)
  • [x] [attr=value] (attribute equality, this stringifies values on the tree)
  • [x] [attr^=value] (attribute begins with, only works on strings)
  • [x] [attr$=value] (attribute ends with, only works on strings)
  • [x] [attr*=value] (attribute contains, only works on strings)
  • [x] [attr~=value] (attribute contains, checks if value is in the array, if there’s an array on the tree, otherwise same as attribute equality)
  • [x] :any() (functional pseudo-class, use :matches instead)
  • [x] :has() (functional pseudo-class) Relative selectors (:has(> img)) are not supported, but :scope is
  • [x] :matches() (functional pseudo-class)
  • [x] :not() (functional pseudo-class)
  • [x] :blank (pseudo-class, blank and empty are the same: a parent without children, or a node without value)
  • [x] :empty (pseudo-class, blank and empty are the same: a parent without children, or a node without value)
  • [x] :root (pseudo-class, matches the given node)
  • [x] :scope (pseudo-class, matches the given node)
  • [x] * :first-child (pseudo-class)
  • [x] * :first-of-type (pseudo-class)
  • [x] * :last-child (pseudo-class)
  • [x] * :last-of-type (pseudo-class)
  • [x] * :only-child (pseudo-class)
  • [x] * :only-of-type (pseudo-class)
  • [x] * :nth-child() (functional pseudo-class)
  • [x] * :nth-last-child() (functional pseudo-class)
  • [x] * :nth-last-of-type() (functional pseudo-class)
  • [x] * :nth-of-type() (functional pseudo-class)
Notes
  • * — Not supported in matches

Related

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

Current Tags

  • 3.0.1                                ...           latest (8 months ago)

12 Versions

  • 3.0.1                                ...           8 months ago
  • 3.0.0                                ...           a year ago
  • 2.0.2                                ...           a year ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           2 years ago
  • 1.5.0                                ...           5 years ago
  • 1.4.0                                ...           5 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
Maintainers (2)
Downloads
Today 14
This Week 14
This Month 1,174
Last Day 30
Last Week 268
Last Month 1,370
Dependencies (5)
Dev Dependencies (8)
Dependents (164)

Copyright 2014 - 2016 © taobao.org |