jenkins-bot has submitted this change and it was merged.

Change subject: Fix duplicate search suggestions appearing when focusing to fast
......................................................................


Fix duplicate search suggestions appearing when focusing to fast

The bug is quite obvious the moment you realize what's going on: The whole
code snippet was in a "focus" event handler. But when the user focused the
search field earlier, before this event handler was assigned, this event
was never fired.

My approach might have one issue: Let's assume my event handler is already
active, but the native search was not initialized yet. When the user
triggers a change event in this exact moment, the "one" event does nothing
and stops being executed. This is extremely unlikely, it may even be
impossible.

You may ask why I'm doing the null check then, and not assuming the native
search is initialized. Yes, but I want an obvious regression (the duplicate
suggestion will re-appear) instead of a script execution error.

Bug: T119492
Change-Id: I276a9c25ad7af6bd0a069d15a1ef0d7d00a8a80c
---
M repo/resources/wikibase.ui.entitysearch.js
1 file changed, 9 insertions(+), 17 deletions(-)

Approvals:
  Hoo man: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/resources/wikibase.ui.entitysearch.js 
b/repo/resources/wikibase.ui.entitysearch.js
index f4db77a..0e670a4 100644
--- a/repo/resources/wikibase.ui.entitysearch.js
+++ b/repo/resources/wikibase.ui.entitysearch.js
@@ -50,9 +50,13 @@
                 * @param {HTMLElement} input Search box node
                 */
                function removeSuggestionContext( input ) {
-                       // Native fetch() updates/re-sets the data attribute 
with the suggestion context.
-                       $.data( input, 'suggestionsContext' ).config.fetch = 
function() {};
-                       $.removeData( input, 'suggestionsContext' );
+                       var context = $.data( input, 'suggestionsContext' );
+
+                       if ( context ) {
+                               // Native fetch() updates/re-sets the data 
attribute with the suggestion context.
+                               context.config.fetch = function() {};
+                               $.removeData( input, 'suggestionsContext' );
+                       }
                }
 
                var suggestionsPlaceholder = new $.ui.ooMenu.CustomItem(
@@ -81,20 +85,8 @@
                $input.before( $hiddenInput );
 
                $input
-               .one( 'focus', function( event ) {
-                       if ( $.data( this, 'suggestionsContext' ) ) {
-                               removeSuggestionContext( this );
-                       } else {
-                               // Suggestion context might not be initialized 
when focusing the search box while
-                               // the page is still rendered.
-                               var $input = $( this );
-                               $input.on( 'keypress.entitysearch', function( 
event ) {
-                                       if ( $.data( this, 'suggestionsContext' 
) ) {
-                                               removeSuggestionContext( this );
-                                               $input.off( '.entitysearch' );
-                                       }
-                               } );
-                       }
+               .one( 'eachchange.entitysearch', function() {
+                       removeSuggestionContext( this );
                } )
                .entitysearch( {
                        url: mw.config.get( 'wgServer' ) + mw.config.get( 
'wgScriptPath' ) + '/api.php',

-- 
To view, visit https://gerrit.wikimedia.org/r/312279
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I276a9c25ad7af6bd0a069d15a1ef0d7d00a8a80c
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to