Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .bare event modifier, fix #5976 #5977

Merged
merged 5 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compiler/codegen/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ function genHandler (
if (keyCodes[key]) {
keys.push(key)
}
} else if (key === 'exact') {
const modifiers: ASTModifiers = (handler.modifiers: any)
genModifierCode += genGuard(
['ctrl', 'shift', 'alt', 'meta']
.filter(keyModifier => !modifiers[keyModifier])
.map(keyModifier => `$event.${keyModifier}Key`)
.join('||')
)
} else {
keys.push(key)
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/instance/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if (process.env.NODE_ENV !== 'production') {
Proxy.toString().match(/native code/)

if (hasProxy) {
const isBuiltInModifier = makeMap('stop,prevent,self,ctrl,shift,alt,meta')
const isBuiltInModifier = makeMap('stop,prevent,self,ctrl,shift,alt,meta,exact')
config.keyCodes = new Proxy(config.keyCodes, {
set (target, key, value) {
if (isBuiltInModifier(key)) {
Expand Down
94 changes: 92 additions & 2 deletions test/unit/features/directives/on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,96 @@ describe('Directive v-on', () => {
expect(spy).toHaveBeenCalled()
})

// ctrl, shift, alt, meta
it('should support system modifers', () => {
vm = new Vue({
el,
template: `
<div>
<input ref="ctrl" @keyup.ctrl="foo">
<input ref="shift" @keyup.shift="foo">
<input ref="alt" @keyup.alt="foo">
<input ref="meta" @keyup.meta="foo">
</div>
`,
methods: { foo: spy }
})

triggerEvent(vm.$refs.ctrl, 'keyup')
expect(spy.calls.count()).toBe(0)
triggerEvent(vm.$refs.ctrl, 'keyup', e => { e.ctrlKey = true })
expect(spy.calls.count()).toBe(1)

triggerEvent(vm.$refs.shift, 'keyup')
expect(spy.calls.count()).toBe(1)
triggerEvent(vm.$refs.shift, 'keyup', e => { e.shiftKey = true })
expect(spy.calls.count()).toBe(2)

triggerEvent(vm.$refs.alt, 'keyup')
expect(spy.calls.count()).toBe(2)
triggerEvent(vm.$refs.alt, 'keyup', e => { e.altKey = true })
expect(spy.calls.count()).toBe(3)

triggerEvent(vm.$refs.meta, 'keyup')
expect(spy.calls.count()).toBe(3)
triggerEvent(vm.$refs.meta, 'keyup', e => { e.metaKey = true })
expect(spy.calls.count()).toBe(4)
})

it('should support exact modifier', () => {
vm = new Vue({
el,
template: `
<div>
<input ref="ctrl" @keyup.exact="foo">
</div>
`,
methods: { foo: spy }
})

triggerEvent(vm.$refs.ctrl, 'keyup')
expect(spy.calls.count()).toBe(1)

triggerEvent(vm.$refs.ctrl, 'keyup', e => {
e.ctrlKey = true
})
expect(spy.calls.count()).toBe(1)

// should not trigger if has other system modifiers
triggerEvent(vm.$refs.ctrl, 'keyup', e => {
e.ctrlKey = true
e.altKey = true
})
expect(spy.calls.count()).toBe(1)
})

it('should support system modifers with exact', () => {
vm = new Vue({
el,
template: `
<div>
<input ref="ctrl" @keyup.ctrl.exact="foo">
</div>
`,
methods: { foo: spy }
})

triggerEvent(vm.$refs.ctrl, 'keyup')
expect(spy.calls.count()).toBe(0)

triggerEvent(vm.$refs.ctrl, 'keyup', e => {
e.ctrlKey = true
})
expect(spy.calls.count()).toBe(1)

// should not trigger if has other system modifiers
triggerEvent(vm.$refs.ctrl, 'keyup', e => {
e.ctrlKey = true
e.altKey = true
})
expect(spy.calls.count()).toBe(1)
})

it('should support number keyCode', () => {
vm = new Vue({
el,
Expand Down Expand Up @@ -497,8 +587,8 @@ describe('Directive v-on', () => {
}).not.toThrow()
})

// GitHub Issue #5046
it('should support keyboard modifier', () => {
// Github Issue #5046
it('should support keyboard modifier for direction keys', () => {
const spyLeft = jasmine.createSpy()
const spyRight = jasmine.createSpy()
const spyUp = jasmine.createSpy()
Expand Down
8 changes: 8 additions & 0 deletions test/unit/modules/compiler/codegen.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ describe('codegen', () => {
'<input @click.meta="onClick">',
`with(this){return _c('input',{on:{"click":function($event){if(!$event.metaKey)return null;onClick($event)}}})}`
)
assertCodegen(
'<input @click.exact="onClick">',
`with(this){return _c('input',{on:{"click":function($event){if($event.ctrlKey||$event.shiftKey||$event.altKey||$event.metaKey)return null;onClick($event)}}})}`
)
assertCodegen(
'<input @click.ctrl.exact="onClick">',
`with(this){return _c('input',{on:{"click":function($event){if(!$event.ctrlKey)return null;if($event.shiftKey||$event.altKey||$event.metaKey)return null;onClick($event)}}})}`
)
})

it('generate events with multiple modifiers', () => {
Expand Down