|
1 | 1 | /** @import { AST } from '#compiler' */ |
2 | 2 | /** @import { Node, Element } from './types'; */ |
| 3 | +import { escape_html } from '../../../../../escaping.js'; |
| 4 | +import { is_void } from '../../../../../utils.js'; |
| 5 | +import * as b from '#compiler/builders'; |
| 6 | +import fix_attribute_casing from './fix-attribute-casing.js'; |
| 7 | +import { regex_starts_with_newline } from '../../../patterns.js'; |
3 | 8 |
|
4 | 9 | export class Template { |
5 | 10 | /** |
@@ -65,4 +70,94 @@ export class Template { |
65 | 70 | set_prop(key, value) { |
66 | 71 | /** @type {Element} */ (this.#element).attributes[key] = value; |
67 | 72 | } |
| 73 | + |
| 74 | + as_string() { |
| 75 | + return b.template([b.quasi(this.nodes.map(stringify).join(''), true)], []); |
| 76 | + } |
| 77 | + |
| 78 | + as_objects() { |
| 79 | + // if the first item is a comment we need to add another comment for effect.start |
| 80 | + if (this.nodes[0].type === 'anchor') { |
| 81 | + this.nodes.unshift({ type: 'anchor', data: undefined }); |
| 82 | + } |
| 83 | + |
| 84 | + return b.array(this.nodes.map(objectify)); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @param {Node} item |
| 90 | + */ |
| 91 | +function stringify(item) { |
| 92 | + if (item.type === 'text') { |
| 93 | + return item.nodes.map((node) => node.raw).join(''); |
| 94 | + } |
| 95 | + |
| 96 | + if (item.type === 'anchor') { |
| 97 | + return item.data ? `<!--${item.data}-->` : '<!>'; |
| 98 | + } |
| 99 | + |
| 100 | + let str = `<${item.name}`; |
| 101 | + |
| 102 | + for (const key in item.attributes) { |
| 103 | + const value = item.attributes[key]; |
| 104 | + |
| 105 | + str += ` ${key}`; |
| 106 | + if (value !== undefined) str += `="${escape_html(value, true)}"`; |
| 107 | + } |
| 108 | + |
| 109 | + str += `>`; |
| 110 | + str += item.children.map(stringify).join(''); |
| 111 | + |
| 112 | + if (!is_void(item.name)) { |
| 113 | + str += `</${item.name}>`; |
| 114 | + } |
| 115 | + |
| 116 | + return str; |
| 117 | +} |
| 118 | + |
| 119 | +/** @param {Node} item */ |
| 120 | +function objectify(item) { |
| 121 | + if (item.type === 'text') { |
| 122 | + return b.literal(item.nodes.map((node) => node.data).join('')); |
| 123 | + } |
| 124 | + |
| 125 | + if (item.type === 'anchor') { |
| 126 | + return item.data ? b.array([b.literal(`// ${item.data}`)]) : null; |
| 127 | + } |
| 128 | + |
| 129 | + const element = b.array([b.literal(item.name)]); |
| 130 | + |
| 131 | + const attributes = b.object([]); |
| 132 | + |
| 133 | + for (const key in item.attributes) { |
| 134 | + const value = item.attributes[key]; |
| 135 | + |
| 136 | + attributes.properties.push( |
| 137 | + b.prop( |
| 138 | + 'init', |
| 139 | + b.key(fix_attribute_casing(key)), |
| 140 | + value === undefined ? b.void0 : b.literal(value) |
| 141 | + ) |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + if (attributes.properties.length > 0 || item.children.length > 0) { |
| 146 | + element.elements.push(attributes.properties.length > 0 ? attributes : b.null); |
| 147 | + } |
| 148 | + |
| 149 | + if (item.children.length > 0) { |
| 150 | + const children = item.children.map(objectify); |
| 151 | + element.elements.push(...children); |
| 152 | + |
| 153 | + // special case — strip leading newline from `<pre>` and `<textarea>` |
| 154 | + if (item.name === 'pre' || item.name === 'textarea') { |
| 155 | + const first = children[0]; |
| 156 | + if (first?.type === 'Literal') { |
| 157 | + first.value = /** @type {string} */ (first.value).replace(regex_starts_with_newline, ''); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return element; |
68 | 163 | } |
0 commit comments