test(runtime-dom): add test coverage for v-on runtime guards, fix "exact" guard (#298)

This commit is contained in:
宋铄运
2019-10-16 05:08:44 +08:00
committed by Evan You
parent 3385480ba7
commit db5c343c33
4 changed files with 91 additions and 24 deletions

View File

@@ -90,4 +90,17 @@ describe('compiler-dom: transform v-on', () => {
})
})
})
it('should not wrap keys guard if no key modifier is present', () => {
const [prop] = parseVOnProperties(`<div @keyup.exact="test"/>`, {
prefixIdentifiers: true
})
expect(prop).toMatchObject({
type: NodeTypes.JS_PROPERTY,
value: {
callee: V_ON_MODIFIERS_GUARD,
arguments: [{ content: '_ctx.test' }, '["exact"]']
}
})
})
})

View File

@@ -41,15 +41,17 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
value,
JSON.stringify(runtimeModifiers.filter(m => m in NOT_KEY_MODIFIERS))
])
const keyModifiers = runtimeModifiers.filter(m => !(m in NOT_KEY_MODIFIERS))
if (
keyModifiers.length &&
// if event name is dynamic, always wrap with keys guard
key.type === NodeTypes.COMPOUND_EXPRESSION ||
!key.isStatic ||
key.content.toLowerCase() in KEYBOARD_EVENTS
(key.type === NodeTypes.COMPOUND_EXPRESSION ||
!key.isStatic ||
key.content.toLowerCase() in KEYBOARD_EVENTS)
) {
handler = createCallExpression(context.helper(V_ON_KEYS_GUARD), [
handler,
JSON.stringify(runtimeModifiers.filter(m => !(m in NOT_KEY_MODIFIERS)))
JSON.stringify(keyModifiers)
])
}