From a32870a8f611dd1146bb17d5605b168a7805c73f Mon Sep 17 00:00:00 2001 From: underfin Date: Tue, 15 Sep 2020 04:02:29 +0800 Subject: [PATCH] fix: inherit `el` for static nodes inside keyed `template` fragment (#2089) fix #2080 --- .../__tests__/rendererFragment.spec.ts | 48 ++++++++++++++++++- packages/runtime-core/src/renderer.ts | 18 +++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/packages/runtime-core/__tests__/rendererFragment.spec.ts b/packages/runtime-core/__tests__/rendererFragment.spec.ts index 9e1f87aec8e..93140f13f4e 100644 --- a/packages/runtime-core/__tests__/rendererFragment.spec.ts +++ b/packages/runtime-core/__tests__/rendererFragment.spec.ts @@ -10,9 +10,13 @@ import { dumpOps, NodeOpTypes, serializeInner, - createTextVNode + createTextVNode, + createBlock, + openBlock, + createCommentVNode } from '@vue/runtime-test' import { PatchFlags } from '@vue/shared' +import { renderList } from '../src/helpers/renderList' describe('renderer: fragment', () => { it('should allow returning multiple component root nodes', () => { @@ -269,4 +273,46 @@ describe('renderer: fragment', () => { render(null, root) expect(serializeInner(root)).toBe(``) }) + + // #2080 + test('`template` keyed fragment w/ comment + hoisted node', () => { + const root = nodeOps.createElement('div') + const hoisted = h('span') + + const renderFn = (items: string[]) => { + return ( + openBlock(true), + createBlock( + Fragment, + null, + renderList(items, item => { + return ( + openBlock(), + createBlock( + Fragment, + { key: item }, + [ + createCommentVNode('comment'), + hoisted, + createVNode('div', null, item, PatchFlags.TEXT) + ], + PatchFlags.STABLE_FRAGMENT + ) + ) + }), + PatchFlags.KEYED_FRAGMENT + ) + ) + } + + render(renderFn(['one', 'two']), root) + expect(serializeInner(root)).toBe( + `
one
two
` + ) + + render(renderFn(['two', 'one']), root) + expect(serializeInner(root)).toBe( + `
two
one
` + ) + }) }) diff --git a/packages/runtime-core/src/renderer.ts b/packages/runtime-core/src/renderer.ts index 670cd41f5c0..5246fcb1baa 100644 --- a/packages/runtime-core/src/renderer.ts +++ b/packages/runtime-core/src/renderer.ts @@ -1153,8 +1153,10 @@ function baseCreateRenderer( parentSuspense, isSVG ) - if (__DEV__ && parentComponent && parentComponent.type.__hmrId) { - traverseStaticChildren(n1, n2) + // #2080 if the stable fragment has a key, it's a