Skip to content

Commit 314ab2c

Browse files
authored
fix(compiler): stringify values on v-text (#2432)
fix #2430
1 parent edd49dc commit 314ab2c

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

packages/compiler-dom/__tests__/__snapshots__/index.spec.ts.snap

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ exports[`compile should contain standard transforms 1`] = `
55
66
return function render(_ctx, _cache) {
77
with (_ctx) {
8-
const { createVNode: _createVNode, Fragment: _Fragment, openBlock: _openBlock, createBlock: _createBlock } = _Vue
8+
const { toDisplayString: _toDisplayString, createVNode: _createVNode, Fragment: _Fragment, openBlock: _openBlock, createBlock: _createBlock } = _Vue
99
1010
return (_openBlock(), _createBlock(_Fragment, null, [
11-
_createVNode(\\"div\\", { textContent: text }, null, 8 /* PROPS */, [\\"textContent\\"]),
11+
_createVNode(\\"div\\", {
12+
textContent: _toDisplayString(text)
13+
}, null, 8 /* PROPS */, [\\"textContent\\"]),
1214
_createVNode(\\"div\\", { innerHTML: html }, null, 8 /* PROPS */, [\\"innerHTML\\"]),
1315
_createVNode(\\"div\\", null, \\"test\\"),
1416
_createVNode(\\"div\\", { style: {\\"color\\":\\"red\\"} }, \\"red\\"),

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ describe('compiler: v-text transform', () => {
3131
expect((ast.children[0] as PlainElementNode).codegenNode).toMatchObject({
3232
tag: `"div"`,
3333
props: createObjectMatcher({
34-
textContent: `[test]`
34+
textContent: {
35+
arguments: [{ content: 'test' }]
36+
}
3537
}),
3638
children: undefined,
3739
patchFlag: genFlagText(PatchFlags.PROPS),
@@ -50,7 +52,9 @@ describe('compiler: v-text transform', () => {
5052
expect((ast.children[0] as PlainElementNode).codegenNode).toMatchObject({
5153
tag: `"div"`,
5254
props: createObjectMatcher({
53-
textContent: `[test]`
55+
textContent: {
56+
arguments: [{ content: 'test' }]
57+
}
5458
}),
5559
children: undefined, // <-- children should have been removed
5660
patchFlag: genFlagText(PatchFlags.PROPS),

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
DirectiveTransform,
33
createObjectProperty,
4-
createSimpleExpression
4+
createSimpleExpression,
5+
TO_DISPLAY_STRING,
6+
createCallExpression
57
} from '@vue/compiler-core'
68
import { createDOMCompilerError, DOMErrorCodes } from '../errors'
79

@@ -21,8 +23,14 @@ export const transformVText: DirectiveTransform = (dir, node, context) => {
2123
return {
2224
props: [
2325
createObjectProperty(
24-
createSimpleExpression(`textContent`, true, loc),
25-
exp || createSimpleExpression('', true)
26+
createSimpleExpression(`textContent`, true),
27+
exp
28+
? createCallExpression(
29+
context.helperString(TO_DISPLAY_STRING),
30+
[exp],
31+
loc
32+
)
33+
: createSimpleExpression('', true)
2634
)
2735
]
2836
}

0 commit comments

Comments
 (0)