2019-10-14 12:33:23 +08:00
|
|
|
import {
|
2019-12-23 08:44:21 +08:00
|
|
|
baseParse as parse,
|
2019-10-14 12:33:23 +08:00
|
|
|
CompilerOptions,
|
|
|
|
ElementNode,
|
2020-10-20 05:15:53 +08:00
|
|
|
TO_HANDLER_KEY,
|
2020-07-14 23:48:05 +08:00
|
|
|
helperNameMap,
|
2020-10-20 05:15:53 +08:00
|
|
|
NodeTypes,
|
|
|
|
ObjectExpression,
|
|
|
|
transform,
|
|
|
|
VNodeCall
|
2019-10-14 12:33:23 +08:00
|
|
|
} from '@vue/compiler-core'
|
|
|
|
import { transformOn } from '../../src/transforms/vOn'
|
2020-10-20 05:15:53 +08:00
|
|
|
import { V_ON_WITH_KEYS, V_ON_WITH_MODIFIERS } from '../../src/runtimeHelpers'
|
2019-10-14 12:33:23 +08:00
|
|
|
import { transformElement } from '../../../compiler-core/src/transforms/transformElement'
|
|
|
|
import { transformExpression } from '../../../compiler-core/src/transforms/transformExpression'
|
2020-07-14 23:48:05 +08:00
|
|
|
import { genFlagText } from '../../../compiler-core/__tests__/testUtils'
|
2020-02-14 07:28:40 +08:00
|
|
|
import { PatchFlags } from '@vue/shared'
|
2019-10-14 12:33:23 +08:00
|
|
|
|
2019-10-19 09:51:34 +08:00
|
|
|
function parseWithVOn(template: string, options: CompilerOptions = {}) {
|
2019-10-14 12:33:23 +08:00
|
|
|
const ast = parse(template)
|
|
|
|
transform(ast, {
|
|
|
|
nodeTransforms: [transformExpression, transformElement],
|
|
|
|
directiveTransforms: {
|
|
|
|
on: transformOn
|
|
|
|
},
|
|
|
|
...options
|
|
|
|
})
|
2019-10-19 09:51:34 +08:00
|
|
|
return {
|
|
|
|
root: ast,
|
2021-07-20 06:24:18 +08:00
|
|
|
props: (
|
|
|
|
((ast.children[0] as ElementNode).codegenNode as VNodeCall)
|
|
|
|
.props as ObjectExpression
|
|
|
|
).properties
|
2019-10-19 09:51:34 +08:00
|
|
|
}
|
2019-10-14 12:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('compiler-dom: transform v-on', () => {
|
2019-10-18 03:01:51 +08:00
|
|
|
it('should support multiple modifiers w/ prefixIdentifiers: true', () => {
|
2019-10-19 09:51:34 +08:00
|
|
|
const {
|
|
|
|
props: [prop]
|
|
|
|
} = parseWithVOn(`<div @click.stop.prevent="test"/>`, {
|
2019-10-14 12:33:23 +08:00
|
|
|
prefixIdentifiers: true
|
|
|
|
})
|
|
|
|
expect(prop).toMatchObject({
|
|
|
|
type: NodeTypes.JS_PROPERTY,
|
2019-10-15 11:06:51 +08:00
|
|
|
value: {
|
2019-10-19 04:36:42 +08:00
|
|
|
callee: V_ON_WITH_MODIFIERS,
|
2019-10-15 11:06:51 +08:00
|
|
|
arguments: [{ content: '_ctx.test' }, '["stop","prevent"]']
|
|
|
|
}
|
2019-11-16 04:37:02 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support multiple events and modifiers options w/ prefixIdentifiers: true', () => {
|
|
|
|
const { props } = parseWithVOn(
|
|
|
|
`<div @click.stop="test" @keyup.enter="test" />`,
|
|
|
|
{
|
|
|
|
prefixIdentifiers: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const [clickProp, keyUpProp] = props
|
|
|
|
|
|
|
|
expect(props).toHaveLength(2)
|
|
|
|
expect(clickProp).toMatchObject({
|
|
|
|
type: NodeTypes.JS_PROPERTY,
|
|
|
|
value: {
|
|
|
|
callee: V_ON_WITH_MODIFIERS,
|
|
|
|
arguments: [{ content: '_ctx.test' }, '["stop"]']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(keyUpProp).toMatchObject({
|
|
|
|
type: NodeTypes.JS_PROPERTY,
|
|
|
|
value: {
|
|
|
|
callee: V_ON_WITH_KEYS,
|
|
|
|
arguments: [{ content: '_ctx.test' }, '["enter"]']
|
|
|
|
}
|
2019-10-14 12:33:23 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support multiple modifiers and event options w/ prefixIdentifiers: true', () => {
|
2019-10-19 09:51:34 +08:00
|
|
|
const {
|
|
|
|
props: [prop]
|
2020-07-15 01:20:59 +08:00
|
|
|
} = parseWithVOn(`<div @click.stop.capture.once="test"/>`, {
|
2019-10-19 09:51:34 +08:00
|
|
|
prefixIdentifiers: true
|
|
|
|
})
|
2019-10-14 12:33:23 +08:00
|
|
|
expect(prop).toMatchObject({
|
|
|
|
type: NodeTypes.JS_PROPERTY,
|
2020-07-14 23:48:05 +08:00
|
|
|
key: {
|
2020-07-15 01:20:59 +08:00
|
|
|
content: `onClickCaptureOnce`
|
2020-07-14 23:48:05 +08:00
|
|
|
},
|
|
|
|
value: {
|
|
|
|
callee: V_ON_WITH_MODIFIERS,
|
|
|
|
arguments: [{ content: '_ctx.test' }, '["stop"]']
|
|
|
|
}
|
2019-10-14 12:33:23 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should wrap keys guard for keyboard events or dynamic events', () => {
|
2019-10-19 09:51:34 +08:00
|
|
|
const {
|
|
|
|
props: [prop]
|
2020-07-14 23:48:05 +08:00
|
|
|
} = parseWithVOn(`<div @keydown.stop.capture.ctrl.a="test"/>`, {
|
2019-10-19 09:51:34 +08:00
|
|
|
prefixIdentifiers: true
|
|
|
|
})
|
2019-10-14 12:33:23 +08:00
|
|
|
expect(prop).toMatchObject({
|
|
|
|
type: NodeTypes.JS_PROPERTY,
|
2020-07-14 23:48:05 +08:00
|
|
|
key: {
|
2020-07-15 01:20:59 +08:00
|
|
|
content: `onKeydownCapture`
|
2020-07-14 23:48:05 +08:00
|
|
|
},
|
|
|
|
value: {
|
|
|
|
callee: V_ON_WITH_KEYS,
|
|
|
|
arguments: [
|
|
|
|
{
|
|
|
|
callee: V_ON_WITH_MODIFIERS,
|
|
|
|
arguments: [{ content: '_ctx.test' }, '["stop","ctrl"]']
|
|
|
|
},
|
|
|
|
'["a"]'
|
|
|
|
]
|
|
|
|
}
|
2019-10-14 12:33:23 +08:00
|
|
|
})
|
|
|
|
})
|
2019-10-16 05:08:44 +08:00
|
|
|
|
|
|
|
it('should not wrap keys guard if no key modifier is present', () => {
|
2019-10-19 09:51:34 +08:00
|
|
|
const {
|
|
|
|
props: [prop]
|
|
|
|
} = parseWithVOn(`<div @keyup.exact="test"/>`, {
|
2019-10-16 05:08:44 +08:00
|
|
|
prefixIdentifiers: true
|
|
|
|
})
|
|
|
|
expect(prop).toMatchObject({
|
|
|
|
type: NodeTypes.JS_PROPERTY,
|
|
|
|
value: {
|
2019-10-19 04:36:42 +08:00
|
|
|
callee: V_ON_WITH_MODIFIERS,
|
2019-10-16 05:08:44 +08:00
|
|
|
arguments: [{ content: '_ctx.test' }, '["exact"]']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-10-19 04:20:45 +08:00
|
|
|
|
2020-07-14 02:00:08 +08:00
|
|
|
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"]'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-19 04:20:45 +08:00
|
|
|
it('should not wrap normal guard if there is only keys guard', () => {
|
2019-10-19 09:51:34 +08:00
|
|
|
const {
|
|
|
|
props: [prop]
|
|
|
|
} = parseWithVOn(`<div @keyup.enter="test"/>`, {
|
2019-10-19 04:20:45 +08:00
|
|
|
prefixIdentifiers: true
|
|
|
|
})
|
|
|
|
expect(prop).toMatchObject({
|
|
|
|
type: NodeTypes.JS_PROPERTY,
|
|
|
|
value: {
|
2019-10-19 04:36:42 +08:00
|
|
|
callee: V_ON_WITH_KEYS,
|
2019-10-19 04:20:45 +08:00
|
|
|
arguments: [{ content: '_ctx.test' }, '["enter"]']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-02-19 03:32:06 +08:00
|
|
|
|
|
|
|
test('should transform click.right', () => {
|
|
|
|
const {
|
|
|
|
props: [prop]
|
|
|
|
} = parseWithVOn(`<div @click.right="test"/>`)
|
|
|
|
expect(prop.key).toMatchObject({
|
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
|
|
|
content: `onContextmenu`
|
|
|
|
})
|
|
|
|
|
|
|
|
// dynamic
|
|
|
|
const {
|
|
|
|
props: [prop2]
|
|
|
|
} = parseWithVOn(`<div @[event].right="test"/>`)
|
2020-10-20 05:15:53 +08:00
|
|
|
// (_toHandlerKey(event)).toLowerCase() === "onclick" ? "onContextmenu" : (_toHandlerKey(event))
|
2020-02-19 03:32:06 +08:00
|
|
|
expect(prop2.key).toMatchObject({
|
|
|
|
type: NodeTypes.COMPOUND_EXPRESSION,
|
|
|
|
children: [
|
|
|
|
`(`,
|
2020-07-14 23:48:05 +08:00
|
|
|
{
|
|
|
|
children: [
|
2020-10-20 05:15:53 +08:00
|
|
|
`_${helperNameMap[TO_HANDLER_KEY]}(`,
|
2020-07-14 23:48:05 +08:00
|
|
|
{ content: 'event' },
|
|
|
|
`)`
|
|
|
|
]
|
|
|
|
},
|
|
|
|
`) === "onClick" ? "onContextmenu" : (`,
|
|
|
|
{
|
|
|
|
children: [
|
2020-10-20 05:15:53 +08:00
|
|
|
`_${helperNameMap[TO_HANDLER_KEY]}(`,
|
2020-07-14 23:48:05 +08:00
|
|
|
{ content: 'event' },
|
|
|
|
`)`
|
|
|
|
]
|
|
|
|
},
|
2020-02-19 03:32:06 +08:00
|
|
|
`)`
|
|
|
|
]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should transform click.middle', () => {
|
|
|
|
const {
|
|
|
|
props: [prop]
|
|
|
|
} = parseWithVOn(`<div @click.middle="test"/>`)
|
|
|
|
expect(prop.key).toMatchObject({
|
|
|
|
type: NodeTypes.SIMPLE_EXPRESSION,
|
|
|
|
content: `onMouseup`
|
|
|
|
})
|
|
|
|
|
|
|
|
// dynamic
|
|
|
|
const {
|
|
|
|
props: [prop2]
|
|
|
|
} = parseWithVOn(`<div @[event].middle="test"/>`)
|
2020-10-20 05:15:53 +08:00
|
|
|
// (_eventNaming(event)).toLowerCase() === "onclick" ? "onMouseup" : (_eventNaming(event))
|
2020-02-19 03:32:06 +08:00
|
|
|
expect(prop2.key).toMatchObject({
|
|
|
|
type: NodeTypes.COMPOUND_EXPRESSION,
|
|
|
|
children: [
|
|
|
|
`(`,
|
2020-07-14 23:48:05 +08:00
|
|
|
{
|
|
|
|
children: [
|
2020-10-20 05:15:53 +08:00
|
|
|
`_${helperNameMap[TO_HANDLER_KEY]}(`,
|
2020-07-14 23:48:05 +08:00
|
|
|
{ content: 'event' },
|
|
|
|
`)`
|
|
|
|
]
|
|
|
|
},
|
|
|
|
`) === "onClick" ? "onMouseup" : (`,
|
|
|
|
{
|
|
|
|
children: [
|
2020-10-20 05:15:53 +08:00
|
|
|
`_${helperNameMap[TO_HANDLER_KEY]}(`,
|
2020-07-14 23:48:05 +08:00
|
|
|
{ content: 'event' },
|
|
|
|
`)`
|
|
|
|
]
|
|
|
|
},
|
2020-02-19 03:32:06 +08:00
|
|
|
`)`
|
|
|
|
]
|
|
|
|
})
|
|
|
|
})
|
2019-10-19 09:51:34 +08:00
|
|
|
|
|
|
|
test('cache handler w/ modifiers', () => {
|
|
|
|
const {
|
|
|
|
root,
|
|
|
|
props: [prop]
|
|
|
|
} = parseWithVOn(`<div @keyup.enter.capture="foo" />`, {
|
|
|
|
prefixIdentifiers: true,
|
|
|
|
cacheHandlers: true
|
|
|
|
})
|
|
|
|
expect(root.cached).toBe(1)
|
2020-02-14 07:28:40 +08:00
|
|
|
// should not treat cached handler as dynamicProp, so it should have no
|
|
|
|
// dynamicProps flags and only the hydration flag
|
|
|
|
expect((root as any).children[0].codegenNode.patchFlag).toBe(
|
|
|
|
genFlagText(PatchFlags.HYDRATE_EVENTS)
|
|
|
|
)
|
2020-07-14 23:48:05 +08:00
|
|
|
expect(prop).toMatchObject({
|
|
|
|
key: {
|
2020-07-15 01:20:59 +08:00
|
|
|
content: `onKeyupCapture`
|
2020-07-14 23:48:05 +08:00
|
|
|
},
|
2019-10-19 09:51:34 +08:00
|
|
|
value: {
|
2020-07-14 23:48:05 +08:00
|
|
|
type: NodeTypes.JS_CACHE_EXPRESSION,
|
2021-07-10 09:41:44 +08:00
|
|
|
index: 0,
|
2020-07-14 23:48:05 +08:00
|
|
|
value: {
|
|
|
|
type: NodeTypes.JS_CALL_EXPRESSION,
|
|
|
|
callee: V_ON_WITH_KEYS
|
|
|
|
}
|
2019-10-19 09:51:34 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-10-14 12:33:23 +08:00
|
|
|
})
|