Skip to content

Commit

Permalink
only set a key to VNode have similar shape, fix vuejs#5618
Browse files Browse the repository at this point in the history
  • Loading branch information
pengchongfu committed May 7, 2017
1 parent b977c77 commit 920ddc2
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/core/vdom/helpers/normalize-children.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import VNode, { createTextVNode } from 'core/vdom/vnode'
import { isDef, isUndef, isPrimitive } from 'shared/util'
import { isDef, isUndef, isPrimitive, isObject } from 'shared/util'

// The template compiler attempts to minimize the need for normalization by
// statically analyzing the template at compile time.
Expand Down Expand Up @@ -45,7 +45,11 @@ function normalizeArrayChildren (children: any, nestedIndex?: string): Array<VNo
last = res[res.length - 1]
// nested
if (Array.isArray(c)) {
res.push.apply(res, normalizeArrayChildren(c, `${nestedIndex || ''}_${i}`))
if (hasNestedIndex(c)) {
res.push.apply(res, normalizeArrayChildren(c, `${nestedIndex || ''}_${i}`))
} else {
res.push.apply(res, normalizeArrayChildren(c))
}
} else if (isPrimitive(c)) {
if (isDef(last) && isDef(last.text)) {
last.text += String(c)
Expand All @@ -67,3 +71,21 @@ function normalizeArrayChildren (children: any, nestedIndex?: string): Array<VNo
}
return res
}

function hasNestedIndex (children: any): boolean {
const length = children.length
if (length <= 1) return true
if (isObject(children[0]) === false || isUndef(children[0].tag) || isDef(children[0].key)) return false
let i
for (i = 1; i < length; i++) {
if (isObject(children[i]) === false || similarVNode(children[0], children[i]) === false) return false
}
return true
}

function similarVNode (a: VNode, b: VNode): boolean {
return (
a.tag === b.tag &&
a.key === b.key
)
}

0 comments on commit 920ddc2

Please sign in to comment.