Skip to content

Commit

Permalink
fix(compiler-ssr): should escape template string interpolation chars …
Browse files Browse the repository at this point in the history
…in generated code
  • Loading branch information
yyx990803 committed May 26, 2020
1 parent 1815410 commit 5f15d9a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/compiler-core/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ function genTemplateLiteral(node: TemplateLiteral, context: CodegenContext) {
for (let i = 0; i < l; i++) {
const e = node.elements[i]
if (isString(e)) {
push(e.replace(/`/g, '\\`'))
push(e.replace(/(`|\$|\\)/g, '\\$1'))
} else {
push('${')
if (multilines) indent()
Expand Down
15 changes: 15 additions & 0 deletions packages/compiler-ssr/__tests__/ssrText.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ describe('ssr: text', () => {
expect(getCompiledString(`foo`)).toMatchInlineSnapshot(`"\`foo\`"`)
})

test('static text with template string special chars', () => {
expect(getCompiledString(`\`\${foo}\``)).toMatchInlineSnapshot(
`"\`\\\\\`\\\\\${foo}\\\\\`\`"`
)
})

test('static text with char escape', () => {
// the desired generated code should be `\\\$foo`
// snapshot -> inline snapshot goes through two escapes
// so that makes a total of 3 * 2 * 2 = 12 back slashes
expect(getCompiledString(`\\$foo`)).toMatchInlineSnapshot(
`"\`\\\\\\\\\\\\$foo\`"`
)
})

test('comments', () => {
expect(getCompiledString(`<!--bar-->`)).toMatchInlineSnapshot(
`"\`<!--bar-->\`"`
Expand Down

0 comments on commit 5f15d9a

Please sign in to comment.