From 1fc36c15507348fe8b53547890c7744e701bb18c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 3 Oct 2022 22:29:05 +0400 Subject: [PATCH 1/2] Block Editor: Fix block search for non-Latin characters Restore usage of `lodash.words` instead of `noCase`; the latter only supports Latin characters. --- .eslintrc.js | 1 - package-lock.json | 1 - packages/block-editor/package.json | 1 - .../src/components/inserter/search-items.js | 18 +++--------------- 4 files changed, 3 insertions(+), 18 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 4669050d0619be..6ab56997db8171 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -164,7 +164,6 @@ module.exports = { 'uniqWith', 'upperFirst', 'values', - 'words', 'xor', 'zip', ], diff --git a/package-lock.json b/package-lock.json index 9986f336b11f78..f1315a7b90ea29 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17355,7 +17355,6 @@ "@wordpress/url": "file:packages/url", "@wordpress/warning": "file:packages/warning", "@wordpress/wordcount": "file:packages/wordcount", - "change-case": "^4.1.2", "classnames": "^2.3.1", "colord": "^2.7.0", "diff": "^4.0.2", diff --git a/packages/block-editor/package.json b/packages/block-editor/package.json index 92a5ae12b53ae0..0e8b9668ed49c0 100644 --- a/packages/block-editor/package.json +++ b/packages/block-editor/package.json @@ -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", diff --git a/packages/block-editor/src/components/inserter/search-items.js b/packages/block-editor/src/components/inserter/search-items.js index c2f8da7a6001b2..f8edd14fc5ff49 100644 --- a/packages/block-editor/src/components/inserter/search-items.js +++ b/packages/block-editor/src/components/inserter/search-items.js @@ -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 || ''; @@ -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. * @@ -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 ) => { @@ -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 From c78476b2e2b0aa3d7b4b7edc1899c4617a1e1f6c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 3 Oct 2022 22:31:30 +0400 Subject: [PATCH 2/2] Add unit tests to avoid similar regressions in the future --- .../block-editor/src/components/inserter/test/search-items.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-editor/src/components/inserter/test/search-items.js b/packages/block-editor/src/components/inserter/test/search-items.js index 96402392d36f4d..a33da9d580230e 100644 --- a/packages/block-editor/src/components/inserter/test/search-items.js +++ b/packages/block-editor/src/components/inserter/test/search-items.js @@ -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', () => {