Skip to content

Commit 29c348f

Browse files
committed
fix: fix keyCode check for Chrome autofill fake key events
close #9441
1 parent 21fca2f commit 29c348f

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/compiler/codegen/events.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
163163
}
164164

165165
function genKeyFilter (keys: Array<string>): string {
166-
return `if(('keyCode' in $event)&&${keys.map(genFilterCode).join('&&')})return null;`
166+
return (
167+
// make sure the key filters only apply to KeyboardEvents
168+
// #9441: can't use 'keyCode' in $event because Chrome autofill fires fake
169+
// key events that do not have keyCode property...
170+
`if(!$event.type.indexOf('key')&&` +
171+
`${keys.map(genFilterCode).join('&&')})return null;`
172+
)
167173
}
168174

169175
function genFilterCode (key: string): string {

test/unit/modules/compiler/codegen.spec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -363,37 +363,37 @@ describe('codegen', () => {
363363
it('generate events with keycode', () => {
364364
assertCodegen(
365365
'<input @input.enter="onInput">',
366-
`with(this){return _c('input',{on:{"input":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput($event)}}})}`
366+
`with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput($event)}}})}`
367367
)
368368
// multiple keycodes (delete)
369369
assertCodegen(
370370
'<input @input.delete="onInput">',
371-
`with(this){return _c('input',{on:{"input":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
371+
`with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
372372
)
373373
// multiple keycodes (esc)
374374
assertCodegen(
375375
'<input @input.esc="onInput">',
376-
`with(this){return _c('input',{on:{"input":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"esc",27,$event.key,["Esc","Escape"]))return null;return onInput($event)}}})}`
376+
`with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"esc",27,$event.key,["Esc","Escape"]))return null;return onInput($event)}}})}`
377377
)
378378
// multiple keycodes (space)
379379
assertCodegen(
380380
'<input @input.space="onInput">',
381-
`with(this){return _c('input',{on:{"input":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"space",32,$event.key,[" ","Spacebar"]))return null;return onInput($event)}}})}`
381+
`with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"space",32,$event.key,[" ","Spacebar"]))return null;return onInput($event)}}})}`
382382
)
383383
// multiple keycodes (chained)
384384
assertCodegen(
385385
'<input @keydown.enter.delete="onInput">',
386-
`with(this){return _c('input',{on:{"keydown":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
386+
`with(this){return _c('input',{on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
387387
)
388388
// number keycode
389389
assertCodegen(
390390
'<input @input.13="onInput">',
391-
`with(this){return _c('input',{on:{"input":function($event){if(('keyCode' in $event)&&$event.keyCode!==13)return null;return onInput($event)}}})}`
391+
`with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&$event.keyCode!==13)return null;return onInput($event)}}})}`
392392
)
393393
// custom keycode
394394
assertCodegen(
395395
'<input @input.custom="onInput">',
396-
`with(this){return _c('input',{on:{"input":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput($event)}}})}`
396+
`with(this){return _c('input',{on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput($event)}}})}`
397397
)
398398
})
399399

@@ -416,12 +416,12 @@ describe('codegen', () => {
416416
it('generate events with generic modifiers and keycode correct order', () => {
417417
assertCodegen(
418418
'<input @keydown.enter.prevent="onInput">',
419-
`with(this){return _c('input',{on:{"keydown":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput($event)}}})}`
419+
`with(this){return _c('input',{on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput($event)}}})}`
420420
)
421421

422422
assertCodegen(
423423
'<input @keydown.enter.stop="onInput">',
424-
`with(this){return _c('input',{on:{"keydown":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput($event)}}})}`
424+
`with(this){return _c('input',{on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput($event)}}})}`
425425
)
426426
})
427427

@@ -528,7 +528,7 @@ describe('codegen', () => {
528528
// with modifiers
529529
assertCodegen(
530530
`<input @keyup.enter="e=>current++">`,
531-
`with(this){return _c('input',{on:{"keyup":function($event){if(('keyCode' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++)($event)}}})}`
531+
`with(this){return _c('input',{on:{"keyup":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++)($event)}}})}`
532532
)
533533
})
534534

0 commit comments

Comments
 (0)