Skip to content

Commit

Permalink
fix(compiler-dom): handle newlines when evaluating constants during s…
Browse files Browse the repository at this point in the history
…tringification (#7995)

fix #7994
  • Loading branch information
baiwusanyu-c authored Mar 31, 2023
1 parent 482f2e3 commit 5261085
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 11 additions & 0 deletions packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
2 changes: 1 addition & 1 deletion packages/compiler-dom/src/transforms/stringifyStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ``
Expand Down

0 comments on commit 5261085

Please sign in to comment.