Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c223634

Browse files
fix(patch): clone insert hooks to avoid being mutated during iteration (#12905)
1 parent 51fef2c commit c223634

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

‎src/core/vdom/patch.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,11 @@ export function createPatchFunction(backend) {
878878
const insert = ancestor.data.hook.insert
879879
if (insert.merged) {
880880
// start at index 1 to avoid re-invoking component mounted hook
881-
for (let i = 1; i < insert.fns.length; i++) {
882-
insert.fns[i]()
881+
// clone insert hooks to avoid being mutated during iteration.
882+
// e.g. for customed directives under transition group.
883+
const cloned = insert.fns.slice(1)
884+
for (let i = 0; i < cloned.length; i++) {
885+
cloned[i]()
883886
}
884887
}
885888
} else {

‎test/unit/modules/vdom/patch/edge-cases.spec.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,42 @@ describe('vdom patch: edge cases', () => {
467467
vm.visible = false
468468
vm.$nextTick(done)
469469
})
470+
471+
it('should call directive\'s inserted hook correctly when template root is changed', done => {
472+
let insertCalled = false
473+
const vm = new Vue({
474+
data: {
475+
isOn: false
476+
},
477+
components: {
478+
'v-switch': {
479+
props: {
480+
on: Boolean
481+
},
482+
template: `
483+
<div v-if="on" key="on">On</div>
484+
<div v-else key="off">Off</div>`
485+
}
486+
},
487+
directives: {
488+
foo: {
489+
inserted() {
490+
insertCalled = true
491+
}
492+
}
493+
},
494+
template: `
495+
<transition-group>
496+
<v-switch key="swicth" v-foo :on="isOn"/>
497+
</transition-group>
498+
`
499+
}).$mount()
500+
501+
vm.isOn = true
502+
insertCalled = false
503+
waitForUpdate(() => {
504+
expect(vm.$el.textContent).toMatch('On')
505+
expect(insertCalled).toBe(true)
506+
}).then(done)
507+
})
470508
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /