Skip to content

Commit

Permalink
Fix internal circular dependency
Browse files Browse the repository at this point in the history
Closes GH-23.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
ZirionNeft committed Jul 20, 2021
1 parent 25baee6 commit 106f1b9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 67 deletions.
24 changes: 0 additions & 24 deletions lib/all.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {html, svg} from 'property-information'
import {htmlVoidElements} from 'html-void-elements'
import {omission} from './omission/index.js'
import {one} from './one.js'
import {one} from './tree.js'

/**
* @param {Node|Array.<Node>} node
Expand Down
41 changes: 0 additions & 41 deletions lib/one.js

This file was deleted.

56 changes: 55 additions & 1 deletion lib/element.js → lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,69 @@
* @typedef {import('./types.js').Context} Context
* @typedef {import('./types.js').Properties} Properties
* @typedef {import('./types.js').PropertyValue} PropertyValue
* @typedef {import('./types.js').Parent} Parent
*/

import {svg, find} from 'property-information'
import {stringify as spaces} from 'space-separated-tokens'
import {stringify as commas} from 'comma-separated-tokens'
import {stringifyEntities} from 'stringify-entities'
import {ccount} from 'ccount'
import {all} from './all.js'
import {constants} from './constants.js'
import {comment} from './comment'
import {doctype} from './doctype'
import {raw} from './raw'
import {text} from './text'

/**
* @type {Object.<string, Handle>}
*/
var handlers = {
comment,
doctype,
element,
// @ts-ignore `raw` is nonstandard
raw,
// @ts-ignore `root` is a parent.
root: all,
text
}

var own = {}.hasOwnProperty

/**
* @type {Handle}
*/
export function one(ctx, node, index, parent) {
if (!node || !node.type) {
throw new Error('Expected node, not `' + node + '`')
}

if (!own.call(handlers, node.type)) {
throw new Error('Cannot compile unknown node `' + node.type + '`')
}

return handlers[node.type](ctx, node, index, parent)
}

/**
* Serialize all children of `parent`.
*
* @type {Handle}
* @param {Parent} parent
*/
export function all(ctx, parent) {
/** @type {Array.<string>} */
var results = []
var children = (parent && parent.children) || []
var index = -1

while (++index < children.length) {
results[index] = one(ctx, children[index], index, parent)
}

return results.join('')
}

/**
* @type {Handle}
Expand Down

0 comments on commit 106f1b9

Please sign in to comment.