]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-ssr): should escape template string interpolation chars in generated...
authorEvan You <yyx990803@gmail.com>
Tue, 26 May 2020 18:27:01 +0000 (14:27 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 26 May 2020 18:38:24 +0000 (14:38 -0400)
packages/compiler-core/src/codegen.ts
packages/compiler-ssr/__tests__/ssrText.spec.ts

index d7d8b8bfa37c7d7b04ad368c720efd600439f302..d95b22b6bd76835b2c50eb8bf804dc1da1d76605 100644 (file)
@@ -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()
index 281f518f07f2978a6388f0447959b1ff0aa4838f..37c9646d2729689b5d4bffa21c478cba4714cc3c 100644 (file)
@@ -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-->\`"`