hast utility to create trees
npm install hastscript[![Build][badge-build-image]][badge-build-url]
[![Coverage][badge-coverage-image]][badge-coverage-url]
[![Downloads][badge-downloads-image]][badge-downloads-url]
[![Size][badge-size-image]][badge-size-url]
[hast][github-hast] utility to create trees with ease.
* What is this?
* When should I use this?
* Install
* Use
* API
* [h(selector?[, properties][, …children])](#hselector-properties-children)
* [s(selector?[, properties][, …children])](#sselector-properties-children)
* Child
* Properties
* Result
* Syntax tree
* JSX
* Compatibility
* Security
* Related
* Contribute
* License
This package is a hyperscript interface (like createElement from React andh from Vue and such) to help with creating hast trees.
You can use this utility in your project when you generate hast syntax trees
with code.
It helps because it replaces most of the repetition otherwise needed in a syntax
tree with function calls.
It also helps as it improves the attributes you pass by turning them into the
form that is required by hast.
You can instead use [unist-builder][github-unist-builder]
when creating any unist nodes and
[xastscript][github-xastscript] when creating xast (XML) nodes.
This package is [ESM only][github-gist-esm].
In Node.js (version 16+),
install with [npm][npmjs-install]:
``sh`
npm install hastscript
In Deno with [esm.sh][esmsh]:
`js`
import {h} from 'https://esm.sh/hastscript@9'
In browsers with [esm.sh][esmsh]:
`html`
`js
import {h, s} from 'hastscript'
console.log(
h('.foo#some-id', [
h('span', 'some text'),
h('input', {type: 'text', value: 'foo'}),
h('a.alpha', {class: 'bravo charlie', download: 'download'}, [
'delta',
'echo'
])
])
)
console.log(
s('svg', {viewbox: '0 0 500 500', xmlns: 'http://www.w3.org/2000/svg'}, [
s('title', 'SVG element'),`
s('circle', {cx: 120, cy: 120, r: 100})
])
)
Yields:
`js
{
type: 'element',
tagName: 'div',
properties: {className: ['foo'], id: 'some-id'},
children: [
{
type: 'element',
tagName: 'span',
properties: {},
children: [{type: 'text', value: 'some text'}]
},
{
type: 'element',
tagName: 'input',
properties: {type: 'text', value: 'foo'},
children: []
},
{
type: 'element',
tagName: 'a',
properties: {className: ['alpha', 'bravo', 'charlie'], download: true},
children: [{type: 'text', value: 'delta'}, {type: 'text', value: 'echo'}]
}
]
}
{
type: 'element',
tagName: 'svg',
properties: {viewBox: '0 0 500 500', xmlns: 'http://www.w3.org/2000/svg'},
children: [
{
type: 'element',
tagName: 'title',
properties: {},
children: [{type: 'text', value: 'SVG element'}]`
},
{
type: 'element',
tagName: 'circle',
properties: {cx: 120, cy: 120, r: 100},
children: []
}
]
}
This package exports the identifiers [h][api-h] and [s][api-s].Child
There is no default export.
It exports the additional [TypeScript][] types
[][api-child],Properties
[][api-properties],Result
and
[][api-result].
The export map supports the automatic JSX runtime.
You can pass hastscript or hastscript/svg to your build toolimportSource
(TypeScript, Babel, SWC)
with an option or similar.
Create virtual [hast][github-hast] trees for HTML.
##### Signatures
* h(): rooth(null[, …children]): root
* h(selector[, properties][, …children]): element
*
##### Parameters
###### selector
Simple CSS selector
(string, optional).Element
When string, builds an [][github-hast-element].Root
When nullish, builds a [][github-hast-root] instead.foo
The selector can contain a tag name (),#bar
IDs (),.baz
and classes ().h
If the selector is a string but there is no tag name in it then defaults todiv
build a element and s to a g element.selector is parsed byhast-util-parse-selector
[][github-hast-util-parse-selector].
###### properties
Properties of the element
([Properties][api-properties], optional).
###### children
Children of the node ([Child][api-child] or Array, optional).
##### Returns
Created tree ([Result][api-result]).
[Element][github-hast-element] when a selector is passed,Root
otherwise [][github-hast-root].
Create virtual [hast][github-hast] trees for SVG.
Signatures, parameters, and return value are the same as h above.selector
Importantly,
the and properties parameters are interpreted as SVG.
(Lists of) children (TypeScript type).
When strings or numbers are encountered,
they are turned into [Text][github-hast-text]Root
nodes.
[][github-hast-root] nodes are treated as “fragments”,
meaning that their children are used instead.
###### Type
`ts`
type Child =
| Array
| Node
| number
| string
| null
| undefined
Map of properties (TypeScript type).
Keys should match either the HTML attribute name or the DOM property name,
but are case-insensitive.
###### Type
`tsclassName
type Properties = Record<
string,
| boolean
| number
| string
| null
| undefined
// For comma- and space-separated values such as :style
| Array
// Accepts value for prop as object.`
| Record
>
Result from a h (or s) call (TypeScript type).
###### Type
`ts`
type Result = Element | Root
The syntax tree is [hast][github-hast].
This package can be used with JSX.
You should use the automatic JSX runtime set to hastscript orhastscript/svg.
> 👉 Note
> while h supports dots (.) for classes or number signs (#)selector
> for IDs in ,
> those are not supported in JSX.
> 🪦 Legacy:
> you can also use the classic JSX runtime,
> but this is not recommended.
> To do so,
> import h (or s) yourself and define it as the pragmanull
> (plus set the fragment to ).
The Use example above can then be written like so,
using inline pragmas,
so that SVG can be used too:
example-html.jsx:
`js`
/* @jsxImportSource hastscript /
console.log(
)
example-svg.jsx:
`js<circle>
/* @jsxImportSource hastscript/svg /
console.log(
element`
)
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release,
we drop support for unmaintained versions of Node.
This means we try to keep the current release line,
hastscript@9,
compatible with Node.js 16.
Use of hastscript can open you up to a
[cross-site scripting (XSS)][wikipedia-xss]
when you pass user-provided input to it because values are injected into the
syntax tree.
The following example shows how an image is injected that fails loading and
therefore runs code in a browser.
`js
const tree = h()
// Somehow someone injected these properties instead of an expected src andalt
// :
const otherProps = {onError: 'alert(1)', src: 'x'}
tree.children.push(h('img', {src: 'default.png', ...otherProps}))
`
Yields:
`html`
The following example shows how code can run in a browser because someone stored
an object in a database instead of the expected string.
`js
const tree = h()
// Somehow this isn’t the expected 'wooorm'.
const username = {
type: 'element',
tagName: 'script',
children: [{type: 'text', value: 'alert(2)'}]
}
tree.children.push(h('span.handle', username))
`
Yields:
`html`
Either do not use user-provided input in hastscript or usehast-util-santize
[][github-hast-util-sanitize].
* [unist-builder][github-unist-builder]xastscript
— create unist trees
* [][github-xastscript]hast-to-hyperscript
— create xast trees
* hast-util-to-html
— turn hast into React, Preact, Vue, etc
* hast-util-to-dom
— turn hast into HTML
* estree-util-build-jsx
— turn hast into DOM trees
*
— compile JSX away
See
[contributing.md][health-contributing]syntax-tree/.github
in
[][health]support.md`][health-support] for ways to get help.
for ways to get started.
See [
This project has a [code of conduct][health-coc].
By interacting with this repository,
organization,
or community you agree to abide by its terms.
[MIT][file-license] © [Titus Wormer][wooorm]
[api-child]: #child
[api-h]: #hselector-properties-children
[api-properties]: #properties-1
[api-result]: #result
[api-s]: #sselector-properties-children
[badge-build-image]: https://github.com/syntax-tree/hastscript/workflows/main/badge.svg
[badge-build-url]: https://github.com/syntax-tree/hastscript/actions
[badge-coverage-image]: https://img.shields.io/codecov/c/github/syntax-tree/hastscript.svg
[badge-coverage-url]: https://codecov.io/github/syntax-tree/hastscript
[badge-downloads-image]: https://img.shields.io/npm/dm/hastscript.svg
[badge-downloads-url]: https://www.npmjs.com/package/hastscript
[badge-size-image]: https://img.shields.io/bundlejs/size/hastscript
[badge-size-url]: https://bundlejs.com/?q=hastscript
[esmsh]: https://esm.sh
[file-license]: license
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[github-hast]: https://github.com/syntax-tree/hast
[github-hast-element]: https://github.com/syntax-tree/hast#element
[github-hast-root]: https://github.com/syntax-tree/hast#root
[github-hast-text]: https://github.com/syntax-tree/hast#text
[github-hast-util-parse-selector]: https://github.com/syntax-tree/hast-util-parse-selector
[github-hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
[github-unist-builder]: https://github.com/syntax-tree/unist-builder
[github-xastscript]: https://github.com/syntax-tree/xastscript
[health]: https://github.com/syntax-tree/.github
[health-coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
[health-contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
[health-support]: https://github.com/syntax-tree/.github/blob/main/support.md
[npmjs-install]: https://docs.npmjs.com/cli/install
[typescript]: https://www.typescriptlang.org
[wikipedia-xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
[wooorm]: https://wooorm.com