Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
people: Fix should_remove_diacritics_for_query for non-letter queries.
Pratik Chanda512c583bf61c
diff --git a/web/src/people.ts b/web/src/people.tsindex 49dd0f48066..54ac404281c 100644--- a/web/src/people.ts+++ b/web/src/people.ts@@ -1185,11 +1185,9 @@ export function get_people_for_search_bar(query: string): User[] {export function should_remove_diacritics_for_query(query_lower_case: string): boolean {// We only do diacritic-sensitive matching for queries that do not- // contain diacritics themselves.- //- // TODO: This check is too strict; ideally we'd check for presence- // of diacritics; punctuation should not be relevant.- return /^[a-z]+$/.test(query_lower_case);+ // contain diacritics themselves. Spaces and other punctuation are+ // irrelevant to this check.+ return typeahead.remove_diacritics(query_lower_case) === query_lower_case;}export function maybe_remove_diacritics_from_name(diff --git a/web/src/pill_typeahead.ts b/web/src/pill_typeahead.tsindex bf588a588bd..487a200694b 100644--- a/web/src/pill_typeahead.ts+++ b/web/src/pill_typeahead.ts@@ -15,10 +15,14 @@ import type {UserGroup} from "./user_groups.ts";import * as user_pill from "./user_pill.ts";import type {UserPillData, UserPillWidget} from "./user_pill.ts";-function person_matcher(query: string, item: UserPillData): boolean {+function person_matcher(+ query: string,+ item: UserPillData,+ should_remove_diacritics: boolean,+): boolean {return (people.is_known_user_id(item.user.user_id) &&- typeahead_helper.query_matches_person(query, item)+ typeahead_helper.query_matches_person(query, item, should_remove_diacritics));}@@ -53,7 +57,8 @@ export function set_up_user(matcher(query: string): (item: UserPillData) => boolean {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");- return (item: UserPillData) => person_matcher(query, item);+ const should_remove_diacritics = people.should_remove_diacritics_for_query(query);+ return (item: UserPillData) => person_matcher(query, item, should_remove_diacritics);},sorter(matches: UserPillData[], query: string): UserPillData[] {const users = matches.filter((match) => people.is_known_user_id(match.user.user_id));@@ -208,6 +213,7 @@ export function set_up_group_setting_typeahead(matcher(query: string): (item: GroupSettingTypeaheadItem) => boolean {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");+ const should_remove_diacritics = people.should_remove_diacritics_for_query(query);return (item: GroupSettingTypeaheadItem): boolean => {let matches = false;@@ -216,7 +222,7 @@ export function set_up_group_setting_typeahead(}if (item.type === "user") {- matches = matches || person_matcher(query, item);+ matches = matches || person_matcher(query, item, should_remove_diacritics);}return matches;};@@ -356,6 +362,7 @@ export function set_up_combined(matcher(query: string): (item: TypeaheadItem) => boolean {query = query.toLowerCase();query = query.replaceAll("\u00A0", " ");+ const should_remove_diacritics = people.should_remove_diacritics_for_query(query);return (item: TypeaheadItem): boolean => {if (include_streams(query) && item.type === "stream") {@@ -377,7 +384,7 @@ export function set_up_combined(}if (include_users && item.type === "user") {- matches = matches || person_matcher(query, item);+ matches = matches || person_matcher(query, item, should_remove_diacritics);}return matches;};diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.tsindex f130217088d..78ef74b3218 100644--- a/web/src/typeahead_helper.ts+++ b/web/src/typeahead_helper.ts@@ -1079,11 +1079,10 @@ export function rewire_sort_user_groups(value: typeof sort_user_groups): void {export function query_matches_person_name(query: string,person: UserPillData,- should_remove_diacritics?: boolean,+ should_remove_diacritics: boolean,match_prefix?: boolean,): boolean {query = query.toLowerCase();- should_remove_diacritics ??= people.should_remove_diacritics_for_query(query);const full_name = people.maybe_remove_diacritics_from_name(person.user,@@ -1101,7 +1100,7 @@ export function query_matches_person_name(export function query_matches_person(query: string,person: UserPillData | UserOrMentionPillData,- should_remove_diacritics?: boolean,+ should_remove_diacritics: boolean,match_prefix?: boolean,allow_custom_profile_field_matching = false,): boolean {diff --git a/web/tests/composebox_typeahead.test.cjs b/web/tests/composebox_typeahead.test.cjsindex 32be6a8d19a..2c6abe825e5 100644--- a/web/tests/composebox_typeahead.test.cjs+++ b/web/tests/composebox_typeahead.test.cjs@@ -1507,7 +1507,13 @@ test("initialize", ({override, override_rewire, mock_template}) => {function matcher(query, person) {query = typeahead.clean_query_lowercase(query);- return typeahead_helper.query_matches_person(query, person);+ const should_remove_diacritics =+ people.should_remove_diacritics_for_query(query);+ return typeahead_helper.query_matches_person(+ query,+ person,+ should_remove_diacritics,+ );}let query;@@ -1528,6 +1534,12 @@ test("initialize", ({override, override_rewire, mock_template}) => {query = "gaël";assert.equal(matcher(query, gael_item), true);+ query = "gaël twi";+ assert.equal(matcher(query, gael_item), true);++ query = "gael twi";+ assert.equal(matcher(query, gael_item), true);+// Don't make suggestions if the last name only has whitespaces// (we're between typing names).query = "[email protected], ";diff --git a/web/tests/people.test.cjs b/web/tests/people.test.cjsindex d07ef98537e..9cf92fa98f2 100644--- a/web/tests/people.test.cjs+++ b/web/tests/people.test.cjs@@ -857,6 +857,18 @@ run_test("filtered_users", () => {filtered_people = people.filter_people_by_search_terms(users, "ëm");assert.equal(filtered_people.size, 1);assert.ok(filtered_people.has(noah.user_id));++ // Multi-word query should ignore diacritics when the query+ // itself has no diacritics.+ filtered_people = people.filter_people_by_search_terms(users, "nooa em");+ assert.equal(filtered_people.size, 2);+ assert.ok(filtered_people.has(noah.user_id));+ assert.ok(filtered_people.has(plain_noah.user_id));++ // Multi-word query with diacritics should match only Nöôáàh+ filtered_people = people.filter_people_by_search_terms(users, "nöôáàh Ëme");+ assert.equal(filtered_people.size, 1);+ assert.ok(filtered_people.has(noah.user_id));});run_test("dm_matches_search_string", () => {