]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): fix move/removal of static fragments containing text nodes (#6858)
author被雨水过滤的空气(Rairn) <958414905@qq.com>
Thu, 10 Nov 2022 10:03:10 +0000 (18:03 +0800)
committerGitHub <noreply@github.com>
Thu, 10 Nov 2022 10:03:10 +0000 (05:03 -0500)
fix #6852

packages/runtime-core/__tests__/rendererFragment.spec.ts
packages/runtime-core/src/renderer.ts

index 93140f13f4efc697a3a8cdeee88137676b2af030..1de73ef632c819383a623fc4c4c666901dd9b96d 100644 (file)
@@ -315,4 +315,40 @@ describe('renderer: fragment', () => {
       `<!--comment--><span></span><div>two</div><!--comment--><span></span><div>one</div>`
     )
   })
+
+  // #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(`text<div>one</div>text<div>two</div>`)
+
+    render(renderFn(['two', 'one']), root)
+    expect(serializeInner(root)).toBe(`text<div>two</div>text<div>one</div>`)
+  })
 })
index c043492e1df42192cb5c3520608b2bfa4383050b..4a6d8993a491768cae6ecf20dce88c5f2a8d49a9 100644 (file)
@@ -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) {