Skip to content

Commit

Permalink
fix(core): clone mounted hoisted vnodes on patch
Browse files Browse the repository at this point in the history
...since they may need to be checked as fragment child
  • Loading branch information
yyx990803 committed Dec 22, 2019
1 parent eda495e commit 47a6a84
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
36 changes: 20 additions & 16 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Fragment,
Comment,
Portal,
cloneIfMounted,
normalizeVNode,
VNode,
VNodeChildren,
Expand Down Expand Up @@ -438,9 +439,9 @@ export function createRenderer<
start: number = 0
) {
for (let i = start; i < children.length; i++) {
const child = optimized
? (children[i] as HostVNode)
: (children[i] = normalizeVNode(children[i]))
const child = (children[i] = optimized
? cloneIfMounted(children[i] as HostVNode)
: normalizeVNode(children[i]))
patch(
null,
child,
Expand Down Expand Up @@ -1198,9 +1199,9 @@ export function createRenderer<
const commonLength = Math.min(oldLength, newLength)
let i
for (i = 0; i < commonLength; i++) {
const nextChild = optimized
? (c2[i] as HostVNode)
: (c2[i] = normalizeVNode(c2[i]))
const nextChild = (c2[i] = optimized
? cloneIfMounted(c2[i] as HostVNode)
: normalizeVNode(c2[i]))
patch(
c1[i],
nextChild,
Expand Down Expand Up @@ -1251,9 +1252,9 @@ export function createRenderer<
// (a b) d e
while (i <= e1 && i <= e2) {
const n1 = c1[i]
const n2 = optimized
? (c2[i] as HostVNode)
: (c2[i] = normalizeVNode(c2[i]))
const n2 = (c2[i] = optimized
? cloneIfMounted(c2[i] as HostVNode)
: normalizeVNode(c2[i]))
if (isSameVNodeType(n1, n2)) {
patch(
n1,
Expand All @@ -1276,9 +1277,9 @@ export function createRenderer<
// d e (b c)
while (i <= e1 && i <= e2) {
const n1 = c1[e1]
const n2 = optimized
? (c2[e2] as HostVNode)
: (c2[e2] = normalizeVNode(c2[e2]))
const n2 = (c2[e2] = optimized
? cloneIfMounted(c2[e2] as HostVNode)
: normalizeVNode(c2[e2]))
if (isSameVNodeType(n1, n2)) {
patch(
n1,
Expand Down Expand Up @@ -1309,10 +1310,13 @@ export function createRenderer<
const nextPos = e2 + 1
const anchor =
nextPos < l2 ? (c2[nextPos] as HostVNode).el : parentAnchor
const n2 = (c2[i] = optimized
? cloneIfMounted(c2[i] as HostVNode)
: normalizeVNode(c2[i]))
while (i <= e2) {
patch(
null,
optimized ? (c2[i] as HostVNode) : (c2[i] = normalizeVNode(c2[i])),
n2,
container,
anchor,
parentComponent,
Expand Down Expand Up @@ -1349,9 +1353,9 @@ export function createRenderer<
// 5.1 build key:index map for newChildren
const keyToNewIndexMap: Map<string | number, number> = new Map()
for (i = s2; i <= e2; i++) {
const nextChild = optimized
? (c2[i] as HostVNode)
: (c2[i] = normalizeVNode(c2[i]))
const nextChild = (c2[i] = optimized
? cloneIfMounted(c2[i] as HostVNode)
: normalizeVNode(c2[i]))
if (nextChild.key != null) {
if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
warn(
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ export function normalizeVNode<T, U>(child: VNodeChild<T, U>): VNode<T, U> {
}
}

// optimized normalization for template-compiled render fns
export function cloneIfMounted(child: VNode): VNode {
return child.el == null ? child : cloneVNode(child)
}

export function normalizeChildren(vnode: VNode, children: unknown) {
let type = 0
if (children == null) {
Expand Down

0 comments on commit 47a6a84

Please sign in to comment.