hastscript
Advanced tools
Comparing version 7.1.0 to 7.2.0
@@ -21,16 +21,37 @@ /** | ||
export type Root = import('hast').Root | ||
export type Content = import('hast').Content | ||
export type Element = import('hast').Element | ||
export type Properties = import('hast').Properties | ||
export type Child = Root['children'][number] | ||
export type Node = Child | Root | ||
export type Info = import('property-information').Info | ||
export type Schema = import('property-information').Schema | ||
/** | ||
* Any concrete `hast` node. | ||
*/ | ||
export type Node = Content | Root | ||
/** | ||
* Result from a `h` (or `s`) call. | ||
*/ | ||
export type HResult = Root | Element | ||
/** | ||
* Value for a CSS style field. | ||
*/ | ||
export type HStyleValue = string | number | ||
/** | ||
* Supported value of a `style` prop. | ||
*/ | ||
export type HStyle = Record<string, HStyleValue> | ||
/** | ||
* Primitive property value. | ||
*/ | ||
export type HPrimitiveValue = string | number | boolean | null | undefined | ||
/** | ||
* List of property values for space- or comma separated values (such as `className`). | ||
*/ | ||
export type HArrayValue = Array<string | number> | ||
/** | ||
* Primitive value or list value. | ||
*/ | ||
export type HPropertyValue = HPrimitiveValue | (string | number)[] | ||
/** | ||
* Acceptable properties value. | ||
* Acceptable value for element properties. | ||
*/ | ||
@@ -40,7 +61,13 @@ export type HProperties = { | ||
} | ||
/** | ||
* Primitive children, either ignored (nullish), or turned into text nodes. | ||
*/ | ||
export type HPrimitiveChild = string | number | null | undefined | ||
/** | ||
* List of children. | ||
*/ | ||
export type HArrayChild = Array<Node | HPrimitiveChild> | ||
/** | ||
* Acceptable child value | ||
* Acceptable child value. | ||
*/ | ||
export type HChild = Node | HPrimitiveChild | (Node | HPrimitiveChild)[] |
/** | ||
* @typedef {import('hast').Root} Root | ||
* @typedef {import('hast').Content} Content | ||
* @typedef {import('hast').Element} Element | ||
* @typedef {import('hast').Properties} Properties | ||
* @typedef {Root['children'][number]} Child | ||
* @typedef {Child|Root} Node | ||
* @typedef {import('property-information').Info} Info | ||
* @typedef {import('property-information').Schema} Schema | ||
*/ | ||
/** | ||
* @typedef {Content | Root} Node | ||
* Any concrete `hast` node. | ||
* @typedef {Root | Element} HResult | ||
* Result from a `h` (or `s`) call. | ||
* | ||
* @typedef {Root|Element} HResult | ||
* @typedef {string|number} HStyleValue | ||
* @typedef {string | number} HStyleValue | ||
* Value for a CSS style field. | ||
* @typedef {Record<string, HStyleValue>} HStyle | ||
* @typedef {string|number|boolean|null|undefined} HPrimitiveValue | ||
* @typedef {Array<string|number>} HArrayValue | ||
* @typedef {HPrimitiveValue|HArrayValue} HPropertyValue | ||
* @typedef {{[property: string]: HPropertyValue|HStyle}} HProperties | ||
* Acceptable properties value. | ||
* Supported value of a `style` prop. | ||
* @typedef {string | number | boolean | null | undefined} HPrimitiveValue | ||
* Primitive property value. | ||
* @typedef {Array<string | number>} HArrayValue | ||
* List of property values for space- or comma separated values (such as `className`). | ||
* @typedef {HPrimitiveValue | HArrayValue} HPropertyValue | ||
* Primitive value or list value. | ||
* @typedef {{[property: string]: HPropertyValue | HStyle}} HProperties | ||
* Acceptable value for element properties. | ||
* | ||
* @typedef {string|number|null|undefined} HPrimitiveChild | ||
* @typedef {Array<Node|HPrimitiveChild>} HArrayChild | ||
* @typedef {Node|HPrimitiveChild|HArrayChild} HChild | ||
* Acceptable child value | ||
* @typedef {string | number | null | undefined} HPrimitiveChild | ||
* Primitive children, either ignored (nullish), or turned into text nodes. | ||
* @typedef {Array<Node | HPrimitiveChild>} HArrayChild | ||
* List of children. | ||
* @typedef {Node | HPrimitiveChild | HArrayChild} HChild | ||
* Acceptable child value. | ||
*/ | ||
@@ -46,3 +58,3 @@ | ||
* (): Root | ||
* (selector: null|undefined, ...children: Array<HChild>): Root | ||
* (selector: null | undefined, ...children: Array<HChild>): Root | ||
* (selector: string, properties?: HProperties, ...children: Array<HChild>): Element | ||
@@ -56,4 +68,4 @@ * (selector: string, ...children: Array<HChild>): Element | ||
* | ||
* @param {string|null} [selector] | ||
* @param {HProperties|HChild} [properties] | ||
* @param {string | null} [selector] | ||
* @param {HProperties | HChild} [properties] | ||
* @param {Array<HChild>} children | ||
@@ -113,3 +125,3 @@ * @returns {HResult} | ||
/** | ||
* @param {HProperties|HChild} value | ||
* @param {HProperties | HChild} value | ||
* @param {string} name | ||
@@ -147,3 +159,3 @@ * @returns {value is HProperties} | ||
* @param {string} key | ||
* @param {HStyle|HPropertyValue} value | ||
* @param {HStyle | HPropertyValue} value | ||
* @returns {void} | ||
@@ -188,3 +200,3 @@ */ | ||
if (Array.isArray(result)) { | ||
/** @type {Array<string|number>} */ | ||
/** @type {Array<string | number>} */ | ||
const finalResult = [] | ||
@@ -210,3 +222,3 @@ | ||
/** | ||
* @param {Array<Child>} nodes | ||
* @param {Array<Content>} nodes | ||
* @param {HChild} value | ||
@@ -263,4 +275,8 @@ * @returns {void} | ||
/** | ||
* Serialize a `style` object as a string. | ||
* | ||
* @param {HStyle} value | ||
* Style object. | ||
* @returns {string} | ||
* CSS string. | ||
*/ | ||
@@ -283,4 +299,8 @@ function style(value) { | ||
/** | ||
* Create a map to adjust casing. | ||
* | ||
* @param {Array<string>} values | ||
* List of properly cased keys. | ||
* @returns {Record<string, string>} | ||
* Map of lowercase keys to uppercase keys. | ||
*/ | ||
@@ -287,0 +307,0 @@ function createAdjustMap(values) { |
@@ -19,16 +19,20 @@ export const h: { | ||
namespace JSX { | ||
type Element = import('./jsx-classic').Element | ||
type IntrinsicAttributes = import('./jsx-classic').IntrinsicAttributes | ||
type IntrinsicElements = import('./jsx-classic').IntrinsicElements | ||
type Element = import('./jsx-classic.js').Element | ||
type IntrinsicAttributes = import('./jsx-classic.js').IntrinsicAttributes | ||
type IntrinsicElements = import('./jsx-classic.js').IntrinsicElements | ||
type ElementChildrenAttribute = | ||
import('./jsx-classic').ElementChildrenAttribute | ||
import('./jsx-classic.js').ElementChildrenAttribute | ||
} | ||
} | ||
/** | ||
* Acceptable child value | ||
* Acceptable child value. | ||
*/ | ||
export type Child = import('./core.js').HChild | ||
/** | ||
* Acceptable properties value. | ||
* Acceptable value for element properties. | ||
*/ | ||
export type Properties = import('./core.js').HProperties | ||
/** | ||
* Result from a `h` (or `s`) call. | ||
*/ | ||
export type Result = import('./core.js').HResult |
/** | ||
* @typedef {import('./core.js').HChild} Child Acceptable child value | ||
* @typedef {import('./core.js').HProperties} Properties Acceptable properties value. | ||
* @typedef {import('./core.js').HChild} Child | ||
* Acceptable child value. | ||
* @typedef {import('./core.js').HProperties} Properties | ||
* Acceptable value for element properties. | ||
* @typedef {import('./core.js').HResult} Result | ||
* Result from a `h` (or `s`) call. | ||
* | ||
* @typedef {import('./jsx-classic').Element} h.JSX.Element | ||
* @typedef {import('./jsx-classic').IntrinsicAttributes} h.JSX.IntrinsicAttributes | ||
* @typedef {import('./jsx-classic').IntrinsicElements} h.JSX.IntrinsicElements | ||
* @typedef {import('./jsx-classic').ElementChildrenAttribute} h.JSX.ElementChildrenAttribute | ||
* @typedef {import('./jsx-classic.js').Element} h.JSX.Element | ||
* @typedef {import('./jsx-classic.js').IntrinsicAttributes} h.JSX.IntrinsicAttributes | ||
* @typedef {import('./jsx-classic.js').IntrinsicElements} h.JSX.IntrinsicElements | ||
* @typedef {import('./jsx-classic.js').ElementChildrenAttribute} h.JSX.ElementChildrenAttribute | ||
*/ | ||
@@ -10,0 +14,0 @@ |
export {h} from './html.js' | ||
export {s} from './svg.js' | ||
/** | ||
* Acceptable child value. | ||
*/ | ||
export type Child = import('./core.js').HChild | ||
/** | ||
* Acceptable value for element properties. | ||
*/ | ||
export type Properties = import('./core.js').HProperties | ||
/** | ||
* Result from a `h` (or `s`) call. | ||
*/ | ||
export type Result = import('./core.js').HResult |
/** | ||
* @typedef {import('./core.js').HChild} Child | ||
* Acceptable child value. | ||
* @typedef {import('./core.js').HProperties} Properties | ||
* Acceptable value for element properties. | ||
* @typedef {import('./core.js').HResult} Result | ||
* Result from a `h` (or `s`) call. | ||
*/ | ||
@@ -6,0 +9,0 @@ |
@@ -19,5 +19,5 @@ export * from './jsx-automatic.js' | ||
| import('hast').Root | ||
| import('hast').Element | ||
| import('hast').Comment | ||
| import('hast').DocType | ||
| import('hast').Element | ||
| import('hast').Text | ||
@@ -49,5 +49,34 @@ | import('./core.js').HStyle | ||
| import('hast').Root | ||
| import('hast').Comment | ||
| import('hast').DocType | ||
| import('hast').Element | ||
| import('hast').Text | ||
| import('./core.js').HStyle | ||
| import('./core.js').HArrayValue | ||
| import('./core.js').HArrayChild | ||
| null | ||
| undefined | ||
>, | ||
key?: string | undefined | ||
): import('hast').Element | ||
} | ||
export const jsxDEV: { | ||
( | ||
type: null | undefined, | ||
props: { | ||
children?: import('./core.js').HChild | ||
}, | ||
key?: string | undefined | ||
): import('hast').Root | ||
( | ||
type: string, | ||
props: Record< | ||
string, | ||
| string | ||
| number | ||
| boolean | ||
| import('hast').Root | ||
| import('hast').Comment | ||
| import('hast').DocType | ||
| import('hast').Element | ||
| import('hast').Text | ||
@@ -54,0 +83,0 @@ | import('./core.js').HStyle |
@@ -6,2 +6,2 @@ // Export `JSX` as a global for TypeScript. | ||
export * from './jsx-automatic.js' | ||
export const {Fragment, jsx, jsxs} = runtime(h) | ||
export const {Fragment, jsx, jsxs, jsxDEV} = runtime(h) |
@@ -19,5 +19,5 @@ export * from './jsx-automatic.js' | ||
| import('hast').Root | ||
| import('hast').Element | ||
| import('hast').Comment | ||
| import('hast').DocType | ||
| import('hast').Element | ||
| import('hast').Text | ||
@@ -49,5 +49,34 @@ | import('./core.js').HStyle | ||
| import('hast').Root | ||
| import('hast').Comment | ||
| import('hast').DocType | ||
| import('hast').Element | ||
| import('hast').Text | ||
| import('./core.js').HStyle | ||
| import('./core.js').HArrayValue | ||
| import('./core.js').HArrayChild | ||
| null | ||
| undefined | ||
>, | ||
key?: string | undefined | ||
): import('hast').Element | ||
} | ||
export const jsxDEV: { | ||
( | ||
type: null | undefined, | ||
props: { | ||
children?: import('./core.js').HChild | ||
}, | ||
key?: string | undefined | ||
): import('hast').Root | ||
( | ||
type: string, | ||
props: Record< | ||
string, | ||
| string | ||
| number | ||
| boolean | ||
| import('hast').Root | ||
| import('hast').Comment | ||
| import('hast').DocType | ||
| import('hast').Element | ||
| import('hast').Text | ||
@@ -54,0 +83,0 @@ | import('./core.js').HStyle |
@@ -6,2 +6,2 @@ // Export `JSX` as a global for TypeScript. | ||
export * from './jsx-automatic.js' | ||
export const {Fragment, jsx, jsxs} = runtime(s) | ||
export const {Fragment, jsx, jsxs, jsxDEV} = runtime(s) |
@@ -11,5 +11,7 @@ /** | ||
* | ||
* @typedef {Record<string, HPropertyValue|HStyle|HChild>} JSXProps | ||
* @typedef {Record<string, HPropertyValue | HStyle | HChild>} JSXProps | ||
*/ | ||
/** | ||
* Create an automatic runtime. | ||
* | ||
* @param {ReturnType<Core>} f | ||
@@ -39,2 +41,12 @@ */ | ||
} | ||
jsxDEV: { | ||
( | ||
type: null | undefined, | ||
props: { | ||
children?: HChild | ||
}, | ||
key?: string | ||
): Root | ||
(type: string, props: JSXProps, key?: string): Element | ||
} | ||
} | ||
@@ -41,0 +53,0 @@ export type Element = import('./core.js').Element |
@@ -11,6 +11,8 @@ /** | ||
* | ||
* @typedef {Record<string, HPropertyValue|HStyle|HChild>} JSXProps | ||
* @typedef {Record<string, HPropertyValue | HStyle | HChild>} JSXProps | ||
*/ | ||
/** | ||
* Create an automatic runtime. | ||
* | ||
* @param {ReturnType<Core>} f | ||
@@ -22,3 +24,3 @@ */ | ||
* @type {{ | ||
* (type: null|undefined, props: {children?: HChild}, key?: string): Root | ||
* (type: null | undefined, props: {children?: HChild}, key?: string): Root | ||
* (type: string, props: JSXProps, key?: string): Element | ||
@@ -29,3 +31,3 @@ * }} | ||
/** | ||
* @param {string|null} type | ||
* @param {string | null} type | ||
* @param {HProperties & {children?: HChild}} props | ||
@@ -40,3 +42,3 @@ * @returns {HResult} | ||
return {Fragment: null, jsx, jsxs: jsx} | ||
return {Fragment: null, jsx, jsxs: jsx, jsxDEV: jsx} | ||
} |
@@ -19,10 +19,20 @@ export const s: { | ||
namespace JSX { | ||
type Element = import('./jsx-classic').Element | ||
type IntrinsicAttributes = import('./jsx-classic').IntrinsicAttributes | ||
type IntrinsicElements = import('./jsx-classic').IntrinsicElements | ||
type Element = import('./jsx-classic.js').Element | ||
type IntrinsicAttributes = import('./jsx-classic.js').IntrinsicAttributes | ||
type IntrinsicElements = import('./jsx-classic.js').IntrinsicElements | ||
type ElementChildrenAttribute = | ||
import('./jsx-classic').ElementChildrenAttribute | ||
import('./jsx-classic.js').ElementChildrenAttribute | ||
} | ||
} | ||
/** | ||
* Acceptable child value. | ||
*/ | ||
export type Child = import('./core.js').HChild | ||
/** | ||
* Acceptable value for element properties. | ||
*/ | ||
export type Properties = import('./core.js').HProperties | ||
/** | ||
* Result from a `h` (or `s`) call. | ||
*/ | ||
export type Result = import('./core.js').HResult |
/** | ||
* @typedef {import('./core.js').HChild} Child | ||
* Acceptable child value. | ||
* @typedef {import('./core.js').HProperties} Properties | ||
* Acceptable value for element properties. | ||
* @typedef {import('./core.js').HResult} Result | ||
* Result from a `h` (or `s`) call. | ||
* | ||
* @typedef {import('./jsx-classic').Element} s.JSX.Element | ||
* @typedef {import('./jsx-classic').IntrinsicAttributes} s.JSX.IntrinsicAttributes | ||
* @typedef {import('./jsx-classic').IntrinsicElements} s.JSX.IntrinsicElements | ||
* @typedef {import('./jsx-classic').ElementChildrenAttribute} s.JSX.ElementChildrenAttribute | ||
* @typedef {import('./jsx-classic.js').Element} s.JSX.Element | ||
* @typedef {import('./jsx-classic.js').IntrinsicAttributes} s.JSX.IntrinsicAttributes | ||
* @typedef {import('./jsx-classic.js').IntrinsicElements} s.JSX.IntrinsicElements | ||
* @typedef {import('./jsx-classic.js').ElementChildrenAttribute} s.JSX.ElementChildrenAttribute | ||
*/ | ||
@@ -10,0 +14,0 @@ |
{ | ||
"name": "hastscript", | ||
"version": "7.1.0", | ||
"version": "7.2.0", | ||
"description": "hast utility to create trees", | ||
@@ -40,4 +40,7 @@ "license": "MIT", | ||
"./jsx-runtime": "./jsx-runtime.js", | ||
"./jsx-dev-runtime": "./jsx-runtime.js", | ||
"./html/jsx-runtime": "./html/jsx-runtime.js", | ||
"./svg/jsx-runtime": "./svg/jsx-runtime.js" | ||
"./html/jsx-dev-runtime": "./html/jsx-runtime.js", | ||
"./svg/jsx-runtime": "./svg/jsx-runtime.js", | ||
"./svg/jsx-dev-runtime": "./svg/jsx-runtime.js" | ||
}, | ||
@@ -65,28 +68,22 @@ "files": [ | ||
"devDependencies": { | ||
"@babel/core": "^7.0.0", | ||
"@babel/plugin-syntax-jsx": "^7.0.0", | ||
"@babel/plugin-transform-react-jsx": "^7.0.0", | ||
"@types/babel__core": "^7.0.0", | ||
"@types/tape": "^4.0.0", | ||
"acorn": "^8.0.0", | ||
"@types/node": "^18.0.0", | ||
"acorn-jsx": "^5.0.0", | ||
"astring": "^1.0.0", | ||
"c8": "^7.0.0", | ||
"esast-util-from-js": "^1.0.0", | ||
"estree-util-build-jsx": "^2.0.0", | ||
"estree-util-to-js": "^1.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"svg-tag-names": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tsd": "^0.24.0", | ||
"tsd": "^0.25.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unist-builder": "^3.0.0", | ||
"xo": "^0.52.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"{script/**,test/**,}*.d.ts\" \"lib/{core,html,index,runtime-html,runtime-svg,runtime,svg-case-sensitive-tag-names,svg}.d.ts\" && tsc && tsd && type-coverage", | ||
"generate": "node script/generate-jsx && node script/build", | ||
"build": "tsc --build --clean && tsc --build && tsd && type-coverage", | ||
"generate": "node script/generate-jsx.js && node script/build.js", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
@@ -93,0 +90,0 @@ "test-api": "node --conditions development test/index.js", |
179
readme.md
@@ -22,2 +22,6 @@ # hastscript | ||
* [`s(selector?[, properties][, …children])`](#sselector-properties-children) | ||
* [`Child`](#child) | ||
* [`Properties`](#properties-1) | ||
* [`Result`](#result) | ||
* [Syntax tree](#syntax-tree) | ||
* [JSX](#jsx) | ||
@@ -51,3 +55,3 @@ * [Types](#types) | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: | ||
In Node.js (version 14.14+ or 16.0+), install with [npm][]: | ||
@@ -151,11 +155,9 @@ ```sh | ||
The export map supports the automatic JSX runtime. | ||
You can pass `hastscript/html` (or `hastscript`) or `hastscript/svg` to your | ||
build tool (TypeScript, Babel, SWC) as with an `importSource` option or similar. | ||
You can pass `hastscript` (or `hastscript/html`) or `hastscript/svg` to your | ||
build tool (TypeScript, Babel, SWC) with an `importSource` option or similar. | ||
### `h(selector?[, properties][, …children])` | ||
### `s(selector?[, properties][, …children])` | ||
Create virtual **[hast][]** trees for HTML. | ||
Create virtual [**hast**][hast] [*trees*][tree] for HTML or SVG. | ||
##### Signatures | ||
@@ -167,4 +169,2 @@ | ||
(and the same for `s`). | ||
##### Parameters | ||
@@ -184,26 +184,93 @@ | ||
Map of properties (`Record<string, any>`, 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`][root]. | ||
Properties of the element ([`Properties`][properties], optional). | ||
###### `children` | ||
(Lists of) children (`string`, `number`, `Node`, `Array<children>`, optional). | ||
When strings or numbers are encountered, they are mapped to [`Text`][text] | ||
nodes. | ||
If [`Root`][root] nodes are given, their children are used instead. | ||
Children of the element ([`Child`][child] or `Array<Child>`, optional). | ||
##### Returns | ||
[`Element`][element] or [`Root`][root]. | ||
Created tree ([`Result`][result]). | ||
[`Element`][element] when a `selector` is passed, otherwise [`Root`][root]. | ||
### `s(selector?[, properties][, …children])` | ||
Create virtual **[hast][]** trees for SVG. | ||
Signatures, parameters, and return value are the same as `h` above. | ||
Importantly, the `selector` and `properties` parameters are interpreted as | ||
SVG. | ||
### `Child` | ||
(Lists of) children (TypeScript type). | ||
When strings or numbers are encountered, they are turned into [`Text`][text] | ||
nodes. | ||
[`Root`][root] nodes are treated as “fragments”, meaning that their children | ||
are used instead. | ||
###### Type | ||
```ts | ||
type Child = | ||
| string | ||
| number | ||
| null | ||
| undefined | ||
| Node | ||
| Array<string | number | null | undefined | Node> | ||
``` | ||
### `Properties` | ||
Map of properties (TypeScript type). | ||
Keys should match either the HTML attribute name, or the DOM property name, but | ||
are case-insensitive. | ||
###### Type | ||
```ts | ||
type Properties = Record< | ||
string, | ||
| string | ||
| number | ||
| boolean | ||
| null | ||
| undefined | ||
// For comma- and space-separated values such as `className`: | ||
| Array<string | number> | ||
// Accepts value for `style` prop as object. | ||
| Record<string, string | number> | ||
> | ||
``` | ||
### `Result` | ||
Result from a `h` (or `s`) call (TypeScript type). | ||
###### Type | ||
```ts | ||
type Result = Root | Element | ||
``` | ||
## Syntax tree | ||
The syntax tree is [hast][]. | ||
## JSX | ||
`hastscript` can be used with JSX. | ||
Either use the automatic runtime set to `hastscript/html` (or `hastscript`) or | ||
`hastscript/svg` or import `h` or `s` yourself and define it as the pragma (plus | ||
set the fragment to `null`). | ||
This package can be used with JSX. | ||
You should use the automatic JSX runtime set to `hastscript` (also available as | ||
the more explicit name `hastscript/html`) or `hastscript/svg`. | ||
The example above can then be written like so, using inline pragmas, so | ||
> 👉 **Note**: while `h` supports dots (`.`) for classes or number signs (`#`) | ||
> for IDs in `selector`, 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 pragma (plus | ||
> set the fragment to `null`). | ||
The Use example above can then be written like so, using inline pragmas, so | ||
that SVG can be used too: | ||
@@ -238,32 +305,7 @@ | ||
> 👉 **Note**: while `h` supports dots (`.`) for classes or number signs (`#`) | ||
> for IDs in `selector`, those are not supported in JSX. | ||
You can use [`estree-util-build-jsx`][build-jsx] to compile JSX away. | ||
For [Babel][], use [`@babel/plugin-transform-react-jsx`][babel-jsx] and either | ||
pass `pragma: 'h'` and `pragmaFrag: 'null'`, or pass `importSource: | ||
'hastscript'`. | ||
This is not perfect as it allows only a single pragma. | ||
Alternatively, Babel also lets you configure this with a comment: | ||
```jsx | ||
/** @jsx s @jsxFrag null */ | ||
import {s} from 'hastscript' | ||
console.log(<rect />) | ||
``` | ||
This is useful because it allows using *both* `html` and `svg` when used in | ||
different files. | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types: | ||
It exports the additional types `Child`, `Properties`, and `Result`. | ||
* `Child` — valid value used as a child | ||
* `Properties` — valid properties passed to an element | ||
* `Result` — output of a `h` (or `s`) call | ||
## Compatibility | ||
@@ -273,3 +315,3 @@ | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, and 16.0+. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
@@ -280,18 +322,5 @@ | ||
Use of `hastscript` can open you up to a [cross-site scripting (XSS)][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. | ||
when you pass user-provided input to it because values are injected into the | ||
syntax tree. | ||
```js | ||
const tree = h() | ||
tree.children.push(h('script', 'alert(1)')) | ||
``` | ||
Yields: | ||
```html | ||
<script>alert(1)</script> | ||
``` | ||
The following example shows how an image is injected that fails loading and | ||
@@ -338,3 +367,3 @@ therefore runs code in a browser. | ||
Either do not use user input in `hastscript` or use | ||
Either do not use user-provided input in `hastscript` or use | ||
[`hast-util-santize`][hast-util-sanitize]. | ||
@@ -350,6 +379,2 @@ | ||
— turn hast into React, Preact, Vue, etc | ||
* [`hast-util-from-dom`](https://github.com/syntax-tree/hast-util-from-dom) | ||
— turn DOM trees into hast | ||
* [`hast-util-select`](https://github.com/syntax-tree/hast-util-select) | ||
— `querySelector`, `querySelectorAll`, and `matches` | ||
* [`hast-util-to-html`](https://github.com/syntax-tree/hast-util-to-html) | ||
@@ -359,2 +384,4 @@ — turn hast into HTML | ||
— turn hast into DOM trees | ||
* [`estree-util-build-jsx`](https://github.com/syntax-tree/estree-util-build-jsx) | ||
— compile JSX away | ||
@@ -423,4 +450,2 @@ ## Contribute | ||
[tree]: https://github.com/syntax-tree/unist#tree | ||
[hast]: https://github.com/syntax-tree/hast | ||
@@ -438,12 +463,12 @@ | ||
[build-jsx]: https://github.com/wooorm/estree-util-build-jsx | ||
[parse-selector]: https://github.com/syntax-tree/hast-util-parse-selector | ||
[babel]: https://github.com/babel/babel | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting | ||
[babel-jsx]: https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-react-jsx | ||
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize | ||
[parse-selector]: https://github.com/syntax-tree/hast-util-parse-selector | ||
[child]: #child | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting | ||
[properties]: #properties-1 | ||
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize | ||
[result]: #result |
41979
15
926
462