Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8a5ea74

Browse files
committedFeb 20, 2024
fix: only escape characters in SSR template
Adjusts the escaping mechanism done for server compilation. For template literals it's now only applied when explicitly told, which is the case for generated literals from the html template. Fixes a bug where a template literal string inside the `@html` tag was wrongfully escaped (#10359 (comment))
1 parent b80d9bd commit 8a5ea74

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed
 

‎.changeset/neat-boats-shake.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: only escape characters in SSR template

‎packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ function t_string(value) {
3232

3333
/**
3434
* @param {import('estree').Expression} value
35+
* @param {boolean} [needs_escaping]
3536
* @returns {import('./types').TemplateExpression}
3637
*/
37-
function t_expression(value) {
38-
return { type: 'expression', value };
38+
function t_expression(value, needs_escaping = false) {
39+
return { type: 'expression', value, needs_escaping };
3940
}
4041

4142
/**
@@ -94,7 +95,8 @@ function serialize_template(template, out = b.id('out')) {
9495
} else if (template_item.type === 'expression') {
9596
const value = template_item.value;
9697
if (value.type === 'TemplateLiteral') {
97-
last.value.raw += sanitize_template_string(value.quasis[0].value.raw);
98+
const raw = value.quasis[0].value.raw;
99+
last.value.raw += template_item.needs_escaping ? sanitize_template_string(raw) : raw;
98100
quasis.push(...value.quasis.slice(1));
99101
expressions.push(...value.expressions);
100102
continue;
@@ -198,7 +200,7 @@ function process_children(nodes, parent, { visit, state }) {
198200
}
199201
}
200202

201-
state.template.push(t_expression(b.template(quasis, expressions)));
203+
state.template.push(t_expression(b.template(quasis, expressions), true));
202204
}
203205

204206
for (let i = 0; i < nodes.length; i += 1) {

‎packages/svelte/src/compiler/phases/3-transform/server/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { ComponentAnalysis } from '../../types.js';
1212
export type TemplateExpression = {
1313
type: 'expression';
1414
value: Expression;
15+
needs_escaping: boolean;
1516
};
1617

1718
export type TemplateString = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `s s s \\u73`
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{@html `\u{73}`}
2+
{@html '\u{73}'}
3+
{@html "\u{73}"}
4+
5+
\u{73}

0 commit comments

Comments
 (0)
Please sign in to comment.