From 4964b253d0dac29578458b8f1fd9dced4828be0c Mon Sep 17 00:00:00 2001
From: kingwl <805037171@163.com>
Date: Fri, 10 Mar 2017 09:42:14 +0800
Subject: [PATCH] fix wrong order of generate modifier code (#5147)
* fix wrong order of generate modifier code
* add unit test of #5146
* Update events.js
---
src/compiler/codegen/events.js | 7 ++++++-
test/unit/features/directives/on.spec.js | 21 +++++++++++++++++++++
test/unit/modules/compiler/codegen.spec.js | 13 +++++++++++++
3 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/src/compiler/codegen/events.js b/src/compiler/codegen/events.js
index ce0ae289..a6cf166d 100644
--- a/src/compiler/codegen/events.js
+++ b/src/compiler/codegen/events.js
@@ -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)
@@ -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
diff --git a/test/unit/features/directives/on.spec.js b/test/unit/features/directives/on.spec.js
index e7f43bc7..10f99b91 100644
--- a/test/unit/features/directives/on.spec.js
+++ b/test/unit/features/directives/on.spec.js
@@ -527,4 +527,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: `
+
+ `,
+ 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)
+ })
})
diff --git a/test/unit/modules/compiler/codegen.spec.js b/test/unit/modules/compiler/codegen.spec.js
index 0fea71a0..ba4374cb 100644
--- a/test/unit/modules/compiler/codegen.spec.js
+++ b/test/unit/modules/compiler/codegen.spec.js
@@ -268,6 +268,19 @@ describe('codegen', () => {
)
})
+ // Github Issues #5146
+ it('generate events with generic modifiers and keycode correct order', () => {
+ assertCodegen(
+ '',
+ `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(
+ '',
+ `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(
'',