Skip to content

Commit 1d4ed1f

Browse files
committed
Refactor code-style
* Add support for `null` in API input types * Add more docs to JSDoc
1 parent b6b7393 commit 1d4ed1f

File tree

1 file changed

+86
-57
lines changed

1 file changed

+86
-57
lines changed

lib/index.js

Lines changed: 86 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,111 @@
11
/**
2-
* @typedef {import('hast').Parent} HastParent
32
* @typedef {import('hast').Root} HastRoot
43
* @typedef {import('hast').DocType} HastDoctype
54
* @typedef {import('hast').Element} HastElement
65
* @typedef {import('hast').Text} HastText
76
* @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
1012
*
1113
* @callback AfterTransform
12-
* Called when a DOM node was transformed into a hast node.
14+
* Callback called when each node is transformed.
1315
* @param {Node} domNode
14-
* DOM node that was transformed.
16+
* DOM node that was handled.
1517
* @param {HastNode} hastNode
16-
* hast node the transform yielded.
18+
* Corresponding hast node.
1719
* @returns {void}
1820
* Nothing.
1921
*
2022
* @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.
2326
*/
2427

2528
import {webNamespaces} from 'web-namespaces'
2629
import {h, s} from 'hastscript'
2730

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-
3531
/**
3632
* Transform a DOM tree to a hast tree.
3733
*
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).
4038
* @returns {HastNode}
39+
* Equivalent hast node.
4140
*/
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: []}
4944
}
5045

5146
/**
5247
* @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)
5555
*/
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)
5960
return transformed
6061
}
6162

6263
/**
6364
* @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.
6670
*/
67-
function one(node, ctx) {
71+
function one(node, options) {
6872
switch (node.nodeType) {
69-
case ELEMENT_NODE: {
73+
case 1 /* Element */: {
7074
// @ts-expect-error TypeScript is wrong.
71-
return element(node, ctx)
75+
return element(node, options)
7276
}
7377

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).
7979

80-
case TEXT_NODE: {
80+
case 3 /* Text */: {
8181
// @ts-expect-error TypeScript is wrong.
8282
return text(node)
8383
}
8484

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 */: {
8691
// @ts-expect-error TypeScript is wrong.
8792
return comment(node)
8893
}
8994

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 */: {
91101
return doctype()
92102
}
93103

104+
case 11 /* Document fragment */: {
105+
// @ts-expect-error TypeScript is wrong.
106+
return root(node, options)
107+
}
108+
94109
default: {
95110
return undefined
96111
}
@@ -100,18 +115,22 @@ function one(node, ctx) {
100115
/**
101116
* Transform a document.
102117
*
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.
105122
* @returns {HastRoot}
123+
* Equivalent hast node.
106124
*/
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)}
109127
}
110128

111129
/**
112130
* Transform a doctype.
113131
*
114132
* @returns {HastDoctype}
133+
* Equivalent hast node.
115134
*/
116135
function doctype() {
117136
// @ts-expect-error hast types out of date.
@@ -122,7 +141,9 @@ function doctype() {
122141
* Transform a text.
123142
*
124143
* @param {Text} node
144+
* DOM node to transform.
125145
* @returns {HastText}
146+
* Equivalent hast node.
126147
*/
127148
function text(node) {
128149
return {type: 'text', value: node.nodeValue || ''}
@@ -132,7 +153,9 @@ function text(node) {
132153
* Transform a comment.
133154
*
134155
* @param {Comment} node
156+
* DOM node to transform.
135157
* @returns {HastComment}
158+
* Equivalent hast node.
136159
*/
137160
function comment(node) {
138161
return {type: 'comment', value: node.nodeValue || ''}
@@ -142,15 +165,18 @@ function comment(node) {
142165
* Transform an element.
143166
*
144167
* @param {Element} node
145-
* @param {Options} ctx
168+
* DOM node to transform.
169+
* @param {Options} options
170+
* Configuration.
146171
* @returns {HastElement}
172+
* Equivalent hast node.
147173
*/
148-
function element(node, ctx) {
174+
function element(node, options) {
149175
const space = node.namespaceURI
150176
const fn = space === webNamespaces.svg ? s : h
151177
const tagName =
152178
space === webNamespaces.html ? node.tagName.toLowerCase() : node.tagName
153-
/** @type {DocumentFragment|Element} */
179+
/** @type {DocumentFragment | Element} */
154180
const content =
155181
// @ts-expect-error Types are wrong.
156182
space === webNamespaces.html && tagName === 'template' ? node.content : node
@@ -163,24 +189,27 @@ function element(node, ctx) {
163189
props[attributes[index]] = node.getAttribute(attributes[index]) || ''
164190
}
165191

166-
return fn(tagName, props, all(content, ctx))
192+
return fn(tagName, props, all(content, options))
167193
}
168194

169195
/**
170-
* Transform an element.
196+
* Transform child nodes in a parent.
171197
*
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.
175204
*/
176-
function all(node, ctx) {
205+
function all(node, options) {
177206
const nodes = node.childNodes
178-
/** @type {Array<HastChild>} */
207+
/** @type {Array<HastContent>} */
179208
const children = []
180209
let index = -1
181210

182211
while (++index < nodes.length) {
183-
const child = transform(nodes[index], ctx)
212+
const child = transform(nodes[index], options)
184213

185214
if (child !== undefined) {
186215
// @ts-expect-error Assume no document inside document.

0 commit comments

Comments
 (0)