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

Change subject: Optimized entityselector auto-completion mechanism
......................................................................


Optimized entityselector auto-completion mechanism

(bug 44884)
If an entity whose label string starts with the string of one of its aliases is 
returned,
the auto-completion mechanism is supposed to auto-complete the alias instead of 
the label
since another entity that might have the search term as actual label would be 
dropped from
the result set immediately when performing auto-completion.

Change-Id: Iee46d20dee5c7ea6d650254c124d2581f344b1cf
---
M lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
M lib/tests/qunit/jquery.wikibase/jquery.wikibase.entityselector.tests.js
2 files changed, 109 insertions(+), 24 deletions(-)

Approvals:
  Tobias Gritschacher: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
index d7ed5df..10cd9a8 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
@@ -443,14 +443,7 @@
                                                // Aliases array is returned 
only when there is a search hit on an alias.
                                                // If the label does not start 
with the search string, auto-complete the
                                                // alias.
-                                               if ( firstMatch.aliases
-                                                       && (
-                                                               
!firstMatch.label // consider label not set at all
-                                                               || 
firstMatch.label.toLowerCase().indexOf(
-                                                                       
searchTerm.toLowerCase()
-                                                               ) !== 0
-                                                       )
-                                               ) {
+                                               if ( 
this._shouldAutocompleteOnAlias( firstMatch, searchTerm ) ) {
                                                        stringToAutocomplete = 
firstMatch.aliases[0];
                                                }
 
@@ -493,6 +486,35 @@
                        }
                },
 
+               _shouldAutocompleteOnAlias: function( firstMatch, searchTerm ) {
+                       if( !firstMatch.aliases ) {
+                               return false;
+                       }
+
+                       var label = ( firstMatch.label )
+                               ? firstMatch.label.toLowerCase()
+                               : null; // Entity does not have a label.
+
+                       return !label
+                               || label.indexOf( searchTerm.toLowerCase() ) 
!== 0
+                               || this._labelStartsWithAlias( label, 
firstMatch );
+               },
+
+               _labelStartsWithAlias: function( label, firstMatch ) {
+                       // If the label string starts with one of the alias 
strings,
+                       // auto-complete the alias to not cause dropping of 
other search
+                       // results (see QUnit tests):
+                       for( var i = 0; i < firstMatch.aliases.length; i++ ) {
+                               var alias = firstMatch.aliases[i].toLowerCase();
+
+                               if( label.indexOf( alias ) === 0 ) {
+                                       return true;
+                               }
+                       }
+
+                       return false;
+               },
+
                /**
                 * Checks whether the value specified in the input box matches 
the first suggested item.
                 * If not, the user changed the input value after selecting an 
item, so the actual value
diff --git 
a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.entityselector.tests.js 
b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.entityselector.tests.js
index 2064ac0..5b352a1 100644
--- a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.entityselector.tests.js
+++ b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.entityselector.tests.js
@@ -35,8 +35,8 @@
        } ) );
 
        QUnit.test( 'Basic tests', function( assert ) {
-               var input = newTestEntitySelector();
-               var entityselector = input.data( 'entityselector' );
+               var $input = newTestEntitySelector();
+               var entityselector = $input.data( 'entityselector' );
                var exampleResponse = {
                        searchinfo: {
                                totalhits: 3,
@@ -65,19 +65,19 @@
                        success: 1
                };
 
-               input.on( 'entityselectorresponse', $.proxy( function( event, 
items ) {
+               $input.on( 'entityselectorresponse', $.proxy( function( event, 
items ) {
                        assert.deepEqual(
                                items,
                                exampleResponse.search,
                                'Fired response event passing result set.'
                        );
                }, this ) );
-               input.val( 'ab' );
+               $input.val( 'ab' );
                entityselector._success( exampleResponse );
-               input.off( 'entityselectorresponse' );
+               $input.off( 'entityselectorresponse' );
 
                assert.equal(
-                       input.val(),
+                       $input.val(),
                        'abc',
                        'Replaced input element\'s value with first result 
(remaining part of the string is highlighted).'
                );
@@ -103,14 +103,14 @@
                entityselector.close();
 
                entityselector.destroy();
-               input.remove();
+               $input.remove();
 
                exampleResponse['search-continue'] = 4;
 
-               input = newTestEntitySelector( { limit: 1 } );
-               entityselector = input.data( 'entityselector' );
+               $input = newTestEntitySelector( { limit: 1 } );
+               entityselector = $input.data( 'entityselector' );
 
-               input.val( 'ab' );
+               $input.val( 'ab' );
                entityselector._success( exampleResponse );
 
                assert.equal(
@@ -120,7 +120,7 @@
                );
 
                entityselector.offset = 0;
-               input.val( 'ab' );
+               $input.val( 'ab' );
                entityselector._success( exampleResponse );
 
                assert.equal(
@@ -129,11 +129,39 @@
                        'Cleared result cache after resetting the offset.'
                );
 
+               exampleResponse = {
+                       searchinfo: {
+                               totalhits: 1,
+                               search: 'a'
+                       },
+                       search: [
+                               {
+                                       id: 1,
+                                       label: 'ab',
+                                       aliases: ['abc'],
+                                       score: 1
+                               }
+                       ],
+                       success: 1
+               };
+
+               $input = newTestEntitySelector();
+               entityselector = $input.data( 'entityselector' );
+               $input.val( 'a' );
+               entityselector._success( exampleResponse );
+
+               assert.equal(
+                       $input.val(),
+                       'ab',
+                       'Auto-completing label regardless of an alias string 
starting with the label string.'
+               );
+
+               $input.remove();
        } );
 
        QUnit.test( 'Auto-complete alias instead of label', function( assert ) {
-               var input = newTestEntitySelector();
-               var entityselector = input.data( 'entityselector' );
+               var $input = newTestEntitySelector();
+               var entityselector = $input.data( 'entityselector' );
                var exampleResponse = {
                        searchinfo: {
                                totalhits: 1,
@@ -150,16 +178,51 @@
                        success: 1
                };
 
-               input.val( 'ab' );
+               $input.val( 'ab' );
                entityselector._success( exampleResponse );
 
                assert.equal(
-                       input.val(),
+                       $input.val(),
                        'abc',
                        'Auto-completed alias instead of label.'
                );
 
-               input.remove();
+               $input.remove();
+
+               // When auto-completing the label, the second result would be 
dropped:
+               exampleResponse = {
+                       searchinfo: {
+                               totalhits: 2,
+                               search: 'a'
+                       },
+                       search: [
+                               {
+                                       id: 1,
+                                       label: 'ab',
+                                       aliases: ['a'],
+                                       score: 1
+                               },
+                               {
+                                       id: 1,
+                                       label: 'a',
+                                       score: 1
+                               }
+                       ],
+                       success: 1
+               };
+
+               $input = newTestEntitySelector();
+               entityselector = $input.data( 'entityselector' );
+               $input.val( 'a' );
+               entityselector._success( exampleResponse );
+
+               assert.equal(
+                       $input.val(),
+                       'a',
+                       'Auto-completing alias instead of label when label 
string starts with an alias string.'
+               );
+
+               $input.remove();
        } );
 
 }( jQuery, QUnit ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Iee46d20dee5c7ea6d650254c124d2581f344b1cf
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@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