1
1
/**
2
- * @typedef {import('hast').Parent } HastParent
3
2
* @typedef {import('hast').Root } HastRoot
4
3
* @typedef {import('hast').DocType } HastDoctype
5
4
* @typedef {import('hast').Element } HastElement
6
5
* @typedef {import('hast').Text } HastText
7
6
* @typedef {import('hast').Comment } HastComment
8
- * @typedef {import('hast').Content } HastChild
9
- * @typedef {HastChild|HastRoot } HastNode
7
+ * @typedef {import('hast').Content } HastContent
8
+ */
9
+
10
+ /**
11
+ * @typedef {HastContent | HastRoot } HastNode
10
12
*
11
13
* @callback AfterTransform
12
- * Called when a DOM node was transformed into a hast node .
14
+ * Callback called when each node is transformed.
13
15
* @param {Node } domNode
14
- * DOM node that was transformed .
16
+ * DOM node that was handled .
15
17
* @param {HastNode } hastNode
16
- * hast node the transform yielded .
18
+ * Corresponding hast node.
17
19
* @returns {void }
18
20
* Nothing.
19
21
*
20
22
* @typedef Options
21
- * @property {AfterTransform } [afterTransform]
22
- * Called when a DOM node was transformed into a hast node.
23
+ * Configuration.
24
+ * @property {AfterTransform | null | undefined } [afterTransform]
25
+ * Callback called when each node is transformed.
23
26
*/
24
27
25
28
import { webNamespaces } from 'web-namespaces'
26
29
import { h , s } from 'hastscript'
27
30
28
- const ELEMENT_NODE = 1
29
- const TEXT_NODE = 3
30
- const COMMENT_NODE = 8
31
- const DOCUMENT_NODE = 9
32
- const DOCUMENT_TYPE_NODE = 10
33
- const DOCUMENT_FRAGMENT_NODE = 11
34
-
35
31
/**
36
32
* Transform a DOM tree to a hast tree.
37
33
*
38
- * @param {Node } node
39
- * @param {Options } [options]
34
+ * @param {Node } tree
35
+ * DOM tree to transform.
36
+ * @param {Options | null | undefined } [options]
37
+ * Configuration (optional).
40
38
* @returns {HastNode }
39
+ * Equivalent hast node.
41
40
*/
42
- export function fromDom ( node , options = { } ) {
43
- return (
44
- ( node ? transform ( node , options ) : undefined ) || {
45
- type : 'root' ,
46
- children : [ ]
47
- }
48
- )
41
+ export function fromDom ( tree , options ) {
42
+ const result = tree ? transform ( tree , options || { } ) : undefined
43
+ return result || { type : 'root' , children : [ ] }
49
44
}
50
45
51
46
/**
52
47
* @param {Node } node
53
- * @param {Options } ctx
54
- * @returns {HastNode|undefined }
48
+ * DOM node to transform.
49
+ * @param {Options } options
50
+ * Configuration.
51
+ * @returns {HastNode | undefined }
52
+ * Equivalent hast node.
53
+ *
54
+ * Note that certain legacy DOM nodes (i.e., Attr nodes (2), CDATA, processing instructions)
55
55
*/
56
- function transform ( node , ctx ) {
57
- const transformed = one ( node , ctx )
58
- if ( ctx . afterTransform && transformed ) ctx . afterTransform ( node , transformed )
56
+ function transform ( node , options ) {
57
+ const transformed = one ( node , options )
58
+ if ( transformed && options . afterTransform )
59
+ options . afterTransform ( node , transformed )
59
60
return transformed
60
61
}
61
62
62
63
/**
63
64
* @param {Node } node
64
- * @param {Options } ctx
65
- * @returns {HastNode|undefined }
65
+ * DOM node to transform.
66
+ * @param {Options } options
67
+ * Configuration.
68
+ * @returns {HastNode | undefined }
69
+ * Equivalent hast node.
66
70
*/
67
- function one ( node , ctx ) {
71
+ function one ( node , options ) {
68
72
switch ( node . nodeType ) {
69
- case ELEMENT_NODE : {
73
+ case 1 /* Element */ : {
70
74
// @ts -expect-error TypeScript is wrong.
71
- return element ( node , ctx )
75
+ return element ( node , options )
72
76
}
73
77
74
- case DOCUMENT_NODE :
75
- case DOCUMENT_FRAGMENT_NODE : {
76
- // @ts -expect-error TypeScript is wrong.
77
- return root ( node , ctx )
78
- }
78
+ // Ignore: Attr (2).
79
79
80
- case TEXT_NODE : {
80
+ case 3 /* Text */ : {
81
81
// @ts -expect-error TypeScript is wrong.
82
82
return text ( node )
83
83
}
84
84
85
- case COMMENT_NODE : {
85
+ // Ignore: CDATA (4).
86
+ // Removed: Entity reference (5)
87
+ // Removed: Entity (6)
88
+ // Ignore: Processing instruction (7).
89
+
90
+ case 8 /* Comment */ : {
86
91
// @ts -expect-error TypeScript is wrong.
87
92
return comment ( node )
88
93
}
89
94
90
- case DOCUMENT_TYPE_NODE : {
95
+ case 9 /* Document */ : {
96
+ // @ts -expect-error TypeScript is wrong.
97
+ return root ( node , options )
98
+ }
99
+
100
+ case 10 /* Document type */ : {
91
101
return doctype ( )
92
102
}
93
103
104
+ case 11 /* Document fragment */ : {
105
+ // @ts -expect-error TypeScript is wrong.
106
+ return root ( node , options )
107
+ }
108
+
94
109
default : {
95
110
return undefined
96
111
}
@@ -100,18 +115,22 @@ function one(node, ctx) {
100
115
/**
101
116
* Transform a document.
102
117
*
103
- * @param {Document|DocumentFragment } node
104
- * @param {Options } ctx
118
+ * @param {Document | DocumentFragment } node
119
+ * DOM node to transform.
120
+ * @param {Options } options
121
+ * Configuration.
105
122
* @returns {HastRoot }
123
+ * Equivalent hast node.
106
124
*/
107
- function root ( node , ctx ) {
108
- return { type : 'root' , children : all ( node , ctx ) }
125
+ function root ( node , options ) {
126
+ return { type : 'root' , children : all ( node , options ) }
109
127
}
110
128
111
129
/**
112
130
* Transform a doctype.
113
131
*
114
132
* @returns {HastDoctype }
133
+ * Equivalent hast node.
115
134
*/
116
135
function doctype ( ) {
117
136
// @ts -expect-error hast types out of date.
@@ -122,7 +141,9 @@ function doctype() {
122
141
* Transform a text.
123
142
*
124
143
* @param {Text } node
144
+ * DOM node to transform.
125
145
* @returns {HastText }
146
+ * Equivalent hast node.
126
147
*/
127
148
function text ( node ) {
128
149
return { type : 'text' , value : node . nodeValue || '' }
@@ -132,7 +153,9 @@ function text(node) {
132
153
* Transform a comment.
133
154
*
134
155
* @param {Comment } node
156
+ * DOM node to transform.
135
157
* @returns {HastComment }
158
+ * Equivalent hast node.
136
159
*/
137
160
function comment ( node ) {
138
161
return { type : 'comment' , value : node . nodeValue || '' }
@@ -142,15 +165,18 @@ function comment(node) {
142
165
* Transform an element.
143
166
*
144
167
* @param {Element } node
145
- * @param {Options } ctx
168
+ * DOM node to transform.
169
+ * @param {Options } options
170
+ * Configuration.
146
171
* @returns {HastElement }
172
+ * Equivalent hast node.
147
173
*/
148
- function element ( node , ctx ) {
174
+ function element ( node , options ) {
149
175
const space = node . namespaceURI
150
176
const fn = space === webNamespaces . svg ? s : h
151
177
const tagName =
152
178
space === webNamespaces . html ? node . tagName . toLowerCase ( ) : node . tagName
153
- /** @type {DocumentFragment| Element } */
179
+ /** @type {DocumentFragment | Element } */
154
180
const content =
155
181
// @ts -expect-error Types are wrong.
156
182
space === webNamespaces . html && tagName === 'template' ? node . content : node
@@ -163,24 +189,27 @@ function element(node, ctx) {
163
189
props [ attributes [ index ] ] = node . getAttribute ( attributes [ index ] ) || ''
164
190
}
165
191
166
- return fn ( tagName , props , all ( content , ctx ) )
192
+ return fn ( tagName , props , all ( content , options ) )
167
193
}
168
194
169
195
/**
170
- * Transform an element .
196
+ * Transform child nodes in a parent .
171
197
*
172
- * @param {Document|DocumentFragment|Element } node
173
- * @param {Options } ctx
174
- * @returns {Array<HastChild> }
198
+ * @param {Document | DocumentFragment | Element } node
199
+ * DOM node to transform.
200
+ * @param {Options } options
201
+ * Configuration.
202
+ * @returns {Array<HastContent> }
203
+ * Equivalent hast nodes.
175
204
*/
176
- function all ( node , ctx ) {
205
+ function all ( node , options ) {
177
206
const nodes = node . childNodes
178
- /** @type {Array<HastChild > } */
207
+ /** @type {Array<HastContent > } */
179
208
const children = [ ]
180
209
let index = - 1
181
210
182
211
while ( ++ index < nodes . length ) {
183
- const child = transform ( nodes [ index ] , ctx )
212
+ const child = transform ( nodes [ index ] , options )
184
213
185
214
if ( child !== undefined ) {
186
215
// @ts -expect-error Assume no document inside document.
0 commit comments