hast utility to create nodes from an advanced CSS selector.
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install hast-util-from-selector
import {fromSelector} from 'hast-util-from-selector'
console.log(fromSelector('p svg[viewbox=0 0 10 10] circle[cx=10][cy=10][r=10]'))
Yields:
{
type: 'element',
tagName: 'p',
properties: {},
children: [
{
type: 'element',
tagName: 'svg',
properties: {viewBox: '0 0 10 10'},
children: [
{
type: 'element',
tagName: 'circle',
properties: {cx: '10', cy: '10', r: '10'},
children: []
}
]
}
]
}
This package exports the following identifiers: fromSelector
.
There is no default export.
Create one or more element nodes from a CSS selector.
selector
(string
, optional) — CSS selectorspace
(string
, optional) — Treated asoptions.space
options.space
(enum,'svg'
or'html'
, default:'html'
) — Which space first element in the selector is in. When ansvg
is created in HTML, the space is switched automatically to SVG
-
*
(universal selector, creates adiv
in HTML,g
in SVG) -
p
(type selector) -
.class
(class selector) -
#id
(id selector) -
[attr]
(attribute existence, creates a boolean) -
[attr=value]
(attribute equality) -
article p
(descendant combinator) -
article > p
(child combinator) -
section h1 + p
(next-sibling combinator, not at root) -
section h1 ~ p
(subsequent-sibling combinator, not at root)
Use of from-selector
can open you up to a cross-site scripting (XSS)
attack as values are injected into the syntax tree.
Either do not use user input in from-selector
or use
hast-util-santize
.
hast-util-parse-selector
— create an element from a simple CSS selector
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.