@@ -138,34 +138,124 @@ Yields:
138138
139139## API
140140
141- ### ` h(selector?[, properties][, ... children]) `
141+ ### ` h(selector?[, properties][, … children]) `
142142
143- ### ` s(selector?[, properties][, ... children]) `
143+ ### ` s(selector?[, properties][, … children]) `
144144
145- DSL to create virtual [ ** hast** ] [ hast ] [ * trees* ] [ tree ] for HTML or SVG.
145+ Create virtual [ ** hast** ] [ hast ] [ * trees* ] [ tree ] for HTML or SVG.
146+
147+ ##### Signatures
148+
149+ * ` h(): root `
150+ * ` h(null[, …children]): root `
151+ * ` h(name[, properties][, …children]): element `
152+
153+ (and the same for ` s ` ).
146154
147155##### Parameters
148156
149157###### ` selector `
150158
151159Simple CSS selector (` string ` , optional).
152160Can contain a tag name (` foo ` ), IDs (` #bar ` ), and classes (` .baz ` ).
153- If there is no tag name in the selector , ` h ` defaults to a ` div ` element,
154- and ` s ` to a ` g ` element.
161+ If the selector is a string but there is no tag name in it , ` h ` defaults to
162+ build a ` div ` element, and ` s ` to a ` g ` element.
155163` selector ` is parsed by [ ` hast-util-parse-selector ` ] [ parse-selector ] .
164+ When string, builds an [ ` Element ` ] [ element ] .
165+ When nullish, builds a [ ` Root ` ] [ root ] instead.
156166
157167###### ` properties `
158168
159169Map of properties (` Object.<*> ` , optional).
170+ Keys should match either the HTML attribute name, or the DOM property name, but
171+ are case-insensitive.
172+ Cannot be given when building a [ ` Root ` ] [ root ] .
160173
161174###### ` children `
162175
163- (Lists of) child nodes (` string ` , ` Node ` , ` Array.<string|Node> ` , optional).
164- When strings are encountered, they are mapped to [ ` text ` ] [ text ] nodes.
176+ (Lists of) children (` string ` , ` number ` , ` Node ` , ` Array.<children> ` , optional).
177+ When strings or numbers are encountered, they are mapped to [ ` Text ` ] [ text ]
178+ nodes.
179+ If [ ` Root ` ] [ root ] nodes are given, their children are used instead.
165180
166181##### Returns
167182
168- [ ` Element ` ] [ element ] .
183+ [ ` Element ` ] [ element ] or [ ` Root ` ] [ root ] .
184+
185+ ## JSX
186+
187+ ` hastscript ` can be used as a pragma for JSX.
188+ The example above (omitting the second) can then be written like so, using
189+ inline Babel pragmas, so that SVG can be used too:
190+
191+ ` example-html.jsx ` :
192+
193+ ``` jsx
194+ /** @jsx h */
195+ /** @jsxFrag null */
196+ var h = require (' hastscript' )
197+
198+ console .log (
199+ < div class = " foo" id= " some-id" >
200+ < span> some text< / span>
201+ < input type= " text" value= " foo" / >
202+ < a class = " alpha bravo charlie" download>
203+ deltaecho
204+ < / a>
205+ < / div>
206+ )
207+
208+ console .log (
209+ < form method= " POST" >
210+ < input type= " text" name= " foo" / >
211+ < input type= " text" name= " bar" / >
212+ < input type= " submit" name= " send" / >
213+ < / form>
214+ )
215+ ```
216+
217+ ` example-svg.jsx ` :
218+
219+ ``` jsx
220+ /** @jsx s */
221+ /** @jsxFrag null */
222+ var s = require (' hastscript/svg' )
223+
224+ console .log (
225+ < svg xmlns= " http://www.w3.org/2000/svg" viewbox= " 0 0 500 500" >
226+ < title> SVG ` <circle>` element< / title>
227+ < circle cx= {120 } cy= {120 } r= {100 } / >
228+ < / svg>
229+ )
230+ ```
231+
232+ Because JSX does not allow dots (` . ` ) or number signs (` # ` ) in tag names, you
233+ have to pass class names and IDs in as attributes.
234+
235+ Note that you must still import ` hastscript ` yourself and configure your
236+ JavaScript compiler to use the identifier you assign it to as a pragma (and
237+ pass ` null ` for fragments).
238+
239+ For [ bublé] [ ] , this can be done by setting ` jsx: 'h' ` and ` jsxFragment: 'null' `
240+ (note that ` jsxFragment ` is currently only available on the API, not the CLI).
241+ Bublé is less ideal because it allows a single pragma.
242+
243+ For [ Babel] [ ] , use [ ` @babel/plugin-transform-react-jsx ` ] [ babel-jsx ] (in classic
244+ mode), and pass ` pragma: 'h' ` and ` pragmaFrag: 'null' ` .
245+ This is less ideal because it allows a single pragma.
246+
247+ Babel also lets you configure this in a script:
248+
249+ ``` jsx
250+ /** @jsx s */
251+ /** @jsxFrag null */
252+ var s = require (' hastscript/svg' )
253+
254+ console .log (< rect / > )
255+ ```
256+
257+ This is useful because it allows using * both* ` hastscript/html ` and
258+ ` hastscript/svg ` , although in different files.
169259
170260## Security
171261
@@ -317,10 +407,18 @@ abide by its terms.
317407
318408[ element ] : https://github.com/syntax-tree/hast#element
319409
410+ [ root ] : https://github.com/syntax-tree/xast#root
411+
320412[ text ] : https://github.com/syntax-tree/hast#text
321413
322414[ u ] : https://github.com/syntax-tree/unist-builder
323415
416+ [ bublé ] : https://github.com/Rich-Harris/buble
417+
418+ [ babel ] : https://github.com/babel/babel
419+
420+ [ babel-jsx ] : https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-react-jsx
421+
324422[ parse-selector ] : https://github.com/syntax-tree/hast-util-parse-selector
325423
326424[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
0 commit comments