Skip to content

Commit b45b974

Browse files
committed
limit mouse event modifiers to mouse events
1 parent bb6089c commit b45b974

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/compiler/codegen/events.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ const keyCodes = {
1919
const modifierCode = {
2020
stop: '$event.stopPropagation();',
2121
prevent: '$event.preventDefault();',
22-
self: 'if($event.target !== $event.currentTarget)return;',
22+
self: 'if($event.target !== $event.currentTarget)return;'
23+
}
24+
25+
const isMouseEventRE = /^mouse|^pointer|^(click|dblclick|contextmenu|wheel)$/
26+
const mouseEventModifierCode = {
2327
ctrl: 'if(!$event.ctrlKey)return;',
2428
shift: 'if(!$event.shiftKey)return;',
2529
alt: 'if(!$event.altKey)return;',
@@ -29,28 +33,32 @@ const modifierCode = {
2933
export function genHandlers (events: ASTElementHandlers, native?: boolean): string {
3034
let res = native ? 'nativeOn:{' : 'on:{'
3135
for (const name in events) {
32-
res += `"${name}":${genHandler(events[name])},`
36+
res += `"${name}":${genHandler(name, events[name])},`
3337
}
3438
return res.slice(0, -1) + '}'
3539
}
3640

3741
function genHandler (
42+
name: string,
3843
handler: ASTElementHandler | Array<ASTElementHandler>
3944
): string {
4045
if (!handler) {
4146
return 'function(){}'
4247
} else if (Array.isArray(handler)) {
43-
return `[${handler.map(genHandler).join(',')}]`
48+
return `[${handler.map(handler => genHandler(name, handler)).join(',')}]`
4449
} else if (!handler.modifiers) {
4550
return fnExpRE.test(handler.value) || simplePathRE.test(handler.value)
4651
? handler.value
4752
: `function($event){${handler.value}}`
4853
} else {
4954
let code = ''
5055
const keys = []
56+
const isMouseEvnet = isMouseEventRE.test(name)
5157
for (const key in handler.modifiers) {
5258
if (modifierCode[key]) {
5359
code += modifierCode[key]
60+
} else if (isMouseEvnet && mouseEventModifierCode[key]) {
61+
code += mouseEventModifierCode[key]
5462
} else {
5563
keys.push(key)
5664
}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe('codegen', () => {
248248
)
249249
})
250250

251-
it('generate events with modifiers', () => {
251+
it('generate events with generic modifiers', () => {
252252
assertCodegen(
253253
'<input @input.stop="onInput">',
254254
`with(this){return _h('input',{on:{"input":function($event){$event.stopPropagation();onInput($event)}}})}`
@@ -261,21 +261,24 @@ describe('codegen', () => {
261261
'<input @input.self="onInput">',
262262
`with(this){return _h('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return;onInput($event)}}})}`
263263
)
264+
})
265+
266+
it('generate events with mouse event modifiers', () => {
264267
assertCodegen(
265-
'<input @input.ctrl="onInput">',
266-
`with(this){return _h('input',{on:{"input":function($event){if(!$event.ctrlKey)return;onInput($event)}}})}`
268+
'<input @click.ctrl="onClick">',
269+
`with(this){return _h('input',{on:{"click":function($event){if(!$event.ctrlKey)return;onClick($event)}}})}`
267270
)
268271
assertCodegen(
269-
'<input @input.shift="onInput">',
270-
`with(this){return _h('input',{on:{"input":function($event){if(!$event.shiftKey)return;onInput($event)}}})}`
272+
'<input @click.shift="onClick">',
273+
`with(this){return _h('input',{on:{"click":function($event){if(!$event.shiftKey)return;onClick($event)}}})}`
271274
)
272275
assertCodegen(
273-
'<input @input.alt="onInput">',
274-
`with(this){return _h('input',{on:{"input":function($event){if(!$event.altKey)return;onInput($event)}}})}`
276+
'<input @click.alt="onClick">',
277+
`with(this){return _h('input',{on:{"click":function($event){if(!$event.altKey)return;onClick($event)}}})}`
275278
)
276279
assertCodegen(
277-
'<input @input.meta="onInput">',
278-
`with(this){return _h('input',{on:{"input":function($event){if(!$event.metaKey)return;onInput($event)}}})}`
280+
'<input @click.meta="onClick">',
281+
`with(this){return _h('input',{on:{"click":function($event){if(!$event.metaKey)return;onClick($event)}}})}`
279282
)
280283
})
281284

0 commit comments

Comments
 (0)