|
| 1 | +/** |
| 2 | + * @typedef {import('xast').Root} Root |
| 3 | + * @typedef {import('xast').Element} Element |
| 4 | + * @typedef {Root['children'][number]} Child |
| 5 | + * @typedef {Child|Root} Node |
| 6 | + * @typedef {Root|Element} XResult |
| 7 | + * @typedef {string|number|boolean|null|undefined} XValue |
| 8 | + * @typedef {{[attribute: string]: XValue}} XAttributes Attributes to support JS primitive types |
| 9 | + * |
| 10 | + * @typedef {string|number|null|undefined} XPrimitiveChild |
| 11 | + * @typedef {Array.<Node|XPrimitiveChild>} XArrayChild |
| 12 | + * @typedef {Node|XPrimitiveChild|XArrayChild} XChild |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * Create XML trees in xast. |
| 17 | + * |
| 18 | + * @param name Qualified name. Case sensitive and can contain a namespace prefix (such as `rdf:RDF`). Pass `null|undefined` to build a root. |
| 19 | + * @param attributes Map of attributes. Nullish (null or undefined) or NaN values are ignored, other values (strings, booleans) are cast to strings. |
| 20 | + * @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes. |
| 21 | + */ |
| 22 | +export const x = |
| 23 | + /** |
| 24 | + * @type {{ |
| 25 | + * (): Root |
| 26 | + * (name: null|undefined, ...children: XChild[]): Root |
| 27 | + * (name: string, attributes: XAttributes, ...children: XChild[]): Element |
| 28 | + * (name: string, ...children: XChild[]): Element |
| 29 | + * }} |
| 30 | + */ |
| 31 | + ( |
| 32 | + /** |
| 33 | + * Hyperscript compatible DSL for creating virtual xast trees. |
| 34 | + * |
| 35 | + * @param {string|null} [name] |
| 36 | + * @param {XAttributes|XChild} [attributes] |
| 37 | + * @param {XChild[]} children |
| 38 | + * @returns {XResult} |
| 39 | + */ |
| 40 | + function (name, attributes, ...children) { |
| 41 | + var index = -1 |
| 42 | + /** @type {XResult} */ |
| 43 | + var node |
| 44 | + /** @type {string} */ |
| 45 | + var key |
| 46 | + |
| 47 | + if (name === undefined || name === null) { |
| 48 | + node = {type: 'root', children: []} |
| 49 | + // @ts-ignore Root builder doesn’t accept attributes. |
| 50 | + children.unshift(attributes) |
| 51 | + } else if (typeof name === 'string') { |
| 52 | + node = {type: 'element', name, attributes: {}, children: []} |
| 53 | + |
| 54 | + if (isAttributes(attributes)) { |
| 55 | + for (key in attributes) { |
| 56 | + // Ignore nullish and NaN values. |
| 57 | + if ( |
| 58 | + attributes[key] !== undefined && |
| 59 | + attributes[key] !== null && |
| 60 | + (typeof attributes[key] !== 'number' || |
| 61 | + !Number.isNaN(attributes[key])) |
| 62 | + ) { |
| 63 | + // @ts-ignore Pretty sure we just set it. |
| 64 | + node.attributes[key] = String(attributes[key]) |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + children.unshift(attributes) |
| 69 | + } |
| 70 | + } else { |
| 71 | + throw new TypeError('Expected element name, got `' + name + '`') |
| 72 | + } |
| 73 | + |
| 74 | + // Handle children. |
| 75 | + while (++index < children.length) { |
| 76 | + addChild(node.children, children[index]) |
| 77 | + } |
| 78 | + |
| 79 | + return node |
| 80 | + } |
| 81 | + ) |
| 82 | + |
| 83 | +/** |
| 84 | + * @param {Array.<Child>} nodes |
| 85 | + * @param {XChild} value |
| 86 | + */ |
| 87 | +function addChild(nodes, value) { |
| 88 | + var index = -1 |
| 89 | + |
| 90 | + if (value === undefined || value === null) { |
| 91 | + // Empty. |
| 92 | + } else if (typeof value === 'string' || typeof value === 'number') { |
| 93 | + nodes.push({type: 'text', value: String(value)}) |
| 94 | + } else if (Array.isArray(value)) { |
| 95 | + while (++index < value.length) { |
| 96 | + addChild(nodes, value[index]) |
| 97 | + } |
| 98 | + } else if (typeof value === 'object' && 'type' in value) { |
| 99 | + if (value.type === 'root') { |
| 100 | + addChild(nodes, value.children) |
| 101 | + } else { |
| 102 | + nodes.push(value) |
| 103 | + } |
| 104 | + } else { |
| 105 | + throw new TypeError('Expected node, nodes, string, got `' + value + '`') |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * @param {XAttributes|XChild} value |
| 111 | + * @returns {value is XAttributes} |
| 112 | + */ |
| 113 | +function isAttributes(value) { |
| 114 | + if ( |
| 115 | + value === null || |
| 116 | + value === undefined || |
| 117 | + typeof value !== 'object' || |
| 118 | + Array.isArray(value) |
| 119 | + ) { |
| 120 | + return false |
| 121 | + } |
| 122 | + |
| 123 | + return true |
| 124 | +} |
0 commit comments