Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked fromvuejs/vue

Commitdaed1e7

Browse files
committed
fix: normlaize @click.right and @click.middle
fixvuejs#7020
1 parent7cf188e commitdaed1e7

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

‎src/compiler/codegen/events.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,7 @@ export function genHandlers (
4141
):string{
4242
letres=isNative ? 'nativeOn:{' : 'on:{'
4343
for(constnameinevents){
44-
consthandler=events[name]
45-
// #5330: warn click.right, since right clicks do not actually fire click events.
46-
if(process.env.NODE_ENV!=='production'&&
47-
name==='click'&&
48-
handler&&handler.modifiers&&handler.modifiers.right
49-
){
50-
warn(
51-
`Use "contextmenu" instead of "click.right" since right clicks `+
52-
`do not actually fire "click" events.`
53-
)
54-
}
55-
res+= `"${name}":${genHandler(name,handler)},`
44+
res+= `"${name}":${genHandler(name,events[name])},`
5645
}
5746
return res.slice(0, -1) + '}'
5847
}

‎src/compiler/helpers.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*@flow */
22

3+
import{emptyObject}from'shared/util'
34
import{parseFilters}from'./parser/filter-parser'
45

56
exportfunctionbaseWarn(msg:string){
@@ -42,39 +43,59 @@ export function addHandler (
4243
important?:boolean,
4344
warn?:Function
4445
){
46+
modifiers=modifiers||emptyObject
4547
// warn prevent and passive modifier
4648
/* istanbul ignore if */
4749
if(
4850
process.env.NODE_ENV!=='production'&&warn&&
49-
modifiers&&modifiers.prevent&&modifiers.passive
51+
modifiers.prevent&&modifiers.passive
5052
){
5153
warn(
5254
'passive and prevent can\'t be used together. '+
5355
'Passive handler can\'t prevent default event.'
5456
)
5557
}
58+
5659
// check capture modifier
57-
if(modifiers&&modifiers.capture){
60+
if(modifiers.capture){
5861
deletemodifiers.capture
5962
name='!'+name// mark the event as captured
6063
}
61-
if(modifiers&&modifiers.once){
64+
if(modifiers.once){
6265
deletemodifiers.once
6366
name='~'+name// mark the event as once
6467
}
6568
/* istanbul ignore if */
66-
if(modifiers&&modifiers.passive){
69+
if(modifiers.passive){
6770
deletemodifiers.passive
6871
name='&'+name// mark the event as passive
6972
}
73+
74+
// normalize click.right and click.middle since they don't actually fire
75+
// this is technically browser-specific, but at least for now browsers are
76+
// the only target envs that have right/middle clicks.
77+
if(name==='click'){
78+
if(modifiers.right){
79+
name= 'contextmenu'
80+
deletemodifiers.right
81+
}elseif(modifiers.middle){
82+
name='mouseup'
83+
}
84+
}
85+
7086
letevents
71-
if(modifiers&&modifiers.native){
87+
if(modifiers.native){
7288
deletemodifiers.native
7389
events=el.nativeEvents||(el.nativeEvents={})
7490
}else{
7591
events=el.events||(el.events={})
7692
}
77-
constnewHandler={ value, modifiers}
93+
94+
constnewHandler:any={ value}
95+
if(modifiers!==emptyObject){
96+
newHandler.modifiers=modifiers
97+
}
98+
7899
consthandlers=events[name]
79100
/* istanbul ignore if */
80101
if(Array.isArray(handlers)){

‎src/core/util/lang.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*@flow */
22

3-
exportconstemptyObject=Object.freeze({})
4-
53
/**
64
* Check if a string starts with $ or _
75
*/

‎src/shared/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*@flow */
22

3+
exportconstemptyObject=Object.freeze({})
4+
35
// these helpers produces better vm code in JS engines due to their
46
// explicitness and function inlining
57
exportfunctionisUndef(v:any):boolean%checks{

‎test/unit/features/directives/on.spec.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,27 @@ describe('Directive v-on', () => {
693693
expect(prevented).toBe(true)
694694
})
695695

696-
it('should warn click.right',()=>{
697-
newVue({
696+
it('should transform click.right to contextmenu',()=>{
697+
constspy=jasmine.createSpy('click.right')
698+
constvm=newVue({
698699
template:`<div @click.right="foo"></div>`,
699-
methods:{foo(){}}
700+
methods:{foo:spy}
700701
}).$mount()
701702

702-
expect(`Use "contextmenu" instead`).toHaveBeenWarned()
703+
triggerEvent(vm.$el,'contextmenu')
704+
expect(spy).toHaveBeenCalled()
705+
})
706+
707+
it('should transform click.middle to mouseup',()=>{
708+
constspy=jasmine.createSpy('click.middle')
709+
constvm=newVue({
710+
template:`<div @click.middle="foo"></div>`,
711+
methods:{foo:spy}
712+
}).$mount()
713+
triggerEvent(vm.$el,'mouseup',e=>{e.button=0})
714+
expect(spy).not.toHaveBeenCalled()
715+
triggerEvent(vm.$el,'mouseup',e=>{e.button=1})
716+
expect(spy).toHaveBeenCalled()
703717
})
704718

705719
it('object syntax (no argument)',()=>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp