Skip to content

Commit 8e83197

Browse files
authored
fix(compiler-vapor): only apply v-on key modifiers to keyboard events (#14136)
1 parent 0de4641 commit 8e83197

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

packages/compiler-dom/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export {
7474
DOMErrorCodes,
7575
DOMErrorMessages,
7676
} from './errors'
77-
export { resolveModifiers } from './transforms/vOn'
77+
export { resolveModifiers, isKeyboardEvent } from './transforms/vOn'
7878
export { isValidHTMLNesting } from './htmlNesting'
7979
export { postTransformTransition } from './transforms/Transition'
8080
export * from '@vue/compiler-core'

packages/compiler-dom/src/transforms/vOn.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ const isNonKeyModifier = /*@__PURE__*/ makeMap(
2828
)
2929
// left & right could be mouse or key modifiers based on event type
3030
const maybeKeyModifier = /*@__PURE__*/ makeMap('left,right')
31-
const isKeyboardEvent = /*@__PURE__*/ makeMap(`onkeyup,onkeydown,onkeypress`)
31+
export const isKeyboardEvent: (key: string) => boolean = /*@__PURE__*/ makeMap(
32+
`onkeyup,onkeydown,onkeypress`,
33+
)
3234

3335
export const resolveModifiers = (
3436
key: ExpressionNode | string,

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ export function render(_ctx) {
278278
`;
279279
280280
exports[`compiler: element transform > component event with multiple modifiers and event options 1`] = `
281-
"import { resolveComponent as _resolveComponent, withModifiers as _withModifiers, withKeys as _withKeys, createComponentWithFallback as _createComponentWithFallback } from 'vue';
281+
"import { resolveComponent as _resolveComponent, withModifiers as _withModifiers, createComponentWithFallback as _createComponentWithFallback } from 'vue';
282282
283283
export function render(_ctx) {
284284
const _component_Foo = _resolveComponent("Foo")
285-
const n0 = _createComponentWithFallback(_component_Foo, { onFooCaptureOnce: () => _withKeys(_withModifiers(_ctx.bar, ["stop","prevent"]), ["enter"]) }, null, true)
285+
const n0 = _createComponentWithFallback(_component_Foo, { onFooCaptureOnce: () => _withModifiers(_ctx.bar, ["stop","prevent"]) }, null, true)
286286
return n0
287287
}"
288288
`;
@@ -434,14 +434,14 @@ export function render(_ctx) {
434434
`;
435435
436436
exports[`compiler: element transform > props merging: event handlers 1`] = `
437-
"import { createInvoker as _createInvoker, withKeys as _withKeys, delegate as _delegate, delegateEvents as _delegateEvents, template as _template } from 'vue';
437+
"import { createInvoker as _createInvoker, delegate as _delegate, delegateEvents as _delegateEvents, template as _template } from 'vue';
438438
const t0 = _template("<div></div>", true)
439439
_delegateEvents("click")
440440
441441
export function render(_ctx) {
442442
const n0 = t0()
443-
_delegate(n0, "click", _createInvoker(_withKeys(e => _ctx.a(e), ["foo"])))
444-
_delegate(n0, "click", _createInvoker(_withKeys(e => _ctx.b(e), ["bar"])))
443+
_delegate(n0, "click", _createInvoker(e => _ctx.a(e)))
444+
_delegate(n0, "click", _createInvoker(e => _ctx.b(e)))
445445
return n0
446446
}"
447447
`;

packages/compiler-vapor/__tests__/transforms/__snapshots__/vOn.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export function render(_ctx, $props, $emit, $attrs, $slots) {
122122
n7.$evtcontextmenu = _createInvoker(_withModifiers(_ctx.handleEvent, ["right"]))
123123
n8.$evtclick = _createInvoker(_withModifiers(_ctx.handleEvent, ["left"]))
124124
n9.$evtmouseup = _createInvoker(_withModifiers(_ctx.handleEvent, ["middle"]))
125-
n10.$evtcontextmenu = _createInvoker(_withKeys(_withModifiers(_ctx.handleEvent, ["right"]), ["enter"]))
125+
n10.$evtcontextmenu = _createInvoker(_withModifiers(_ctx.handleEvent, ["right"]))
126126
n11.$evtkeyup = _createInvoker(_withKeys(_ctx.handleEvent, ["enter"]))
127127
n12.$evtkeyup = _createInvoker(_withKeys(_ctx.handleEvent, ["tab"]))
128128
n13.$evtkeyup = _createInvoker(_withKeys(_ctx.handleEvent, ["delete"]))

packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ describe('compiler: element transform', () => {
963963
key: { content: 'foo' },
964964
handler: true,
965965
handlerModifiers: {
966-
keys: ['enter'],
966+
keys: [],
967967
nonKeys: ['stop', 'prevent'],
968968
options: ['capture', 'once'],
969969
},

packages/compiler-vapor/src/transforms/vOn.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {
22
ElementTypes,
33
ErrorCodes,
44
createCompilerError,
5+
isKeyboardEvent,
6+
isStaticExp,
57
} from '@vue/compiler-dom'
68
import type { DirectiveTransform } from '../transform'
79
import { IRNodeTypes, type KeyOverride, type SetEventIRNode } from '../ir'
@@ -59,6 +61,16 @@ export const transformVOn: DirectiveTransform = (dir, node, context) => {
5961
}
6062
}
6163

64+
// don't gen keys guard for non-keyboard events
65+
// if event name is dynamic, always wrap with keys guard
66+
if (
67+
keyModifiers.length &&
68+
isStaticExp(arg) &&
69+
!isKeyboardEvent(`on${arg.content.toLowerCase()}`)
70+
) {
71+
keyModifiers.length = 0
72+
}
73+
6274
if (isComponent || isSlotOutlet) {
6375
const handler = exp || EMPTY_EXPRESSION
6476
return {

0 commit comments

Comments
 (0)