Skip to content

Commit 25cdeef

Browse files
committed
Refactor code-style
1 parent 92f8498 commit 25cdeef

File tree

7 files changed

+484
-378
lines changed

7 files changed

+484
-378
lines changed

lib/index.js

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/**
2-
* @typedef {import('hast').Root} HastRoot
3-
* @typedef {import('hast').RootContent} HastRootContent
2+
* @typedef {import('hast').Comment} HastComment
43
* @typedef {import('hast').Doctype} HastDoctype
54
* @typedef {import('hast').Element} HastElement
6-
* @typedef {import('hast').Text} HastText
7-
* @typedef {import('hast').Comment} HastComment
85
* @typedef {import('hast').Nodes} HastNodes
6+
* @typedef {import('hast').Properties} HastProperties
7+
* @typedef {import('hast').Root} HastRoot
8+
* @typedef {import('hast').RootContent} HastRootContent
9+
* @typedef {import('hast').Text} HastText
10+
*
11+
* @typedef {import('property-information').Schema} Schema
912
*/
1013

1114
/**
@@ -15,19 +18,22 @@
1518
* hast node that was handled.
1619
* @param {Node} domNode
1720
* Corresponding DOM node.
18-
* @returns {void}
21+
* @returns {undefined | void}
1922
* Nothing.
2023
*
24+
* Note: `void` included until TS infers `undefined` fine.
25+
*
2126
* @typedef Options
2227
* Configuration.
2328
* @property {AfterTransform | null | undefined} [afterTransform]
24-
* Callback called when each node is transformed.
29+
* Callback called when each node is transformed (optional).
2530
* @property {Document | null | undefined} [document]
2631
* Document interface to use (default: `globalThis.document`).
2732
* @property {boolean | null | undefined} [fragment=false]
28-
* Whether to return a DOM fragment (`true`) or a whole document (`false`).
33+
* Whether to return a DOM fragment (`true`) or a whole document (`false`)
34+
* (default: `false`).
2935
* @property {string | null | undefined} [namespace]
30-
* Namespace to use to create elements.
36+
* Namespace to use to create elements (optional).
3137
*
3238
* @typedef State
3339
* Info passed around about the current state.
@@ -36,17 +42,17 @@
3642
* @property {boolean} fragment
3743
* Whether a fragment (`true`) or whole document (`false`) is built.
3844
* @property {string | undefined} namespace
39-
* Namespace to use.
45+
* Explicit namespace to use.
4046
* @property {string | undefined} impliedNamespace
41-
* To do.
47+
* Namespace.
4248
* @property {AfterTransform | undefined} afterTransform
4349
* Callback called after each hast node is transformed.
4450
*/
4551

4652
/* eslint-env browser */
4753

54+
import {html, find, svg} from 'property-information'
4855
import {webNamespaces} from 'web-namespaces'
49-
import {find, html, svg} from 'property-information'
5056

5157
const own = {}.hasOwnProperty
5258

