fix #5321 don't throw error when node gets relocated (#5322)

* don't throw error when node gets relocated

* perf: Simplify if check in vdom/patch
This commit is contained in:
Razvan Stoenescu 2017-04-03 06:36:50 +03:00 committed by Evan You
parent 9311876c7c
commit 255b627f39
2 changed files with 45 additions and 1 deletions

View File

@ -234,7 +234,9 @@ export function createPatchFunction (backend) {
function insert (parent, elm, ref) {
if (isDef(parent)) {
if (isDef(ref)) {
nodeOps.insertBefore(parent, elm, ref)
if (ref.parentNode === parent) {
nodeOps.insertBefore(parent, elm, ref)
}
} else {
nodeOps.appendChild(parent, elm)
}

View File

@ -366,4 +366,46 @@ describe('Component', () => {
Vue.config.errorHandler = null
}).then(done)
})
it('relocates node without error', done => {
const el = document.createElement('div')
document.body.appendChild(el)
const target = document.createElement('div')
document.body.appendChild(target)
const Test = {
render (h) {
return h('div', { class: 'test' }, this.$slots.default)
},
mounted () {
target.appendChild(this.$el)
},
beforeDestroy () {
const parent = this.$el.parentNode
if (parent) {
parent.removeChild(this.$el)
}
}
}
const vm = new Vue({
data () {
return {
view: true
}
},
template: `<div><test v-if="view">Test</test></div>`,
components: {
test: Test
}
}).$mount(el)
expect(el.outerHTML).toBe('<div></div>')
expect(target.outerHTML).toBe('<div><div class="test">Test</div></div>')
vm.view = false
waitForUpdate(() => {
expect(el.outerHTML).toBe('<div></div>')
expect(target.outerHTML).toBe('<div></div>')
vm.$destroy()
}).then(done)
})
})