Skip to content

fix wrong order of generate modifier code #5147

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

Merged
merged 3 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/compiler/codegen/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ function genHandler (
: `function($event){${handler.value}}` // inline statement
} else {
let code = ''
let genModifierCode = ''
const keys = []
for (const key in handler.modifiers) {
if (modifierCode[key]) {
code += modifierCode[key]
genModifierCode += modifierCode[key]
// left/right
if (keyCodes[key]) {
keys.push(key)
Expand All @@ -78,6 +79,10 @@ function genHandler (
if (keys.length) {
code += genKeyFilter(keys)
}
// Make sure modifiers like prevent and stop get executed after key filtering
if (genModifierCode) {
code += genModifierCode
}
const handlerCode = isMethodPath
? handler.value + '($event)'
: isFunctionExpression
Expand Down
21 changes: 21 additions & 0 deletions test/unit/features/directives/on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,25 @@ describe('Directive v-on', () => {
expect(spyUp.calls.count()).toBe(1)
expect(spyDown.calls.count()).toBe(1)
})

// Github Issues #5146
it('should only prevent when match keycode', () => {
let prevented = false
vm = new Vue({
el,
template: `
<input ref="input" @keydown.enter.prevent="foo">
`,
methods: {
foo ($event) {
prevented = $event.defaultPrevented
}
}
})

triggerEvent(vm.$refs.input, 'keydown', e => { e.keyCode = 32 })
expect(prevented).toBe(false)
triggerEvent(vm.$refs.input, 'keydown', e => { e.keyCode = 13 })
expect(prevented).toBe(true)
})
})
13 changes: 13 additions & 0 deletions test/unit/modules/compiler/codegen.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ describe('codegen', () => {
)
})

// Github Issues #5146
it('generate events with generic modifiers and keycode correct order', () => {
assertCodegen(
'<input @keydown.enter.prevent="onInput">',
`with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13))return null;$event.preventDefault();onInput($event)}}})}`
)

assertCodegen(
'<input @keydown.enter.stop="onInput">',
`with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13))return null;$event.stopPropagation();onInput($event)}}})}`
)
})

it('generate events with mouse event modifiers', () => {
assertCodegen(
'<input @click.ctrl="onClick">',
Expand Down