Skip to content

Commit 60813bc

Browse files
committed
tweak
1 parent 747d24a commit 60813bc

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function transform_template(state, context, namespace, template_name, fla
7777
const args = [
7878
state.options.templatingMode === 'functional'
7979
? template_to_functions(state.template.nodes)
80-
: b.template([b.quasi(template_to_string(state.template.nodes), true)], [])
80+
: template_to_string(state.template.nodes)
8181
];
8282

8383
if (flags) {
Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,46 @@
11
/** @import { Node } from './types.js' */
22
import { escape_html } from '../../../../../escaping.js';
33
import { is_void } from '../../../../../utils.js';
4+
import * as b from '../../../../utils/builders.js';
45

56
/**
67
* @param {Node[]} items
78
*/
89
export function template_to_string(items) {
9-
return items.map((el) => stringify(el)).join('');
10+
return b.template([b.quasi(items.map(stringify).join(''), true)], []);
1011
}
1112

1213
/**
13-
*
14-
* @param {Node} el
15-
* @returns
14+
* @param {Node} node
1615
*/
17-
function stringify(el) {
18-
let str = ``;
19-
if (el.type === 'element') {
20-
// we create the <tagname part
21-
str += `<${el.name}`;
22-
// we concatenate all the prop to it
23-
for (let [prop, value] of Object.entries(el.attributes ?? {})) {
24-
if (value == null) {
25-
str += ` ${prop}`;
26-
} else {
27-
str += ` ${prop}="${escape_html(value, true)}"`;
16+
function stringify(node) {
17+
switch (node.type) {
18+
case 'element': {
19+
let str = `<${node.name}`;
20+
21+
for (const key in node.attributes) {
22+
const value = node.attributes[key];
23+
24+
str += ` ${key}`;
25+
if (value !== undefined) str += `="${escape_html(value, true)}"`;
2826
}
27+
28+
str += `>`;
29+
str += node.children.map(stringify);
30+
31+
if (!is_void(node.name)) {
32+
str += `</${node.name}>`;
33+
}
34+
35+
return str;
2936
}
30-
// then we close the opening tag
31-
str += `>`;
32-
// we stringify all the children and concatenate them
33-
for (let child of el.children ?? []) {
34-
str += stringify(child);
35-
}
36-
// if it's not void we also add the closing tag
37-
if (!is_void(el.name)) {
38-
str += `</${el.name}>`;
37+
38+
case 'text': {
39+
return node.nodes.map((node) => node.raw).join('');
3940
}
40-
} else if (el.type === 'text') {
41-
str += el.nodes.map((node) => node.raw).join('');
42-
} else if (el.type === 'anchor') {
43-
if (el.data) {
44-
str += `<!--${el.data}-->`;
45-
} else {
46-
str += `<!>`;
41+
42+
case 'anchor': {
43+
return node.data ? `<!--${node.data}-->` : '<!>';
4744
}
4845
}
49-
50-
return str;
5146
}

0 commit comments

Comments
 (0)