Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-dom): ensure to v-text enders correctly #7995

Merged
merged 6 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 whit 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 whit escape', () => {
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
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()
})
})
6 changes: 4 additions & 2 deletions packages/compiler-dom/src/transforms/stringifyStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
stringifyStyle,
makeMap,
isKnownSvgAttr,
isBooleanAttr
isBooleanAttr,
removeEscapes
} from '@vue/shared'
import { DOMNamespaces } from '../parserOptions'

Expand Down Expand Up @@ -344,6 +345,7 @@ function stringifyElement(
if (!isVoidTag(node.tag)) {
res += `</${node.tag}>`
}
console.log(res)
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
return res
}

Expand All @@ -356,7 +358,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 ${removeEscapes(exp.content)}`)()
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
} else {
// compound
let res = ``
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './escapeHtml'
export * from './looseEqual'
export * from './toDisplayString'
export * from './typeUtils'
export * from './removeEscapes'
2 changes: 2 additions & 0 deletions packages/shared/src/removeEscapes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const removeEscapes = (content: string) =>
content.replace(/[\n\r\t\f\v\\]/g, '')