From 60813bc18dfa34ee9d3b478326d1bb7565dc25c4 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 21 May 2025 17:48:51 -0400 Subject: [PATCH] tweak --- .../client/transform-template/index.js | 2 +- .../client/transform-template/to-string.js | 61 +++++++++---------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js index 03b8a0ef4f..a643a385fe 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js @@ -77,7 +77,7 @@ export function transform_template(state, context, namespace, template_name, fla const args = [ state.options.templatingMode === 'functional' ? 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) { diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-template/to-string.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-template/to-string.js index 174f7d71e3..6687b30b49 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-template/to-string.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-template/to-string.js @@ -1,51 +1,46 @@ /** @import { Node } from './types.js' */ import { escape_html } from '../../../../../escaping.js'; import { is_void } from '../../../../../utils.js'; +import * as b from '../../../../utils/builders.js'; /** * @param {Node[]} 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} el - * @returns + * @param {Node} node */ -function stringify(el) { - let str = ``; - if (el.type === 'element') { - // we create the `; + str += node.children.map(stringify); + + if (!is_void(node.name)) { + str += ``; + } + + return str; } - // then we close the opening tag - str += `>`; - // we stringify all the children and concatenate them - 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 += ``; + + case 'text': { + return node.nodes.map((node) => node.raw).join(''); } - } else if (el.type === 'text') { - str += el.nodes.map((node) => node.raw).join(''); - } else if (el.type === 'anchor') { - if (el.data) { - str += ``; - } else { - str += ``; + + case 'anchor': { + return node.data ? `` : ''; } } - - return str; }