aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2021-05-10 18:59:14 +0200
committerGitHub <noreply@github.com>2021-05-10 18:59:14 +0200
commite539bac79e666bba95bba86d690b4e609dca2286 (patch)
treef9759b616671b1fa56f36af3832cfde61a3099aa
parenta70274632dc19ff4a64d7bb7657a2cc647ff38b9 (diff)
downloadjquery-e539bac79e666bba95bba86d690b4e609dca2286.tar.gz
jquery-e539bac79e666bba95bba86d690b4e609dca2286.zip
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
-rw-r--r--src/event.js8
-rw-r--r--test/unit/event.js16
2 files changed, 20 insertions, 4 deletions
diff --git a/src/event.js b/src/event.js
index 48ee8d174..4b6eb00e4 100644
--- a/src/event.js
+++ b/src/event.js
@@ -746,10 +746,10 @@ jQuery.each( { focus: "focusin", blur: "focusout" }, function( type, delegateTyp
return true;
},
- // Suppress native focus or blur as it's already being fired
- // in leverageNative.
- _default: function() {
- return true;
+ // Suppress native focus or blur if we're currently inside
+ // a leveraged native-event stack
+ _default: function( event ) {
+ return dataPriv.get( event.target, type );
},
delegateType: delegateType
diff --git a/test/unit/event.js b/test/unit/event.js
index edd9ecaea..1f76c54b9 100644
--- a/test/unit/event.js
+++ b/test/unit/event.js
@@ -3325,6 +3325,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
// https://github.com/jquery/jquery/pull/1367/files#diff-a215316abbaabdf71857809e8673ea28R2464
( function() {