fix(compiler-dom): nodes with v-once shouldn't be stringified (#13878)

This commit is contained in:
Arman Tang 2025-09-24 17:13:44 +08:00 committed by GitHub
parent 4b7170625d
commit 95c1975604
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 0 deletions

View File

@ -12,6 +12,24 @@ return function render(_ctx, _cache) {
}"
`;
exports[`stringify static html > eligible content + v-once node 1`] = `
"const { setBlockTracking: _setBlockTracking, toDisplayString: _toDisplayString, createTextVNode: _createTextVNode, createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
return function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock("div", null, [
_cache[0] || (
_setBlockTracking(-1, true),
(_cache[0] = _createElementVNode("div", null, [
_createTextVNode(_toDisplayString(_ctx.msg), 1 /* TEXT */)
])).cacheIndex = 0,
_setBlockTracking(1),
_cache[0]
),
_cache[1] || (_cache[1] = _createStaticVNode("<span class=\\"foo\\">foo</span><span class=\\"foo\\">foo</span><span class=\\"foo\\">foo</span><span class=\\"foo\\">foo</span><span class=\\"foo\\">foo</span>", 5))
]))
}"
`;
exports[`stringify static html > escape 1`] = `
"const { toDisplayString: _toDisplayString, normalizeClass: _normalizeClass, createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue

View File

@ -525,4 +525,14 @@ describe('stringify static html', () => {
expect(code).toMatchSnapshot()
})
test('eligible content + v-once node', () => {
const { code } = compileWithStringify(
`<div>
<div v-once>{{ msg }}</div>
${repeat(`<span class="foo">foo</span>`, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT)}
</div>`,
)
expect(code).toMatchSnapshot()
})
})

View File

@ -17,6 +17,7 @@ import {
type TextCallNode,
type TransformContext,
createCallExpression,
findDir,
isStaticArgOf,
} from '@vue/compiler-core'
import {
@ -213,6 +214,11 @@ function analyzeNode(node: StringifiableNode): [number, number] | false {
return false
}
// v-once nodes should not be stringified
if (node.type === NodeTypes.ELEMENT && findDir(node, 'once', true)) {
return false
}
if (node.type === NodeTypes.TEXT_CALL) {
return [1, 0]
}