From 52610851137b9c5f6f57d771fd604fba309b3c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E9=9B=BE=E4=B8=89=E8=AF=AD?= <32354856+baiwusanyu-c@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:33:00 +0800 Subject: [PATCH] fix(compiler-dom): handle newlines when evaluating constants during stringification (#7995) fix #7994 --- .../__snapshots__/stringifyStatic.spec.ts.snap | 10 ++++++++++ .../__tests__/transforms/stringifyStatic.spec.ts | 11 +++++++++++ .../compiler-dom/src/transforms/stringifyStatic.ts | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap b/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap index da24c26551e..b671d3252e5 100644 --- a/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap +++ b/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(\\"
text1
<span>show-it </span>
`)
expect(code).toMatchSnapshot()
})
+
+ test('stringify v-text with escape', () => {
+ const { code } = compileWithStringify(`
+
+ text1
`)
+ expect(code).toMatchSnapshot()
+ })
})
diff --git a/packages/compiler-dom/src/transforms/stringifyStatic.ts b/packages/compiler-dom/src/transforms/stringifyStatic.ts
index d3f58bdb9da..0b47cb435b0 100644
--- a/packages/compiler-dom/src/transforms/stringifyStatic.ts
+++ b/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 = ``