Gathering detailed insights and metrics for hastscript-ns
Gathering detailed insights and metrics for hastscript-ns
Gathering detailed insights and metrics for hastscript-ns
Gathering detailed insights and metrics for hastscript-ns
npm install hastscript-ns
Typescript
Module System
Node Version
NPM Version
JavaScript (86.87%)
TypeScript (13.13%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
110 Commits
1 Branches
1 Contributors
Updated on Jan 07, 2022
Latest Version
7.0.3
Package Id
hastscript-ns@7.0.3
Unpacked Size
37.38 kB
Size
10.63 kB
File Count
35
NPM Version
7.24.1
Node Version
14.15.4
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
5
A fork of the original hastscript npm package. This one does not lowercase html tagNames. SVG tagnames are still lowercased (or normalized).
hast utility to create trees in HTML or SVG.
Similar to hyperscript
, virtual-dom/h
,
React.createElement
, and Vue’s createElement
,
but for hast.
Use unist-builder
to create any unist tree.
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
1npm install hastscript-ns
1import {h, s} from 'hastscript' 2 3// Children as an array: 4console.log( 5 h('.foo#some-id', [ 6 h('span', 'some text'), 7 h('input', {type: 'text', value: 'foo'}), 8 h('a.alpha', {class: 'bravo charlie', download: 'download'}, [ 9 'delta', 10 'echo' 11 ]) 12 ]) 13) 14 15// Children as arguments: 16console.log( 17 h( 18 'form', 19 {method: 'POST'}, 20 h('input', {type: 'text', name: 'foo'}), 21 h('input', {type: 'text', name: 'bar'}), 22 h('input', {type: 'submit', value: 'send'}) 23 ) 24) 25 26// SVG: 27console.log( 28 s('svg', {xmlns: 'http://www.w3.org/2000/svg', viewbox: '0 0 500 500'}, [ 29 s('title', 'SVG `<circle>` element'), 30 s('circle', {cx: 120, cy: 120, r: 100}) 31 ]) 32)
Yields:
1{ 2 type: 'element', 3 tagName: 'div', 4 properties: {className: ['foo'], id: 'some-id'}, 5 children: [ 6 { 7 type: 'element', 8 tagName: 'span', 9 properties: {}, 10 children: [{type: 'text', value: 'some text'}] 11 }, 12 { 13 type: 'element', 14 tagName: 'input', 15 properties: {type: 'text', value: 'foo'}, 16 children: [] 17 }, 18 { 19 type: 'element', 20 tagName: 'a', 21 properties: {className: ['alpha', 'bravo', 'charlie'], download: true}, 22 children: [{type: 'text', value: 'delta'}, {type: 'text', value: 'echo'}] 23 } 24 ] 25} 26{ 27 type: 'element', 28 tagName: 'form', 29 properties: {method: 'POST'}, 30 children: [ 31 { 32 type: 'element', 33 tagName: 'input', 34 properties: {type: 'text', name: 'foo'}, 35 children: [] 36 }, 37 { 38 type: 'element', 39 tagName: 'input', 40 properties: {type: 'text', name: 'bar'}, 41 children: [] 42 }, 43 { 44 type: 'element', 45 tagName: 'input', 46 properties: {type: 'submit', value: 'send'}, 47 children: [] 48 } 49 ] 50} 51{ 52 type: 'element', 53 tagName: 'svg', 54 properties: {xmlns: 'http://www.w3.org/2000/svg', viewBox: '0 0 500 500'}, 55 children: [ 56 { 57 type: 'element', 58 tagName: 'title', 59 properties: {}, 60 children: [{type: 'text', value: 'SVG `<circle>` element'}] 61 }, 62 { 63 type: 'element', 64 tagName: 'circle', 65 properties: {cx: 120, cy: 120, r: 100}, 66 children: [] 67 } 68 ] 69}
This package exports the following identifiers: h
and s
.
There is no default export.
h(selector?[, properties][, …children])
s(selector?[, properties][, …children])
Create virtual hast trees for HTML or SVG.
h(): root
h(null[, …children]): root
h(name[, properties][, …children]): element
(and the same for s
).
selector
Simple CSS selector (string
, optional).
Can contain a tag name (foo
), IDs (#bar
), and classes (.baz
).
If the selector is a string but there is no tag name in it, h
defaults to
build a div
element, and s
to a g
element.
selector
is parsed by hast-util-parse-selector
.
When string, builds an Element
.
When nullish, builds a Root
instead.
properties
Map of properties (Object.<*>
, optional).
Keys should match either the HTML attribute name, or the DOM property name, but
are case-insensitive.
Cannot be given when building a Root
.
children
(Lists of) children (string
, number
, Node
, Array.<children>
, optional).
When strings or numbers are encountered, they are mapped to Text
nodes.
If Root
nodes are given, their children are used instead.
hastscript
can be used with JSX.
Either use the automatic runtime set to hastscript/html
, hastscript/svg
,
or hastscript
(shortcut for HTML).
Or import h
or s
yourself and define it as the pragma (plus set the fragment
to null
).
The example above can then be written like so, using inline pragmas, so that SVG can be used too:
example-html.jsx
:
1/** @jsxImportSource hastscript */ 2 3console.log( 4 <div class="foo" id="some-id"> 5 <span>some text</span> 6 <input type="text" value="foo" /> 7 <a class="alpha bravo charlie" download> 8 deltaecho 9 </a> 10 </div> 11) 12 13console.log( 14 <form method="POST"> 15 <input type="text" name="foo" /> 16 <input type="text" name="bar" /> 17 <input type="submit" name="send" /> 18 </form> 19)
example-svg.jsx
:
1/** @jsxImportSource hastscript/svg */ 2console.log( 3 <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 500 500"> 4 <title>SVG `<circle>` element</title> 5 <circle cx={120} cy={120} r={100} /> 6 </svg> 7)
Because JSX does not allow dots (.
) or number signs (#
) in tag names, you
have to pass class names and IDs in as attributes.
You can use estree-util-build-jsx
to compile JSX away.
You could also use bublé, but it’s not ideal (jsxFragment
is currently
only available on the API, not the CLI, and it only allows a single pragma).
For Babel, use @babel/plugin-transform-react-jsx
and either
pass pragma: 'h'
and pragmaFrag: 'null'
, or pass importSource: 'hastscript'
.
This is less ideal because it allows a single pragma.
Babel also lets you configure this in a script:
1/** @jsx s @jsxFrag null */ 2import {s} from 'hastscript' 3 4console.log(<rect />)
This is useful because it allows using both html
and svg
, although in
different files.
Use of hastscript
can open you up to a cross-site scripting (XSS)
attack as values are injected into the syntax tree.
The following example shows how a script is injected that runs when loaded in a
browser.
1const tree = {type: 'root', children: []} 2 3tree.children.push(h('script', 'alert(1)'))
Yields:
1<script>alert(1)</script>
The following example shows how an image is injected that fails loading and therefore runs code in a browser.
1const tree = {type: 'root', children: []} 2 3// Somehow someone injected these properties instead of an expected `src` and 4// `alt`: 5const otherProps = {src: 'x', onError: 'alert(2)'} 6 7tree.children.push(h('img', {src: 'default.png', ...otherProps}))
Yields:
1<img src="x" onerror="alert(2)">
The following example shows how code can run in a browser because someone stored an object in a database instead of the expected string.
1const tree = {type: 'root', children: []} 2 3// Somehow this isn’t the expected `'wooorm'`. 4const username = { 5 type: 'element', 6 tagName: 'script', 7 children: [{type: 'text', value: 'alert(3)'}] 8} 9 10tree.children.push(h('span.handle', username))
Yields:
1<span class="handle"><script>alert(3)</script></span>
Either do not use user input in hastscript
or use
hast-util-santize
.
unist-builder
— Create any unist treexastscript
— Create a xast treehast-to-hyperscript
— Convert a Node to React, Virtual DOM, Hyperscript, and morehast-util-from-dom
— Transform a DOM tree to hasthast-util-select
— querySelector
, querySelectorAll
, and matches
hast-util-to-html
— Stringify nodes to HTMLhast-util-to-dom
— Transform to a DOM treeSee 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.
No vulnerabilities found.
No security vulnerabilities found.