Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

utility to select unist nodes with CSS-like selectors

License

NotificationsYou must be signed in to change notification settings

syntax-tree/unist-util-select

BuildCoverageDownloadsSizeSponsorsBackersChat

unist utility with equivalents forquerySelector,querySelectorAll,andmatches.

Contents

What is this?

This package lets you find nodes in a tree, similar to howquerySelector,querySelectorAll, andmatches work with the DOM.

One notable difference between DOM and hast is that DOM nodes have referencesto their parents, meaning thatdocument.body.matches(':last-child') canbe 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 withhast, and only want to select elements, usehast-util-select instead.

This is a small utility that is quite useful, but is rather slow if you use it alot.For each call, it has to walk the entire tree.In some cases, walking the tree once withunist-util-visitis 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 isESM only.In Node.js (version 16+), install withnpm:

npm install unist-util-select

In Deno withesm.sh:

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

In browsers withesm.sh:

<scripttype="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'consttree=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))// => trueconsole.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 identifiersmatches,select, andselectAll.There is no default export.

matches(selector, node)

Check that the givennode matchesselector.

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
  • selector (string)— CSS selector, such as (heading,link, linkReference).
  • node (Node, optional)— node that might matchselector
Returns

Whethernode matchesselector (boolean).

Example
import{u}from'unist-builder'import{matches}from'unist-util-select'matches('strong, em',u('strong',[u('text','important')]))// => truematches('[lang]',u('code',{lang:'js'},'console.log(1)'))// => true

select(selector, tree)

Select the first node that matchesselector in the giventree.

Searches the tree inpreorder.

Parameters
  • selector (string)— CSS selector, such as (heading,link, linkReference).
  • tree (Node, optional)— tree to search
Returns

First node intree that matchesselector orundefined if nothing is found.

This could betree 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 matchselector in the giventree.

Searches the tree inpreorder.

Parameters
  • selector (string)— CSS selector, such as (heading,link, linkReference).
  • tree (Node, optional)— tree to search
Returns

Nodes intree that matchselector.

This could includetree 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

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

Types

This package is fully typed withTypeScript.It exports no additional types.

Compatibility

Projects maintained by the unified collective are compatible with maintainedversions of Node.js.

When we cut a new major release, we drop support for unmaintained versions ofNode.This means we try to keep the current release line,unist-util-select@^5,compatible with Node.js 16.

Related

Contribute

Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.

This project has acode of conduct.By interacting with this repository, organization, or community you agree toabide by its terms.

License

MIT © Eugene Sharygin

About

utility to select unist nodes with CSS-like selectors

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp