Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only escape characters in SSR template #10555

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/neat-boats-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: only escape characters in SSR template
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ function t_string(value) {

/**
* @param {import('estree').Expression} value
* @param {boolean} [needs_escaping]
* @returns {import('./types').TemplateExpression}
*/
function t_expression(value) {
return { type: 'expression', value };
function t_expression(value, needs_escaping = false) {
return { type: 'expression', value, needs_escaping };
}

/**
Expand Down Expand Up @@ -94,7 +95,8 @@ function serialize_template(template, out = b.id('out')) {
} else if (template_item.type === 'expression') {
const value = template_item.value;
if (value.type === 'TemplateLiteral') {
last.value.raw += sanitize_template_string(value.quasis[0].value.raw);
const raw = value.quasis[0].value.raw;
last.value.raw += template_item.needs_escaping ? sanitize_template_string(raw) : raw;
quasis.push(...value.quasis.slice(1));
expressions.push(...value.expressions);
continue;
Expand Down Expand Up @@ -198,7 +200,7 @@ function process_children(nodes, parent, { visit, state }) {
}
}

state.template.push(t_expression(b.template(quasis, expressions)));
state.template.push(t_expression(b.template(quasis, expressions), true));
}

for (let i = 0; i < nodes.length; i += 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { ComponentAnalysis } from '../../types.js';
export type TemplateExpression = {
type: 'expression';
value: Expression;
needs_escaping: boolean;
};

export type TemplateString = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `s s s \\u73`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{@html `\u{73}`}
{@html '\u{73}'}
{@html "\u{73}"}

\u{73}
Loading