Skip to content

Commit 5261085

Browse files
authored
fix(compiler-dom): handle newlines when evaluating constants during stringification (#7995)
fix #7994
1 parent 482f2e3 commit 5261085

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ return function render(_ctx, _cache) {
5252
return _hoisted_1
5353
}"
5454
`;
55+
56+
exports[`stringify static html > stringify v-text with escape 1`] = `
57+
"const { createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode } = Vue
58+
59+
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)
60+
61+
return function render(_ctx, _cache) {
62+
return _hoisted_1
63+
}"
64+
`;

packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,15 @@ describe('stringify static html', () => {
477477
expect(code).toMatch(`<code>&lt;span&gt;show-it &lt;/span&gt;</code>`)
478478
expect(code).toMatchSnapshot()
479479
})
480+
481+
test('stringify v-text with escape', () => {
482+
const { code } = compileWithStringify(`
483+
<pre data-type="js"><code v-text="
484+
\`text1\`"></code></pre>
485+
<div class>
486+
<span class>1</span><span class>2</span>
487+
</div>`)
488+
expect(code).toMatch(`<code>text1</code>`)
489+
expect(code).toMatchSnapshot()
490+
})
480491
})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function stringifyElement(
356356
// (see compiler-core/src/transforms/transformExpression)
357357
function evaluateConstant(exp: ExpressionNode): string {
358358
if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
359-
return new Function(`return ${exp.content}`)()
359+
return new Function(`return (${exp.content})`)()
360360
} else {
361361
// compound
362362
let res = ``

0 commit comments

Comments
 (0)