Skip to content

Commit

Permalink
fix(compiler-dom): fix v-on .left .right modifier handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 13, 2020
1 parent d1586f4 commit 6b63ba2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
36 changes: 36 additions & 0 deletions packages/compiler-dom/__tests__/transforms/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,42 @@ describe('compiler-dom: transform v-on', () => {
})
})

it('should wrap keys guard for static key event w/ left/right modifiers', () => {
const {
props: [prop]
} = parseWithVOn(`<div @keyup.left="test"/>`, {
prefixIdentifiers: true
})
expect(prop).toMatchObject({
type: NodeTypes.JS_PROPERTY,
value: {
callee: V_ON_WITH_KEYS,
arguments: [{ content: '_ctx.test' }, '["left"]']
}
})
})

it('should wrap both for dynamic key event w/ left/right modifiers', () => {
const {
props: [prop]
} = parseWithVOn(`<div @[e].left="test"/>`, {
prefixIdentifiers: true
})
expect(prop).toMatchObject({
type: NodeTypes.JS_PROPERTY,
value: {
callee: V_ON_WITH_KEYS,
arguments: [
{
callee: V_ON_WITH_MODIFIERS,
arguments: [{ content: `_ctx.test` }, `["left"]`]
},
'["left"]'
]
}
})
})

it('should not wrap normal guard if there is only keys guard', () => {
const {
props: [prop]
Expand Down
31 changes: 24 additions & 7 deletions packages/compiler-dom/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
createSimpleExpression,
NodeTypes,
createCompoundExpression,
ExpressionNode
ExpressionNode,
SimpleExpressionNode
} from '@vue/compiler-core'
import { V_ON_WITH_MODIFIERS, V_ON_WITH_KEYS } from '../runtimeHelpers'
import { makeMap } from '@vue/shared'
Expand All @@ -19,14 +20,17 @@ const isNonKeyModifier = /*#__PURE__*/ makeMap(
// system modifiers + exact
`ctrl,shift,alt,meta,exact,` +
// mouse
`left,middle,right`
`middle`
)
// left & right could be mouse or key modifiers based on event type
const maybeKeyModifier = /*#__PURE__*/ makeMap('left,right')
const isKeyboardEvent = /*#__PURE__*/ makeMap(
`onkeyup,onkeydown,onkeypress`,
true
)

const generateModifiers = (modifiers: string[]) => {
const resolveModifiers = (key: ExpressionNode, modifiers: string[]) => {
const isStaticKey = key.type === NodeTypes.SIMPLE_EXPRESSION && key.isStatic
const keyModifiers = []
const nonKeyModifiers = []
const eventOptionModifiers = []
Expand All @@ -39,10 +43,23 @@ const generateModifiers = (modifiers: string[]) => {
eventOptionModifiers.push(modifier)
} else {
// runtimeModifiers: modifiers that needs runtime guards
if (isNonKeyModifier(modifier)) {
nonKeyModifiers.push(modifier)
if (maybeKeyModifier(modifier)) {
if (isStaticKey) {
if (isKeyboardEvent((key as SimpleExpressionNode).content)) {
keyModifiers.push(modifier)
} else {
nonKeyModifiers.push(modifier)
}
} else {
keyModifiers.push(modifier)
nonKeyModifiers.push(modifier)
}
} else {
keyModifiers.push(modifier)
if (isNonKeyModifier(modifier)) {
nonKeyModifiers.push(modifier)
} else {
keyModifiers.push(modifier)
}
}
}
}
Expand Down Expand Up @@ -82,7 +99,7 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
keyModifiers,
nonKeyModifiers,
eventOptionModifiers
} = generateModifiers(modifiers)
} = resolveModifiers(key, modifiers)

// normalize click.right and click.middle since they don't actually fire
if (nonKeyModifiers.includes('right')) {
Expand Down

0 comments on commit 6b63ba2

Please sign in to comment.