Henning Snater has uploaded a new change for review. https://gerrit.wikimedia.org/r/78365
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, 107 insertions(+), 25 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/65/78365/1 diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js index d7ed5df..7720546 100644 --- a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js +++ b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js @@ -443,15 +443,34 @@ // 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 - ) - ) { - stringToAutocomplete = firstMatch.aliases[0]; + if( firstMatch.aliases ) { + var label = ( firstMatch.label ) + ? firstMatch.label.toLowerCase() + : null; // Entity does not have a label. + + var labelStartsWithAlias = false; + + if( label ) { + // 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( i = 0; i < firstMatch.aliases.length; i++ ) { + var alias = firstMatch.aliases[i].toLowerCase(); + + if( label.indexOf( alias ) === 0 ) { + labelStartsWithAlias = true; + break; + } + } + } + + if( + !label + || label.indexOf( searchTerm.toLowerCase() ) !== 0 + || labelStartsWithAlias + ) { + stringToAutocomplete = firstMatch.aliases[0]; + } } if( this.options.adaptLetterCase ) { 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: newchange Gerrit-Change-Id: Iee46d20dee5c7ea6d650254c124d2581f344b1cf Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits