1
1
/**
2
- * @typedef {import('property-information').Schema } Schema
3
- * @typedef {import('hast').Root } HastRoot
2
+ * @typedef {import('hast').Comment } HastComment
4
3
* @typedef {import('hast').Doctype } HastDoctype
5
4
* @typedef {import('hast').Element } HastElement
6
- * @typedef {import('hast').Comment } HastComment
7
- * @typedef {import('hast').Text } HastText
8
5
* @typedef {import('hast').Nodes } HastNodes
9
- * @typedef {import('xast').Root } XastRoot
10
- * @typedef {import('xast').Element } XastElement
11
- * @typedef {import('xast').Text } XastText
6
+ * @typedef {import('hast').Root } HastRoot
7
+ * @typedef {import('hast').Text } HastText
8
+ *
9
+ * @typedef {import('property-information').Schema } Schema
10
+ *
11
+ * @typedef {import('xast').Attributes } XastAttributes
12
12
* @typedef {import('xast').Comment } XastComment
13
13
* @typedef {import('xast').Doctype } XastDoctype
14
- * @typedef {import('xast').Attributes } XastAttributes
15
- * @typedef {import('xast').RootContent } XastRootContent
16
- * @typedef {import('xast').Nodes } XastNodes
14
+ * @typedef {import('xast').Element } XastElement
17
15
* @typedef {import('xast').ElementContent } XastElementContent
16
+ * @typedef {import('xast').Nodes } XastNodes
17
+ * @typedef {import('xast').Root } XastRoot
18
+ * @typedef {import('xast').RootContent } XastRootContent
19
+ * @typedef {import('xast').Text } XastText
18
20
*/
19
21
20
22
/**
21
- * @typedef {'html' | 'svg' } Space
22
- * Namespace.
23
- *
24
23
* @typedef Options
25
24
* Configuration.
26
25
* @property {Space | null | undefined } [space='html']
27
- * Which space the document is in.
26
+ * Which space the document is in (default: `'html'`) .
28
27
*
29
28
* When an `<svg>` element is found in the HTML space, this package already
30
29
* automatically switches to and from the SVG space when entering and exiting
33
32
* You can also switch explicitly with `xmlns` properties in hast, but note
34
33
* that only HTML and SVG are supported.
35
34
*
35
+ * @typedef {'html' | 'svg' } Space
36
+ * Namespace.
37
+ *
36
38
* @typedef State
37
39
* Info passed around about the current state.
38
- * @property {Schema } schema
39
- * Current schema.
40
40
* @property {string | undefined } ns
41
41
* Namespace.
42
+ * @property {Schema } schema
43
+ * Current schema.
42
44
*/
43
45
44
46
import { stringify as commas } from 'comma-separated-tokens'
47
+ import { find , html , svg } from 'property-information'
45
48
import { stringify as spaces } from 'space-separated-tokens'
46
- import { html , svg , find } from 'property-information'
47
49
import { position } from 'unist-util-position'
48
50
import { webNamespaces } from 'web-namespaces'
49
51
import { zwitch } from 'zwitch'
50
52
53
+ /** @type {Options } */
54
+ const emptyOptions = { }
55
+
51
56
const own = { } . hasOwnProperty
52
57
53
58
/** @type {(node: HastNodes, state: State) => XastNodes } */
@@ -57,19 +62,20 @@ const one = zwitch('type', {
57
62
unknown
58
63
} )
59
64
65
+ // To do: next major: remove `space` shortcut.
60
66
/**
61
67
* Turn a hast tree into a xast tree.
62
68
*
63
69
* @param {HastNodes } tree
64
70
* hast tree to transform.
65
- * @param {Space | Options | null | undefined } [options]
66
- * Configuration.
71
+ * @param {Options | Space | null | undefined } [options]
72
+ * Configuration (optional) .
67
73
* @returns {XastNodes }
68
74
* xast tree.
69
75
*/
70
76
export function toXast ( tree , options ) {
71
77
const settings =
72
- typeof options === 'string' ? { space : options } : options || { }
78
+ typeof options === 'string' ? { space : options } : options || emptyOptions
73
79
74
80
return one ( tree , {
75
81
schema : settings . space === 'svg' ? svg : html ,
@@ -118,8 +124,9 @@ function root(node, state) {
118
124
let index = - 1
119
125
120
126
while ( ++ index < node . children . length ) {
121
- // @ts -expect-error never root.
122
- children [ index ] = one ( node . children [ index ] , state )
127
+ const child = node . children [ index ]
128
+ const result = /** @type {XastRootContent } */ ( one ( child , state ) )
129
+ children [ index ] = result
123
130
}
124
131
125
132
/** @type {XastRoot } */
@@ -232,11 +239,11 @@ function element(node, state) {
232
239
233
240
// Ignore nullish, false, and `NaN` values, and falsey known booleans.
234
241
if (
235
- value === undefined ||
236
- value === null ||
237
- value === false ||
238
242
( typeof value === 'number' && Number . isNaN ( value ) ) ||
239
- ( ! value && info . boolean )
243
+ ( ! value && info . boolean ) ||
244
+ value === false ||
245
+ value === null ||
246
+ value === undefined
240
247
) {
241
248
continue
242
249
}
@@ -268,13 +275,15 @@ function element(node, state) {
268
275
node . tagName === 'template' &&
269
276
node . content
270
277
) {
271
- // @ts -expect-error: never doctype.
272
- children . push ( ...root ( node . content , childState ) . children )
278
+ const results = /** @type {Array<XastElementContent> } */ (
279
+ root ( node . content , childState ) . children
280
+ )
281
+ children . push ( ...results )
273
282
} else {
274
283
while ( ++ index < node . children . length ) {
275
284
const child = node . children [ index ]
276
- // @ts -expect-error: never root.
277
- children [ index ] = one ( child , childState )
285
+ const result = /** @type { XastElementContent } */ ( one ( child , childState ) )
286
+ children [ index ] = result
278
287
}
279
288
}
280
289
@@ -291,8 +300,11 @@ function element(node, state) {
291
300
292
301
/**
293
302
* @param {HastNodes } origin
303
+ * hast node.
294
304
* @param {XastNodes } node
295
- * @returns {void }
305
+ * xast node.
306
+ * @returns {undefined }
307
+ * Nothing.
296
308
*/
297
309
function patch ( origin , node ) {
298
310
if ( origin . position ) node . position = position ( origin )
0 commit comments