@@ -138,34 +138,124 @@ Yields:
138
138
139
139
## API
140
140
141
- ### ` h(selector?[, properties][, ... children]) `
141
+ ### ` h(selector?[, properties][, … children]) `
142
142
143
- ### ` s(selector?[, properties][, ... children]) `
143
+ ### ` s(selector?[, properties][, … children]) `
144
144
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 ` ).
146
154
147
155
##### Parameters
148
156
149
157
###### ` selector `
150
158
151
159
Simple CSS selector (` string ` , optional).
152
160
Can 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.
155
163
` 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.
156
166
157
167
###### ` properties `
158
168
159
169
Map 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 ] .
160
173
161
174
###### ` children `
162
175
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.
165
180
166
181
##### Returns
167
182
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 can then be written like so, using inline Babel pragmas, so
189
+ 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.
169
259
170
260
## Security
171
261
@@ -317,10 +407,18 @@ abide by its terms.
317
407
318
408
[ element ] : https://github.com/syntax-tree/hast#element
319
409
410
+ [ root ] : https://github.com/syntax-tree/xast#root
411
+
320
412
[ text ] : https://github.com/syntax-tree/hast#text
321
413
322
414
[ u ] : https://github.com/syntax-tree/unist-builder
323
415
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
+
324
422
[ parse-selector ] : https://github.com/syntax-tree/hast-util-parse-selector
325
423
326
424
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
0 commit comments