Skip to content

Commit 5f15d9a

Browse files
committed
fix(compiler-ssr): should escape template string interpolation chars in generated code
1 parent 1815410 commit 5f15d9a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/compiler-core/src/codegen.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ function genTemplateLiteral(node: TemplateLiteral, context: CodegenContext) {
891891
for (let i = 0; i < l; i++) {
892892
const e = node.elements[i]
893893
if (isString(e)) {
894-
push(e.replace(/`/g, '\\`'))
894+
push(e.replace(/(`|\$|\\)/g, '\\$1'))
895895
} else {
896896
push('${')
897897
if (multilines) indent()

packages/compiler-ssr/__tests__/ssrText.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ describe('ssr: text', () => {
66
expect(getCompiledString(`foo`)).toMatchInlineSnapshot(`"\`foo\`"`)
77
})
88

9+
test('static text with template string special chars', () => {
10+
expect(getCompiledString(`\`\${foo}\``)).toMatchInlineSnapshot(
11+
`"\`\\\\\`\\\\\${foo}\\\\\`\`"`
12+
)
13+
})
14+
15+
test('static text with char escape', () => {
16+
// the desired generated code should be `\\\$foo`
17+
// snapshot -> inline snapshot goes through two escapes
18+
// so that makes a total of 3 * 2 * 2 = 12 back slashes
19+
expect(getCompiledString(`\\$foo`)).toMatchInlineSnapshot(
20+
`"\`\\\\\\\\\\\\$foo\`"`
21+
)
22+
})
23+
924
test('comments', () => {
1025
expect(getCompiledString(`<!--bar-->`)).toMatchInlineSnapshot(
1126
`"\`<!--bar-->\`"`

0 commit comments

Comments
 (0)