Skip to content

Commit 1b60a88

Browse files
committed
adjust sameVnode check (fix #3176)
1 parent feee9ef commit 1b60a88

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/core/vdom/patch.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ function isDef (s) {
2323
}
2424

2525
function sameVnode (vnode1, vnode2) {
26-
return vnode1.key === vnode2.key && vnode1.tag === vnode2.tag
26+
return (
27+
vnode1.key === vnode2.key &&
28+
vnode1.tag === vnode2.tag &&
29+
!vnode1.data === !vnode2.data
30+
)
2731
}
2832

2933
function createKeyToOldIdx (children, beginIdx, endIdx) {
@@ -269,12 +273,8 @@ export function createPatchFunction (backend) {
269273
if (oldVnode === vnode) return
270274
let i, hook
271275
const hasData = isDef(i = vnode.data)
272-
if (hasData) {
273-
// ensure the oldVnode also has data during patch
274-
oldVnode.data = oldVnode.data || emptyData
275-
if (isDef(hook = i.hook) && isDef(i = hook.prepatch)) {
276-
i(oldVnode, vnode)
277-
}
276+
if (hasData && isDef(hook = i.hook) && isDef(i = hook.prepatch)) {
277+
i(oldVnode, vnode)
278278
}
279279
const elm = vnode.elm = oldVnode.elm
280280
const oldCh = oldVnode.children

test/unit/modules/vdom/patch/children.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,20 @@ describe('children', () => {
450450
expect(map(tag, elm.children)).toEqual(['DIV', 'SPAN', 'SPAN', 'DIV'])
451451
expect(map(inner, elm.children)).toEqual(['four', 'three', 'two', 'one'])
452452
})
453+
454+
it('should handle children with the same tag, same key, but one with data and one without data', () => {
455+
const vnode1 = new VNode('div', {}, [
456+
new VNode('div', { class: 'hi' }, undefined, 'one')
457+
])
458+
const vnode2 = new VNode('div', {}, [
459+
new VNode('div', undefined, undefined, 'four')
460+
])
461+
let elm = patch(vnode0, vnode1)
462+
const child1 = elm.children[0]
463+
expect(child1.className).toBe('hi')
464+
elm = patch(vnode1, vnode2)
465+
const child2 = elm.children[0]
466+
expect(child1).not.toBe(child2)
467+
expect(child2.className).toBe('')
468+
})
453469
})

test/unit/modules/vdom/patch/hooks.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ describe('hooks', () => {
242242
new VNode('span', {}, undefined, 'child 2')
243243
])
244244
])
245-
const vnode2 = new VNode('div')
245+
const vnode2 = new VNode('div', {})
246246
patch1(vnode0, vnode1)
247247
expect(destroyed).toBe(1) // should invoke for replaced root nodes too
248248
patch1(vnode1, vnode2)
@@ -265,7 +265,7 @@ describe('hooks', () => {
265265
createTextVNode(''),
266266
new VNode('span', {}, undefined, 'third child')
267267
])
268-
const vnode2 = new VNode('div')
268+
const vnode2 = new VNode('div', {})
269269
patch1(vnode0, vnode1)
270270
patch1(vnode1, vnode2)
271271
expect(created).toBe(3)
@@ -292,7 +292,7 @@ describe('hooks', () => {
292292
])
293293
])
294294
])
295-
const vnode2 = new VNode('div')
295+
const vnode2 = new VNode('div', {})
296296
patch1(vnode0, vnode1)
297297
expect(destroyed).toBe(1) // should invoke for replaced root nodes too
298298
patch1(vnode1, vnode2)

0 commit comments

Comments
 (0)