Skip to content

Commit 106f1b9

Browse files
authored
Fix internal circular dependency
Closes GH-23. Reviewed-by: Titus Wormer <[email protected]>
1 parent 25baee6 commit 106f1b9

File tree

4 files changed

+56
-67
lines changed

4 files changed

+56
-67
lines changed

Diff for: lib/all.js

-24
This file was deleted.

Diff for: lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import {html, svg} from 'property-information'
99
import {htmlVoidElements} from 'html-void-elements'
1010
import {omission} from './omission/index.js'
11-
import {one} from './one.js'
11+
import {one} from './tree.js'
1212

1313
/**
1414
* @param {Node|Array.<Node>} node

Diff for: lib/one.js

-41
This file was deleted.

Diff for: lib/element.js renamed to lib/tree.js

+55-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,69 @@
44
* @typedef {import('./types.js').Context} Context
55
* @typedef {import('./types.js').Properties} Properties
66
* @typedef {import('./types.js').PropertyValue} PropertyValue
7+
* @typedef {import('./types.js').Parent} Parent
78
*/
89

910
import {svg, find} from 'property-information'
1011
import {stringify as spaces} from 'space-separated-tokens'
1112
import {stringify as commas} from 'comma-separated-tokens'
1213
import {stringifyEntities} from 'stringify-entities'
1314
import {ccount} from 'ccount'
14-
import {all} from './all.js'
1515
import {constants} from './constants.js'
16+
import {comment} from './comment'
17+
import {doctype} from './doctype'
18+
import {raw} from './raw'
19+
import {text} from './text'
20+
21+
/**
22+
* @type {Object.<string, Handle>}
23+
*/
24+
var handlers = {
25+
comment,
26+
doctype,
27+
element,
28+
// @ts-ignore `raw` is nonstandard
29+
raw,
30+
// @ts-ignore `root` is a parent.
31+
root: all,
32+
text
33+
}
34+
35+
var own = {}.hasOwnProperty
36+
37+
/**
38+
* @type {Handle}
39+
*/
40+
export function one(ctx, node, index, parent) {
41+
if (!node || !node.type) {
42+
throw new Error('Expected node, not `' + node + '`')
43+
}
44+
45+
if (!own.call(handlers, node.type)) {
46+
throw new Error('Cannot compile unknown node `' + node.type + '`')
47+
}
48+
49+
return handlers[node.type](ctx, node, index, parent)
50+
}
51+
52+
/**
53+
* Serialize all children of `parent`.
54+
*
55+
* @type {Handle}
56+
* @param {Parent} parent
57+
*/
58+
export function all(ctx, parent) {
59+
/** @type {Array.<string>} */
60+
var results = []
61+
var children = (parent && parent.children) || []
62+
var index = -1
63+
64+
while (++index < children.length) {
65+
results[index] = one(ctx, children[index], index, parent)
66+
}
67+
68+
return results.join('')
69+
}
1670

1771
/**
1872
* @type {Handle}

0 commit comments

Comments
 (0)