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 */
function build(item) {
switch (item.type) {
case 'element': {
const element = b.array([b.literal(item.name)]);
const attributes = b.object([]);
if (item.type === 'text') {
return b.literal(item.nodes.map((node) => node.data).join(''));
}
for (const key in item.attributes) {
const value = item.attributes[key];
if (item.type === 'anchor') {
return item.data ? b.array([b.literal(`// ${item.data}`)]) : null;
}
attributes.properties.push(
b.prop(
'init',
b.key(fix_attribute_casing(key)),
value === undefined ? b.void0 : b.literal(value)
)
);
}
const element = b.array([b.literal(item.name)]);
if (attributes.properties.length > 0 || item.children.length > 0) {
element.elements.push(attributes.properties.length > 0 ? attributes : b.null);
}
const attributes = b.object([]);
if (item.children.length > 0) {
const children = item.children.map(build);
element.elements.push(...children);
for (const key in item.attributes) {
const value = item.attributes[key];
// special case — strip leading newline from `<pre>` and `<textarea>`
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,
''
);
}
}
}
attributes.properties.push(
b.prop(
'init',
b.key(fix_attribute_casing(key)),
value === undefined ? b.void0 : b.literal(value)
)
);
}
return element;
}
if (attributes.properties.length > 0 || item.children.length > 0) {
element.elements.push(attributes.properties.length > 0 ? attributes : b.null);
}
case 'anchor': {
return item.data ? b.array([b.literal(`// ${item.data}`)]) : null;
}
if (item.children.length > 0) {
const children = item.children.map(build);
element.elements.push(...children);
case 'text': {
return b.literal(item.nodes.map((node) => node.data).join(''));
// special case — strip leading newline from `<pre>` and `<textarea>`
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