@@ -57,7 +63,7 @@ const own = {}.hasOwnProperty
5763
* Tree to transform.
5864
* @param {Options | null | undefined} [options]
5965
* Configuration (optional).
60-
* @returns {XMLDocument | DocumentFragment | Text | DocumentType | Comment | Element}
66+
* @returns {Comment | Document | DocumentFragment | DocumentType | Element | Text}
6167
* Equivalent DOM node.
6268
*/
6369
export function toDom(tree, options) {
@@ -76,7 +82,7 @@ export function toDom(tree, options) {
7682
* Node to transform.
7783
* @param {State} state
7884
* Info passed around about the current state.
79-
* @returns {XMLDocument | DocumentFragment | Text | DocumentType | Comment | Element}
85+
* @returns {Comment | Document | DocumentFragment | DocumentType | Element | Text}
8086
* Equivalent DOM node.
8187
*/
8288
function transform(node, state) {
@@ -92,7 +98,7 @@ function transform(node, state) {
9298
* Node to transform.
9399
* @param {State} state
94100
* Info passed around about the current state.
95-
* @returns {XMLDocument | DocumentFragment | Text | DocumentType | Comment | Element}
101+
* @returns {Comment | Document | DocumentFragment | DocumentType | Element | Text}
96102
* Equivalent DOM node.
97103
*/
98104
function one(node, state) {
@@ -127,7 +133,7 @@ function one(node, state) {
127133
* Node to transform.
128134
* @param {State} state
129135
* Info passed around about the current state.
130-
* @returns {XMLDocument | DocumentFragment | HTMLHtmlElement}
136+
* @returns {Document | DocumentFragment | HTMLHtmlElement}
131137
* Equivalent DOM node.
132138
*/
133139
function root(node, state) {
@@ -155,14 +161,14 @@ function root(node, state) {
155161

156162
const namespace = state.namespace || foundNamespace
157163
// The root node will be `Document`, `DocumentFragment`, or `HTMLElement`.
158-
/** @type {XMLDocument | DocumentFragment | HTMLHtmlElement} */
164+
/** @type {Document | DocumentFragment | HTMLHtmlElement} */
159165
let result
160166

161167
if (rootIsDocument) {
162168
result = state.doc.implementation.createDocument(
169+
// Note: `null` is different from `undefined`.
163170
namespace || null,
164-
'',
165-
null
171+
''
166172
)
167173
} else if (state.fragment) {
168174
result = state.doc.createDocumentFragment()
@@ -231,7 +237,6 @@ function comment(node, state) {
231237
* @returns {Element}
232238
* DOM element.
233239
*/
234-
// eslint-disable-next-line complexity
235240
function element(node, state) {
236241
let impliedNamespace = state.impliedNamespace || state.namespace
237242
// Important: unknown nodes are passed to `element`.
@@ -249,12 +254,37 @@ function element(node, state) {
249254
impliedNamespace = webNamespaces.svg
250255
}
251256

252-
const schema = impliedNamespace === webNamespaces.svg ? svg : html
253-
254257
const result = impliedNamespace
255258
? state.doc.createElementNS(impliedNamespace, tagName)
256259
: state.doc.createElement(tagName)
257260

261+
addProperties(
262+
result,
263+
properties,
264+
impliedNamespace === webNamespaces.svg ? svg : html
265+
)
266+
267+
const currentImpliedNamespace = state.impliedNamespace
268+
state.impliedNamespace = impliedNamespace
269+
appendAll(result, children, state)
270+
state.impliedNamespace = currentImpliedNamespace
271+
272+
return result
273+
}
274+
275+
/**
276+
* Add all properties.
277+
*
278+
* @param {Element} result
279+
* Element.
280+
* @param {HastProperties} properties
281+
* Properties from hast element.
282+
* @param {Schema} schema
283+
* Schema from `property-information`.
284+
* @returns {undefined}
285+
* Nothing.
286+
*/
287+
function addProperties(result, properties, schema) {
258288
/** @type {string} */
259289
let key
260290

@@ -268,7 +298,8 @@ function element(node, state) {
268298
}
269299

270300
if (info.mustUseProperty) {
271-
// @ts-expect-error: fine.
301+
// @ts-expect-error: setting the property is fine, according to
302+
// `property-information`.
272303
result[info.property] = value
273304
}
274305

@@ -279,6 +310,7 @@ function element(node, state) {
279310
if (value) {
280311
result.setAttribute(info.attribute, '')
281312
} else {
313+
// To do: remove, we create new elements, we don’t mutate existing ones.
282314
result.removeAttribute(info.attribute)
283315
}
284316
} else if (info.booleanish) {
@@ -290,34 +322,24 @@ function element(node, state) {
290322
}
291323
}
292324
}
293-
294-
const currentImpliedNamespace = state.impliedNamespace
295-
state.impliedNamespace = impliedNamespace
296-
297-
appendAll(result, children, state)
298-
299-
state.impliedNamespace = currentImpliedNamespace
300-
301-
return result
302325
}
303326

304327
/**
305328
* Add all children.
306329
*
307-
* @param {Node} node
330+
* @param {Document | DocumentFragment | Element} node
308331
* DOM node to append to.
309332
* @param {Array<HastRootContent>} children
310333
* hast children.
311334
* @param {State} state
312335
* Info passed around about the current state.
313-
* @returns {void}
336+
* @returns {undefined}
314337
* Nothing.
315338
*/
316339
function appendAll(node, children, state) {
317340
let index = -1
318341

319342
while (++index < children.length) {
320-
// eslint-disable-next-line unicorn/prefer-dom-node-append
321-
node.appendChild(transform(children[index], state))
343+
node.append(transform(children[index], state))
322344
}
323345
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"type": "doctype",
3-
"name": "",
4-
"public": null,
5-
"system": null
2+
"type": "doctype"
63
}

test/fixtures/doctype-quirksmode-ibm/index.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
"type": "root",
33
"children": [
44
{
5-
"type": "doctype",
6-
"name": "html",
7-
"public": null,
8-
"system": "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"
5+
"type": "doctype"
96
},
107
{
118
"type": "element",

test/fixtures/doctype/index.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"type": "doctype",
3-
"name": "html",
4-
"public": null,
5-
"system": null
2+
"type": "doctype"
63
}

test/fixtures/simple/index.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
"type": "root",
33
"children": [
44
{
5-
"type": "doctype",
6-
"name": "html",
7-
"public": null,
8-
"system": null
5+
"type": "doctype"
96
},
107
{
118
"type": "element",

test/fixtures/template/index.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
"type": "root",
33
"children": [
44
{
5-
"type": "doctype",
6-
"name": "html",
7-
"public": null,
8-
"system": null
5+
"type": "doctype"
96
},
107
{
118
"type": "element",

0 commit comments

Comments
 (0)