mirror of
https://github.com/jquery/jquery.git
synced 2025-01-10 18:24:24 +00:00
Event: Don't break focus triggering after .on(focus).off(focus)
The `_default` function in the special event settings for focus/blur has
always returned `true` since gh-4813 as the event was already being fired
from `leverageNative`. However, that only works if there's an active handler
on that element; this made a quick consecutive call:
```js
elem.on( "focus", function() {} ).off( "focus" );
```
make subsequent `.trigger( "focus" )` calls to not do any triggering.
The solution, already used in a similar `_default` method for the `click` event,
is to check for the `dataPriv` entry on the element for the focus event
(similarly for blur).
Fixes gh-4867
Closes gh-4885
(cherry picked from commit e539bac79e
)
This commit is contained in:
parent
752b8981f8
commit
b3e4a7eb16
@ -778,10 +778,10 @@ jQuery.each( { focus: "focusin", blur: "focusout" }, function( type, delegateTyp
|
|||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Suppress native focus or blur as it's already being fired
|
// Suppress native focus or blur if we're currently inside
|
||||||
// in leverageNative.
|
// a leveraged native-event stack
|
||||||
_default: function() {
|
_default: function( event ) {
|
||||||
return true;
|
return dataPriv.get( event.target, type );
|
||||||
},
|
},
|
||||||
|
|
||||||
delegateType: delegateType
|
delegateType: delegateType
|
||||||
|
@ -3297,6 +3297,22 @@ QUnit.test( "focus change during a focus handler (gh-4382)", function( assert )
|
|||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
QUnit.test( "trigger(focus) works after .on(focus).off(focus) (gh-4867)", function( assert ) {
|
||||||
|
assert.expect( 1 );
|
||||||
|
|
||||||
|
var input = jQuery( "<input />" );
|
||||||
|
|
||||||
|
input.appendTo( "#qunit-fixture" );
|
||||||
|
|
||||||
|
input
|
||||||
|
.on( "focus", function() {} )
|
||||||
|
.off( "focus" );
|
||||||
|
|
||||||
|
input.trigger( "focus" );
|
||||||
|
|
||||||
|
assert.equal( document.activeElement, input[ 0 ], "input has focus" );
|
||||||
|
} );
|
||||||
|
|
||||||
// TODO replace with an adaptation of
|
// TODO replace with an adaptation of
|
||||||
// https://github.com/jquery/jquery/pull/1367/files#diff-a215316abbaabdf71857809e8673ea28R2464
|
// https://github.com/jquery/jquery/pull/1367/files#diff-a215316abbaabdf71857809e8673ea28R2464
|
||||||
( function() {
|
( function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user