Skip to content

only set a key to VNode have similar shape, fix #5618 #5622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`))
Copy link
Member

@gebilaoxiong gebilaoxiong May 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here only need to determine whether c is created by slot, avoid to set key

Copy link
Member

@gebilaoxiong gebilaoxiong May 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me fix it 🙂

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
)
}