|
4 | 4 | * @typedef {import('./types.js').Context} Context
|
5 | 5 | * @typedef {import('./types.js').Properties} Properties
|
6 | 6 | * @typedef {import('./types.js').PropertyValue} PropertyValue
|
| 7 | + * @typedef {import('./types.js').Parent} Parent |
7 | 8 | */
|
8 | 9 |
|
9 | 10 | import {svg, find} from 'property-information'
|
10 | 11 | import {stringify as spaces} from 'space-separated-tokens'
|
11 | 12 | import {stringify as commas} from 'comma-separated-tokens'
|
12 | 13 | import {stringifyEntities} from 'stringify-entities'
|
13 | 14 | import {ccount} from 'ccount'
|
14 |
| -import {all} from './all.js' |
15 | 15 | 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 | +} |
16 | 70 |
|
17 | 71 | /**
|
18 | 72 | * @type {Handle}
|
|
0 commit comments