From 4049ffcf29dc12dca71f682edf0b422a5c502e23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=A2=AB=E9=9B=A8=E6=B0=B4=E8=BF=87=E6=BB=A4=E7=9A=84?=
=?UTF-8?q?=E7=A9=BA=E6=B0=94=28Rairn=29?= <958414905@qq.com>
Date: Thu, 10 Nov 2022 18:03:10 +0800
Subject: [PATCH] fix(runtime-core): fix move/removal of static fragments
containing text nodes (#6858)
fix #6852
---
.../__tests__/rendererFragment.spec.ts | 36 +++++++++++++++++++
packages/runtime-core/src/renderer.ts | 4 +++
2 files changed, 40 insertions(+)
diff --git a/packages/runtime-core/__tests__/rendererFragment.spec.ts b/packages/runtime-core/__tests__/rendererFragment.spec.ts
index 93140f13f4e..1de73ef632c 100644
--- a/packages/runtime-core/__tests__/rendererFragment.spec.ts
+++ b/packages/runtime-core/__tests__/rendererFragment.spec.ts
@@ -315,4 +315,40 @@ describe('renderer: fragment', () => {
`
two
one
`
)
})
+
+ // #6852
+ test('`template` keyed fragment w/ text', () => {
+ const root = nodeOps.createElement('div')
+
+ const renderFn = (items: string[]) => {
+ return (
+ openBlock(true),
+ createBlock(
+ Fragment,
+ null,
+ renderList(items, item => {
+ return (
+ openBlock(),
+ createBlock(
+ Fragment,
+ { key: item },
+ [
+ createTextVNode('text'),
+ createVNode('div', null, item, PatchFlags.TEXT)
+ ],
+ PatchFlags.STABLE_FRAGMENT
+ )
+ )
+ }),
+ PatchFlags.KEYED_FRAGMENT
+ )
+ )
+ }
+
+ render(renderFn(['one', 'two']), root)
+ expect(serializeInner(root)).toBe(`textone
texttwo
`)
+
+ render(renderFn(['two', 'one']), root)
+ expect(serializeInner(root)).toBe(`texttwo
textone
`)
+ })
})
diff --git a/packages/runtime-core/src/renderer.ts b/packages/runtime-core/src/renderer.ts
index c043492e1df..4a6d8993a49 100644
--- a/packages/runtime-core/src/renderer.ts
+++ b/packages/runtime-core/src/renderer.ts
@@ -2386,6 +2386,10 @@ export function traverseStaticChildren(n1: VNode, n2: VNode, shallow = false) {
}
if (!shallow) traverseStaticChildren(c1, c2)
}
+ // #6852 also inherit for text nodes
+ if (c2.type === Text) {
+ c2.el = c1.el
+ }
// also inherit for comment nodes, but not placeholders (e.g. v-if which
// would have received .el during block patch)
if (__DEV__ && c2.type === Comment && !c2.el) {