Skip to content

Commit

Permalink
adjust sameVnode check (fix vuejs#3176)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 29, 2016
1 parent feee9ef commit 1b60a88
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/core/vdom/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ function isDef (s) {
}

function sameVnode (vnode1, vnode2) {
return vnode1.key === vnode2.key && vnode1.tag === vnode2.tag
return (
vnode1.key === vnode2.key &&
vnode1.tag === vnode2.tag &&
!vnode1.data === !vnode2.data
)
}

function createKeyToOldIdx (children, beginIdx, endIdx) {
Expand Down Expand Up @@ -269,12 +273,8 @@ export function createPatchFunction (backend) {
if (oldVnode === vnode) return
let i, hook
const hasData = isDef(i = vnode.data)
if (hasData) {
// ensure the oldVnode also has data during patch
oldVnode.data = oldVnode.data || emptyData
if (isDef(hook = i.hook) && isDef(i = hook.prepatch)) {
i(oldVnode, vnode)
}
if (hasData && isDef(hook = i.hook) && isDef(i = hook.prepatch)) {
i(oldVnode, vnode)
}
const elm = vnode.elm = oldVnode.elm
const oldCh = oldVnode.children
Expand Down
16 changes: 16 additions & 0 deletions test/unit/modules/vdom/patch/children.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,20 @@ describe('children', () => {
expect(map(tag, elm.children)).toEqual(['DIV', 'SPAN', 'SPAN', 'DIV'])
expect(map(inner, elm.children)).toEqual(['four', 'three', 'two', 'one'])
})

it('should handle children with the same tag, same key, but one with data and one without data', () => {
const vnode1 = new VNode('div', {}, [
new VNode('div', { class: 'hi' }, undefined, 'one')
])
const vnode2 = new VNode('div', {}, [
new VNode('div', undefined, undefined, 'four')
])
let elm = patch(vnode0, vnode1)
const child1 = elm.children[0]
expect(child1.className).toBe('hi')
elm = patch(vnode1, vnode2)
const child2 = elm.children[0]
expect(child1).not.toBe(child2)
expect(child2.className).toBe('')
})
})
6 changes: 3 additions & 3 deletions test/unit/modules/vdom/patch/hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ describe('hooks', () => {
new VNode('span', {}, undefined, 'child 2')
])
])
const vnode2 = new VNode('div')
const vnode2 = new VNode('div', {})
patch1(vnode0, vnode1)
expect(destroyed).toBe(1) // should invoke for replaced root nodes too
patch1(vnode1, vnode2)
Expand All @@ -265,7 +265,7 @@ describe('hooks', () => {
createTextVNode(''),
new VNode('span', {}, undefined, 'third child')
])
const vnode2 = new VNode('div')
const vnode2 = new VNode('div', {})
patch1(vnode0, vnode1)
patch1(vnode1, vnode2)
expect(created).toBe(3)
Expand All @@ -292,7 +292,7 @@ describe('hooks', () => {
])
])
])
const vnode2 = new VNode('div')
const vnode2 = new VNode('div', {})
patch1(vnode0, vnode1)
expect(destroyed).toBe(1) // should invoke for replaced root nodes too
patch1(vnode1, vnode2)
Expand Down

0 comments on commit 1b60a88

Please sign in to comment.