hast-util-select
hast utility for `querySelector`, `querySelectorAll`, and `matches`
Last updated 8 months ago by wooorm .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install hast-util-select 
SYNC missed versions from official npm registry.

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.

Use
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
  • selector (string) — CSS selectors (, is also supported)
  • node (Node) — Thing to check, could be anything, but should be an element
  • space (enum, 'svg' or 'html', default: 'html') — Which space the node exists in
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.

Use
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
  • selector (string) — CSS selectors (, is also supported)
  • tree (Node) — Tree to search
  • space (enum, 'svg' or 'html', default: 'html') — Which space the tree exists in
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.

Use
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
  • selector (string) — CSS selectors (, is also supported)
  • tree (Node) — Tree to search
  • space (enum, 'svg' or 'html', default: 'html') — Which space the tree exists in
Returns

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

Support

  • [x] * (universal selector)
  • [x] , (multiple selector)
  • [x] p (type selector)
  • [x] .class (class selector)
  • [x] #id (id selector)
  • [x] article p (combinator: descendant selector)
  • [x] article > p (combinator: child selector)
  • [x] h1 + p (combinator: next-sibling selector)
  • [x] h1 ~ p (combinator: subsequent sibling selector)
  • [x] [attr] (attribute existence)
  • [x] [attr=value] (attribute equality)
  • [x] [attr~=value] (attribute contains in space-separated list)
  • [x] [attr|=value] (attribute equality or prefix)
  • [x] [attr^=value] (attribute begins with)
  • [x] [attr$=value] (attribute ends with)
  • [x] [attr*=value] (attribute contains)
  • [x] :any() (functional pseudo-class, use :matches instead)
  • [x] :dir() (functional pseudo-class)
  • [x] :has() (functional pseudo-class)
  • [x] :lang() (functional pseudo-class)
  • [x] :matches() (functional pseudo-class)
  • [x] :not() (functional pseudo-class)
  • [x] :any-link (pseudo-class)
  • [x] :blank (pseudo-class)
  • [x] :checked (pseudo-class)
  • [x] :disabled (pseudo-class)
  • [x] :empty (pseudo-class)
  • [x] :enabled (pseudo-class)
  • [x] :optional (pseudo-class)
  • [x] :read-only (pseudo-class)
  • [x] :read-write (pseudo-class)
  • [x] :required (pseudo-class)
  • [x] :root (pseudo-class)
  • [x] :scope (pseudo-class):
  • [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)

Unsupported

  • [ ] † || (column combinator)
  • [ ] ‡ ns|E (namespace type selector)
  • [ ] ‡ *|E (any namespace type selector)
  • [ ] ‡ |E (no namespace type selector)
  • [ ] ‡ [ns|attr] (namespace attribute)
  • [ ] ‡ [*|attr] (any namespace attribute)
  • [ ] ‡ [|attr] (no namespace attribute)
  • [ ] ‡ [attr=value i] (attribute case-insensitive)
  • [ ] ‡ :has() (functional pseudo-class). Relative selectors (:has(> img)) are not supported, but scope is (:has(:scope > img))
  • [ ] ‖ :nth-child(n of S) (functional pseudo-class). Scoping to parents is not supported
  • [ ] ‖ :nth-last-child(n of S) (scoped to parent S). Scoping to parents is not supported
  • [ ] † :active (pseudo-class)
  • [ ] † :current (pseudo-class)
  • [ ] † :current() (functional pseudo-class)
  • [ ] † :default (pseudo-class)
  • [ ] † :defined (pseudo-class)
  • [ ] † :drop (pseudo-class)
  • [ ] † :drop() (functional pseudo-class)
  • [ ] † :focus (pseudo-class)
  • [ ] † :focus-visible (pseudo-class)
  • [ ] † :focus-within (pseudo-class)
  • [ ] † :fullscreen (pseudo-class)
  • [ ] † :future (pseudo-class)
  • [ ] ‖ :host() (functional pseudo-class)
  • [ ] ‖ :host-context() (functional pseudo-class)
  • [ ] † :hover (pseudo-class)
  • [ ] § :in-range (pseudo-class)
  • [ ] † :indeterminate (pseudo-class)
  • [ ] § :invalid (pseudo-class)
  • [ ] † :link (pseudo-class)
  • [ ] † :local-link (pseudo-class)
  • [ ] † :nth-column() (functional pseudo-class)
  • [ ] † :nth-last-column() (functional pseudo-class)
  • [ ] § :out-of-range (pseudo-class)
  • [ ] † :past (pseudo-class)
  • [ ] † :paused (pseudo-class)
  • [ ] † :placeholder-shown (pseudo-class)
  • [ ] † :playing (pseudo-class)
  • [ ] ‖ :something() (functional pseudo-class)
  • [ ] † :target (pseudo-class)
  • [ ] † :target-within (pseudo-class)
  • [ ] † :user-error (pseudo-class)
  • [ ] † :user-invalid (pseudo-class)
  • [ ] § :valid (pseudo-class)
  • [ ] † :visited (pseudo-class)
  • [ ] † ::before (pseudo-elements: none are supported)
Notes
  • * — Not supported in matches
  • † — Needs a user, browser, interactivity, or scripting to make sense
  • ‡ — Not supported by the underlying algorithm
  • § — Not very interested in writing / including the code for this
  • ‖ — Too new, the spec is still changing

Security

hast-util-select does not change the syntax tree so there are no openings for cross-site scripting (XSS) attacks.

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

Current Tags

  • 4.0.0                                ...           latest (8 months ago)

7 Versions

  • 4.0.0                                ...           8 months ago
  • 3.0.1                                ...           a year ago
  • 3.0.0                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           4 years ago

Copyright 2014 - 2016 © taobao.org |