pull/15538/head
Rich Harris 4 months ago
parent 747d24a8ad
commit 60813bc18d

@ -77,7 +77,7 @@ export function transform_template(state, context, namespace, template_name, fla
const args = [ const args = [
state.options.templatingMode === 'functional' state.options.templatingMode === 'functional'
? template_to_functions(state.template.nodes) ? template_to_functions(state.template.nodes)
: b.template([b.quasi(template_to_string(state.template.nodes), true)], []) : template_to_string(state.template.nodes)
]; ];
if (flags) { if (flags) {

@ -1,51 +1,46 @@
/** @import { Node } from './types.js' */ /** @import { Node } from './types.js' */
import { escape_html } from '../../../../../escaping.js'; import { escape_html } from '../../../../../escaping.js';
import { is_void } from '../../../../../utils.js'; import { is_void } from '../../../../../utils.js';
import * as b from '../../../../utils/builders.js';
/** /**
* @param {Node[]} items * @param {Node[]} items
*/ */
export function template_to_string(items) { export function template_to_string(items) {
return items.map((el) => stringify(el)).join(''); return b.template([b.quasi(items.map(stringify).join(''), true)], []);
} }
/** /**
* * @param {Node} node
* @param {Node} el
* @returns
*/ */
function stringify(el) { function stringify(node) {
let str = ``; switch (node.type) {
if (el.type === 'element') { case 'element': {
// we create the <tagname part let str = `<${node.name}`;
str += `<${el.name}`;
// we concatenate all the prop to it for (const key in node.attributes) {
for (let [prop, value] of Object.entries(el.attributes ?? {})) { const value = node.attributes[key];
if (value == null) {
str += ` ${prop}`; str += ` ${key}`;
} else { if (value !== undefined) str += `="${escape_html(value, true)}"`;
str += ` ${prop}="${escape_html(value, true)}"`;
} }
str += `>`;
str += node.children.map(stringify);
if (!is_void(node.name)) {
str += `</${node.name}>`;
}
return str;
} }
// then we close the opening tag
str += `>`; case 'text': {
// we stringify all the children and concatenate them return node.nodes.map((node) => node.raw).join('');
for (let child of el.children ?? []) {
str += stringify(child);
}
// if it's not void we also add the closing tag
if (!is_void(el.name)) {
str += `</${el.name}>`;
} }
} else if (el.type === 'text') {
str += el.nodes.map((node) => node.raw).join(''); case 'anchor': {
} else if (el.type === 'anchor') { return node.data ? `<!--${node.data}-->` : '<!>';
if (el.data) {
str += `<!--${el.data}-->`;
} else {
str += `<!>`;
} }
} }
return str;
} }

Loading…
Cancel
Save