|
| 1 | +/** |
| 2 | + * @import { TemplateOperations } from "../types.js" |
| 3 | + */ |
| 4 | +import { is_void } from '../../../../../utils.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @param {TemplateOperations} items |
| 8 | + */ |
| 9 | +export function template_to_string(items) { |
| 10 | + let elements = []; |
| 11 | + |
| 12 | + /** |
| 13 | + * @type {Array<Element>} |
| 14 | + */ |
| 15 | + let elements_stack = []; |
| 16 | + |
| 17 | + /** |
| 18 | + * @type {Element | undefined} |
| 19 | + */ |
| 20 | + let last_current_element; |
| 21 | + |
| 22 | + for (let instruction of items) { |
| 23 | + if (instruction.kind === 'push_element' && last_current_element) { |
| 24 | + elements_stack.push(last_current_element); |
| 25 | + continue; |
| 26 | + } |
| 27 | + if (instruction.kind === 'pop_element') { |
| 28 | + elements_stack.pop(); |
| 29 | + continue; |
| 30 | + } |
| 31 | + /** |
| 32 | + * @type {Node | void} |
| 33 | + */ |
| 34 | + // @ts-expect-error we can't be here if `swap_current_element` but TS doesn't know that |
| 35 | + const value = map[instruction.kind]( |
| 36 | + ...[ |
| 37 | + ...(instruction.kind === 'set_prop' ? [last_current_element] : []), |
| 38 | + ...(instruction.args ?? []) |
| 39 | + ] |
| 40 | + ); |
| 41 | + if (instruction.kind !== 'set_prop') { |
| 42 | + if (elements_stack.length >= 1 && value) { |
| 43 | + map.insert(/** @type {Element} */ (elements_stack.at(-1)), value); |
| 44 | + } else if (value) { |
| 45 | + elements.push(value); |
| 46 | + } |
| 47 | + if (instruction.kind === 'create_element') { |
| 48 | + last_current_element = /** @type {Element} */ (value); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return elements.map((el) => stringify(el)).join(''); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @typedef {{ kind: "element", element: string, props?: Record<string, string>, children?: Array<Node> }} Element |
| 58 | + */ |
| 59 | + |
| 60 | +/** |
| 61 | + * @typedef {{ kind: "anchor", data?: string }} Anchor |
| 62 | + */ |
| 63 | + |
| 64 | +/** |
| 65 | + * @typedef {{ kind: "text", value?: string }} Text |
| 66 | + */ |
| 67 | + |
| 68 | +/** |
| 69 | + * @typedef { Element | Anchor| Text } Node |
| 70 | + */ |
| 71 | + |
| 72 | +/** |
| 73 | + * |
| 74 | + * @param {string} element |
| 75 | + * @returns {Element} |
| 76 | + */ |
| 77 | +function create_element(element) { |
| 78 | + return { |
| 79 | + kind: 'element', |
| 80 | + element |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @param {string} data |
| 86 | + * @returns {Anchor} |
| 87 | + */ |
| 88 | +function create_anchor(data) { |
| 89 | + return { |
| 90 | + kind: 'anchor', |
| 91 | + data |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * @param {string} value |
| 97 | + * @returns {Text} |
| 98 | + */ |
| 99 | +function create_text(value) { |
| 100 | + return { |
| 101 | + kind: 'text', |
| 102 | + value |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * |
| 108 | + * @param {Element} el |
| 109 | + * @param {string} prop |
| 110 | + * @param {string} value |
| 111 | + */ |
| 112 | +function set_prop(el, prop, value) { |
| 113 | + el.props ??= {}; |
| 114 | + el.props[prop] = value; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * |
| 119 | + * @param {Element} el |
| 120 | + * @param {Node} child |
| 121 | + * @param {Node} [anchor] |
| 122 | + */ |
| 123 | +function insert(el, child, anchor) { |
| 124 | + el.children ??= []; |
| 125 | + el.children.push(child); |
| 126 | +} |
| 127 | + |
| 128 | +let map = { |
| 129 | + create_element, |
| 130 | + create_text, |
| 131 | + create_anchor, |
| 132 | + set_prop, |
| 133 | + insert |
| 134 | +}; |
| 135 | + |
| 136 | +/** |
| 137 | + * |
| 138 | + * @param {Node} el |
| 139 | + * @returns |
| 140 | + */ |
| 141 | +function stringify(el) { |
| 142 | + let str = ``; |
| 143 | + if (el.kind === 'element') { |
| 144 | + str += `<${el.element}`; |
| 145 | + for (let [prop, value] of Object.entries(el.props ?? {})) { |
| 146 | + if (value == null) { |
| 147 | + str += ` ${prop}`; |
| 148 | + } else { |
| 149 | + str += ` ${prop}="${value}"`; |
| 150 | + } |
| 151 | + } |
| 152 | + str += `>`; |
| 153 | + for (let child of el.children ?? []) { |
| 154 | + str += stringify(child); |
| 155 | + } |
| 156 | + if (!is_void(el.element)) { |
| 157 | + str += `</${el.element}>`; |
| 158 | + } |
| 159 | + } else if (el.kind === 'text') { |
| 160 | + str += el.value; |
| 161 | + } else if (el.kind === 'anchor') { |
| 162 | + if (el.data) { |
| 163 | + str += `<!--${el.data}-->`; |
| 164 | + } else { |
| 165 | + str += `<!>`; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return str; |
| 170 | +} |
0 commit comments