diff --git a/src/compiler/codegen/events.js b/src/compiler/codegen/events.js index 4fb28d86..cb0ca49a 100644 --- a/src/compiler/codegen/events.js +++ b/src/compiler/codegen/events.js @@ -41,25 +41,40 @@ function genHandler ( ? handler.value : `function($event){${handler.value}}` } else { - let code = 'function($event){' + let code = '' + const keys = [] for (const key in handler.modifiers) { - code += modifierCode[key] || genKeyFilter(key) + if (modifierCode[key]) { + code += modifierCode[key] + } else { + keys.push(key) + } + } + if (keys.length) { + code = genKeyFilter(keys) + code } const handlerCode = simplePathRE.test(handler.value) ? handler.value + '($event)' : handler.value - return code + handlerCode + '}' + return 'function($event){' + code + handlerCode + '}' } } -function genKeyFilter (key: string): string { - const code = - parseInt(key, 10) || // number keyCode - keyCodes[key] || // built-in alias - `_k(${JSON.stringify(key)})` // custom alias +function genKeyFilter (keys: Array): string { + const code = keys.length === 1 + ? normalizeKeyCode(keys[0]) + : Array.prototype.concat.apply([], keys.map(normalizeKeyCode)) if (Array.isArray(code)) { return `if(${code.map(c => `$event.keyCode!==${c}`).join('&&')})return;` } else { return `if($event.keyCode!==${code})return;` } } + +function normalizeKeyCode (key) { + return ( + parseInt(key, 10) || // number keyCode + keyCodes[key] || // built-in alias + `_k(${JSON.stringify(key)})` // custom alias + ) +} diff --git a/test/unit/modules/compiler/codegen.spec.js b/test/unit/modules/compiler/codegen.spec.js index 399cadd5..f0b32ebb 100644 --- a/test/unit/modules/compiler/codegen.spec.js +++ b/test/unit/modules/compiler/codegen.spec.js @@ -191,6 +191,11 @@ describe('codegen', () => { '', `with(this){return _h('input',{on:{"input":function($event){if($event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}})}` ) + // multiple keycodes (chained) + assertCodegen( + '', + `with(this){return _h('input',{on:{"keydown":function($event){if($event.keyCode!==13&&$event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}})}` + ) // number keycode assertCodegen( '',