pull/15538/head
Rich Harris 4 months ago
parent d8637fbb75
commit 84f15d005c

@ -17,53 +17,46 @@ export function template_to_functions(items) {
/** @param {Node} item */ /** @param {Node} item */
function build(item) { function build(item) {
switch (item.type) { if (item.type === 'text') {
case 'element': { return b.literal(item.nodes.map((node) => node.data).join(''));
const element = b.array([b.literal(item.name)]); }
const attributes = b.object([]);
for (const key in item.attributes) { if (item.type === 'anchor') {
const value = item.attributes[key]; return item.data ? b.array([b.literal(`// ${item.data}`)]) : null;
}
attributes.properties.push( const element = b.array([b.literal(item.name)]);
b.prop(
'init',
b.key(fix_attribute_casing(key)),
value === undefined ? b.void0 : b.literal(value)
)
);
}
if (attributes.properties.length > 0 || item.children.length > 0) { const attributes = b.object([]);
element.elements.push(attributes.properties.length > 0 ? attributes : b.null);
}
if (item.children.length > 0) { for (const key in item.attributes) {
const children = item.children.map(build); const value = item.attributes[key];
element.elements.push(...children);
// special case — strip leading newline from `<pre>` and `<textarea>` attributes.properties.push(
if (item.name === 'pre' || item.name === 'textarea') { b.prop(
const first = children[0]; 'init',
if (first?.type === 'Literal') { b.key(fix_attribute_casing(key)),
first.value = /** @type {string} */ (first.value).replace( value === undefined ? b.void0 : b.literal(value)
regex_starts_with_newline, )
'' );
); }
}
}
}
return element; if (attributes.properties.length > 0 || item.children.length > 0) {
} element.elements.push(attributes.properties.length > 0 ? attributes : b.null);
}
case 'anchor': { if (item.children.length > 0) {
return item.data ? b.array([b.literal(`// ${item.data}`)]) : null; const children = item.children.map(build);
} element.elements.push(...children);
case 'text': { // special case — strip leading newline from `<pre>` and `<textarea>`
return b.literal(item.nodes.map((node) => node.data).join('')); if (item.name === 'pre' || item.name === 'textarea') {
const first = children[0];
if (first?.type === 'Literal') {
first.value = /** @type {string} */ (first.value).replace(regex_starts_with_newline, '');
}
} }
} }
return element;
} }

Loading…
Cancel
Save