Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix internal circular dependency #23

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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