Skip to content

Commit

Permalink
Block Editor: Fix block search for non-Latin characters (#44652)
Browse files Browse the repository at this point in the history
* Restore usage of `lodash.words` instead of `noCase`; the latter only supports Latin characters.
* Add unit tests to avoid similar regressions in the future.
  • Loading branch information
Mamaduka committed Oct 4, 2022
1 parent b1f1d56 commit bb307eb
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 18 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ module.exports = {
'uniqWith',
'upperFirst',
'values',
'words',
'xor',
'zip',
],
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"@wordpress/url": "file:../url",
"@wordpress/warning": "file:../warning",
"@wordpress/wordcount": "file:../wordcount",
"change-case": "^4.1.2",
"classnames": "^2.3.1",
"colord": "^2.7.0",
"diff": "^4.0.2",
Expand Down
18 changes: 3 additions & 15 deletions packages/block-editor/src/components/inserter/search-items.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* External dependencies
*/
import { noCase } from 'change-case';
import removeAccents from 'remove-accents';
import { find } from 'lodash';
import { find, words } from 'lodash';

// Default search helpers.
const defaultGetName = ( item ) => item.name || '';
Expand Down Expand Up @@ -36,17 +35,6 @@ function normalizeSearchInput( input = '' ) {
return input;
}

/**
* Extracts words from an input string.
*
* @param {string} input The input string.
*
* @return {Array} Words, extracted from the input string.
*/
function extractWords( input = '' ) {
return noCase( input ).split( ' ' ).filter( Boolean );
}

/**
* Converts the search term into a list of normalized terms.
*
Expand All @@ -55,7 +43,7 @@ function extractWords( input = '' ) {
* @return {string[]} The normalized list of search terms.
*/
export const getNormalizedSearchTerms = ( input = '' ) => {
return extractWords( normalizeSearchInput( input ) );
return words( normalizeSearchInput( input ) );
};

const removeMatchingTerms = ( unmatchedTerms, unprocessedTerms ) => {
Expand Down Expand Up @@ -162,7 +150,7 @@ export function getItemSearchRank( item, searchTerm, config = {} ) {
category,
collection,
].join( ' ' );
const normalizedSearchTerms = extractWords( normalizedSearchInput );
const normalizedSearchTerms = words( normalizedSearchInput );
const unmatchedTerms = removeMatchingTerms(
normalizedSearchTerms,
terms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ describe( 'getNormalizedSearchTerms', () => {
getNormalizedSearchTerms( ' Média & Text Tag-Cloud > 123' )
).toEqual( [ 'media', 'text', 'tag', 'cloud', '123' ] );
} );

it( 'should support non-latin letters', () => {
expect( getNormalizedSearchTerms( 'მედია' ) ).toEqual( [ 'მედია' ] );
} );
} );

describe( 'getItemSearchRank', () => {
Expand Down

0 comments on commit bb307eb

Please sign in to comment.