Explorar o código

fix(compiler-dom): handle newlines when evaluating constants during stringification (#7995)

fix #7994
白雾三语 %!s(int64=3) %!d(string=hai) anos
pai
achega
5261085113

+ 10 - 0
packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap

@@ -52,3 +52,13 @@ return function render(_ctx, _cache) {
   return _hoisted_1
 }"
 `;
+
+exports[`stringify static html > stringify v-text with escape 1`] = `
+"const { createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode } = Vue
+
+const _hoisted_1 = /*#__PURE__*/_createStaticVNode(\\"<pre data-type=\\\\\\"js\\\\\\"><code>text1</code></pre><div class><span class>1</span><span class>2</span></div>\\", 2)
+
+return function render(_ctx, _cache) {
+  return _hoisted_1
+}"
+`;

+ 11 - 0
packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts

@@ -477,4 +477,15 @@ describe('stringify static html', () => {
     expect(code).toMatch(`<code>&lt;span&gt;show-it &lt;/span&gt;</code>`)
     expect(code).toMatchSnapshot()
   })
+
+  test('stringify v-text with escape', () => {
+    const { code } = compileWithStringify(`
+      <pre  data-type="js"><code v-text="
+                \`text1\`"></code></pre>
+      <div class>
+        <span class>1</span><span class>2</span>
+      </div>`)
+    expect(code).toMatch(`<code>text1</code>`)
+    expect(code).toMatchSnapshot()
+  })
 })

+ 1 - 1
packages/compiler-dom/src/transforms/stringifyStatic.ts

@@ -356,7 +356,7 @@ function stringifyElement(
 // (see compiler-core/src/transforms/transformExpression)
 function evaluateConstant(exp: ExpressionNode): string {
   if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
-    return new Function(`return ${exp.content}`)()
+    return new Function(`return (${exp.content})`)()
   } else {
     // compound
     let res = ``