Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typeahead: Fix multi-word queries not matching names with diacritics.
Pratik Chanda8936b0e9b638
diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.tsindex 779d4c8ff4f..d7e713628db 100644--- a/web/src/composebox_typeahead.ts+++ b/web/src/composebox_typeahead.ts@@ -215,30 +215,48 @@ export function get_language_matcher(query: string): (language: string) => booleexport function get_stream_matcher(query: string): (stream: StreamPillData) => boolean {// Case-insensitive.- query = typeahead.clean_query_lowercase(query);+ query = typeahead.clean_query_lowercase(query, false);+ const should_remove_diacritics = !typeahead.contains_diacritics(query);return function (stream: StreamPillData) {- return typeahead_helper.query_matches_stream_name(query, stream);+ return typeahead_helper.query_matches_stream_name(query, stream, should_remove_diacritics);};}export function get_slash_matcher(query: string): (item: SlashCommand) => boolean {- query = typeahead.clean_query_lowercase(query);+ query = typeahead.clean_query_lowercase(query, false);+ const should_remove_diacritics = !typeahead.contains_diacritics(query);return function (item: SlashCommand) {return (- typeahead.query_matches_string_in_order(query, item.name, " ") ||- typeahead.query_matches_string_in_order(query, item.aliases, " ")+ typeahead.query_matches_string_in_order(+ query,+ item.name,+ " ",+ should_remove_diacritics,+ ) ||+ typeahead.query_matches_string_in_order(+ query,+ item.aliases,+ " ",+ should_remove_diacritics,+ ));};}function get_topic_matcher(query: string): (topic: string) => boolean {- query = typeahead.clean_query_lowercase(query);+ query = typeahead.clean_query_lowercase(query, false);+ const should_remove_diacritics = !typeahead.contains_diacritics(query);return function (topic: string): boolean {const topic_display_name = util.get_final_topic_display_name(topic);- return typeahead.query_matches_string_in_order(query, topic_display_name, " ");+ return typeahead.query_matches_string_in_order(+ query,+ topic_display_name,+ " ",+ should_remove_diacritics,+ );};}@@ -689,17 +707,13 @@ function filter_persons<T>(}export function get_person_suggestion_for_topic_typeahead(query: string): UserPillData[] {- query = typeahead.clean_query_lowercase(query);+ query = typeahead.clean_query_lowercase(query, false);+ const should_remove_diacritics = !typeahead.contains_diacritics(query);- const filterer = (person_items: UserPillData[]): UserPillData[] => {- const should_remove_diacritics = people.should_remove_diacritics_for_query(- query.toLowerCase(),- );-- return person_items.filter((item) =>+ const filterer = (person_items: UserPillData[]): UserPillData[] =>+ person_items.filter((item) =>typeahead_helper.query_matches_person_name(query, item, should_remove_diacritics, true),);- };const current_narrow_participant_ids = message_lists.current?.data.participants.visible();@@ -766,7 +780,8 @@ export function get_person_suggestions(opts: PersonSuggestionOpts,exclude_non_welcome_bots = false,): (UserOrMentionPillData | UserGroupPillData)[] {- query = typeahead.clean_query_lowercase(query);+ query = typeahead.clean_query_lowercase(query, false);+ const should_remove_diacritics = !typeahead.contains_diacritics(query);let groups: UserGroup[];if (opts.filter_groups_for_mention) {@@ -805,7 +820,7 @@ export function get_person_suggestions(}));const filtered_groups = group_pill_data.filter((item) =>- typeahead_helper.query_matches_group_name(query, item),+ typeahead_helper.query_matches_group_name(query, item, should_remove_diacritics),);const user = people.get_from_unique_full_name(query);@@ -857,9 +872,6 @@ export function get_person_suggestions(broadcast_items: UserOrMentionPillData[],): UserOrMentionPillData[] {const suggestion_items: UserOrMentionPillData[] = [...person_items, ...broadcast_items];- const should_remove_diacritics = people.should_remove_diacritics_for_query(- query.toLowerCase(),- );return suggestion_items.filter((item) =>typeahead_helper.query_matches_person(@@ -1263,8 +1275,9 @@ export function get_candidates(}export function content_item_html(- _query: string,+ query: string,): (item: TypeaheadSuggestion) => string | undefined {+ const should_remove_diacritics = !typeahead.contains_diacritics(query);return function (item: TypeaheadSuggestion): string | undefined {switch (item.type) {case "emoji":@@ -1272,7 +1285,10 @@ export function content_item_html(case "user_group":case "user":case "broadcast":- return typeahead_helper.render_person_or_user_group(item, token);+ return typeahead_helper.render_person_or_user_group(item, {+ query: token,+ should_remove_diacritics,+ });case "slash":return typeahead_helper.render_typeahead_item({primary: item.text,diff --git a/web/src/pill_typeahead.ts b/web/src/pill_typeahead.tsindex 487a200694b..ebd5d196611 100644--- a/web/src/pill_typeahead.ts+++ b/web/src/pill_typeahead.ts@@ -7,6 +7,7 @@ import * as people from "./people.ts";import type {User} from "./people.ts";import * as stream_pill from "./stream_pill.ts";import type {StreamPillData, StreamPillWidget} from "./stream_pill.ts";+import * as typeahead from "./typeahead.ts";import * as typeahead_helper from "./typeahead_helper.ts";import type {CombinedPillContainer, GroupSettingPillContainer} from "./typeahead_helper.ts";import * as user_group_pill from "./user_group_pill.ts";@@ -26,8 +27,12 @@ function person_matcher();}-function group_matcher(query: string, item: UserGroupPillData): boolean {- return typeahead_helper.query_matches_group_name(query, item);+function group_matcher(+ query: string,+ item: UserGroupPillData,+ should_remove_diacritics: boolean,+): boolean {+ return typeahead_helper.query_matches_group_name(query, item, should_remove_diacritics);}type TypeaheadItem = UserGroupPillData | StreamPillData | UserPillData;@@ -55,8 +60,7 @@ export function set_up_user(return (item: UserPillData) => typeahead_helper.render_person(item);},matcher(query: string): (item: UserPillData) => boolean {- query = query.toLowerCase();- query = query.replaceAll("\u00A0", " ");+ query = typeahead.clean_query_lowercase(query, false);const should_remove_diacritics = people.should_remove_diacritics_for_query(query);return (item: UserPillData) => person_matcher(query, item, should_remove_diacritics);},@@ -156,10 +160,11 @@ export function set_up_user_group(return (item: UserGroupPillData) => typeahead_helper.render_user_group(item);},matcher(query: string): (item: UserGroupPillData) => boolean {- query = query.toLowerCase();- query = query.replaceAll("\u00A0", " ");+ query = typeahead.clean_query_lowercase(query, false);+ const should_remove_diacritics = people.should_remove_diacritics_for_query(query);- return (item: UserGroupPillData) => group_matcher(query, item);+ return (item: UserGroupPillData) =>+ group_matcher(query, item, should_remove_diacritics);},sorter(matches: UserGroupPillData[], query: string): UserGroupPillData[] {return typeahead_helper.sort_user_groups(matches, query);@@ -211,14 +216,13 @@ export function set_up_group_setting_typeahead(};},matcher(query: string): (item: GroupSettingTypeaheadItem) => boolean {- query = query.toLowerCase();- query = query.replaceAll("\u00A0", " ");+ query = typeahead.clean_query_lowercase(query, false);const should_remove_diacritics = people.should_remove_diacritics_for_query(query);return (item: GroupSettingTypeaheadItem): boolean => {let matches = false;if (item.type === "user_group") {- matches = matches || group_matcher(query, item);+ matches = matches || group_matcher(query, item, should_remove_diacritics);}if (item.type === "user") {@@ -360,8 +364,7 @@ export function set_up_combined(};},matcher(query: string): (item: TypeaheadItem) => boolean {- query = query.toLowerCase();- query = query.replaceAll("\u00A0", " ");+ query = typeahead.clean_query_lowercase(query, false);const should_remove_diacritics = people.should_remove_diacritics_for_query(query);return (item: TypeaheadItem): boolean => {@@ -373,14 +376,14 @@ export function set_up_combined(if (include_user_groups && query.startsWith("@")) {if (item.type === "user_group") {const normalized_query = query.slice(1);- return group_matcher(normalized_query, item);+ return group_matcher(normalized_query, item, should_remove_diacritics);}return false;}let matches = false;if (include_user_groups && item.type === "user_group") {- matches = group_matcher(query, item);+ matches = group_matcher(query, item, should_remove_diacritics);}if (include_users && item.type === "user") {diff --git a/web/src/typeahead.ts b/web/src/typeahead.tsindex 75fe8ed822a..7d218b19857 100644--- a/web/src/typeahead.ts+++ b/web/src/typeahead.ts@@ -71,6 +71,10 @@ export function remove_diacritics(s: string): string {return s.normalize("NFKD").replaceAll(unicode_marks, "");}+export function contains_diacritics(s: string): boolean {+ return remove_diacritics(s) !== s;+}+export function last_prefix_match(prefix: string, words: string[]): number | null {// This function takes in a lexicographically sorted array of `words`,// and a `prefix` string. It uses binary search to compute the index@@ -104,11 +108,11 @@ export function query_matches_string_in_order(query: string,source_str: string,split_char: string,+ should_remove_diacritics: boolean,): boolean {query = query.toLowerCase();source_str = source_str.toLowerCase();- const should_remove_diacritics = /^[a-z]+$/.test(query);if (should_remove_diacritics) {source_str = remove_diacritics(source_str);}@@ -202,8 +206,10 @@ export function query_matches_string_in_any_order(return true;}-function clean_query(query: string): string {- query = remove_diacritics(query);+function clean_query(query: string, should_remove_diacritics: boolean): string {+ if (should_remove_diacritics) {+ query = remove_diacritics(query);+ }// When `abc ` with a space at the end is typed in// a content-editable widget such as the composebox// direct message section, the space at the end was@@ -214,9 +220,9 @@ function clean_query(query: string): string {return query;}-export function clean_query_lowercase(query: string): string {+export function clean_query_lowercase(query: string, remove_diacritics = true): string {query = query.toLowerCase();- query = clean_query(query);+ query = clean_query(query, remove_diacritics);return query;}@@ -229,13 +235,17 @@ export const parse_unicode_emoji_code = (code: string): string =>export function get_emoji_matcher(query: string): (emoji: EmojiSuggestion) => boolean {// replace spaces with underscores for emoji matchingquery = query.replaceAll(" ", "_");- query = clean_query_lowercase(query);+ query = clean_query_lowercase(query, false);+ const should_remove_diacritics = !contains_diacritics(query);return function (emoji) {const matches_emoji_literal =emoji.reaction_type === "unicode_emoji" &&parse_unicode_emoji_code(emoji.emoji_code) === query;- return matches_emoji_literal || query_matches_string_in_order(query, emoji.emoji_name, "_");+ return (+ matches_emoji_literal ||+ query_matches_string_in_order(query, emoji.emoji_name, "_", should_remove_diacritics)+ );};}diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.tsindex 78ef74b3218..e8ebaddef7a 100644--- a/web/src/typeahead_helper.ts+++ b/web/src/typeahead_helper.ts@@ -95,7 +95,10 @@ export function rewire_render_typeahead_item(value: typeof render_typeahead_itemexport let render_person = (person: UserPillData | UserOrMentionPillData,- query?: string,+ opts?: {+ query: string;+ should_remove_diacritics: boolean;+ },): string => {if (person.type === "broadcast") {return render_typeahead_item({@@ -119,11 +122,12 @@ export let render_person = (// If both the email and a custom profile field match the query, show both.let secondary_text = person.user.delivery_email;- if (query) {+ if (opts) {const email_matches = typeahead.query_matches_string_in_order(- query,+ opts.query,secondary_text ?? "","",+ opts.should_remove_diacritics,);let matched_custom_field;@@ -134,7 +138,14 @@ export let render_person = (const value = people.get_custom_profile_data(person.user.user_id, field.id)?.value;- if (typeahead.query_matches_string_in_order(query, value ?? "", "")) {+ if (+ typeahead.query_matches_string_in_order(+ opts.query,+ value ?? "",+ "",+ opts.should_remove_diacritics,+ )+ ) {matched_custom_field = value;break;}@@ -190,13 +201,16 @@ export function rewire_render_user_group(value: typeof render_user_group): voidexport function render_person_or_user_group(item: UserGroupPillData | UserPillData | UserOrMentionPillData,- query?: string,+ opts?: {+ query: string;+ should_remove_diacritics: boolean;+ },): string {if (item.type === "user_group") {return render_user_group(item);}- return render_person(item, query);+ return render_person(item, opts);}export let render_stream = (stream: StreamData): string =>@@ -1106,7 +1120,12 @@ export function query_matches_person(): boolean {if (person.type === "broadcast" &&- typeahead.query_matches_string_in_order(query, person.user.full_name, " ")+ typeahead.query_matches_string_in_order(+ query,+ person.user.full_name,+ " ",+ should_remove_diacritics,+ )) {return true;}@@ -1122,7 +1141,14 @@ export function query_matches_person(if (field.use_for_user_matching) {const field_value =people.get_custom_profile_data(person.user.user_id, field.id)?.value ?? "";- if (typeahead.query_matches_string_in_order(query, field_value, " ")) {+ if (+ typeahead.query_matches_string_in_order(+ query,+ field_value,+ " ",+ should_remove_diacritics,+ )+ ) {return true;}}@@ -1134,28 +1160,44 @@ export function query_matches_person(query,people.get_visible_email(person.user)," ",+ should_remove_diacritics,);}}return false;}-export function query_matches_stream_name(query: string, stream: StreamPillData): boolean {- return typeahead.query_matches_string_in_order(query, stream.name, " ");+export function query_matches_stream_name(+ query: string,+ stream: StreamPillData,+ should_remove_diacritics: boolean,+): boolean {+ return typeahead.query_matches_string_in_order(+ query,+ stream.name,+ " ",+ should_remove_diacritics,+ );}-export function query_matches_group_name(query: string, user_group: UserGroupPillData): boolean {+export function query_matches_group_name(+ query: string,+ user_group: UserGroupPillData,+ should_remove_diacritics: boolean,+): boolean {if (user_group.name === "role:members") {return (typeahead.query_matches_string_in_order(query,user_groups.get_display_group_name(user_group.name),"",+ should_remove_diacritics,) ||typeahead.query_matches_string_in_order(query,settings_config.alternate_members_group_typeahead_matching_name,"",+ should_remove_diacritics,));}@@ -1163,5 +1205,6 @@ export function query_matches_group_name(query: string, user_group: UserGroupPilquery,user_groups.get_display_group_name(user_group.name),"",+ should_remove_diacritics,);}diff --git a/web/tests/composebox_typeahead.test.cjs b/web/tests/composebox_typeahead.test.cjsindex 2c6abe825e5..2c39af21aaf 100644--- a/web/tests/composebox_typeahead.test.cjs+++ b/web/tests/composebox_typeahead.test.cjs@@ -1506,7 +1506,7 @@ test("initialize", ({override, override_rewire, mock_template}) => {assert.deepEqual(actual_value, expected_value);function matcher(query, person) {- query = typeahead.clean_query_lowercase(query);+ query = typeahead.clean_query_lowercase(query, false);const should_remove_diacritics =people.should_remove_diacritics_for_query(query);return typeahead_helper.query_matches_person(@@ -1674,7 +1674,7 @@ test("initialize", ({override, override_rewire, mock_template}) => {// content_item_html.ct.get_or_set_completing_for_tests("mention");ct.get_or_set_token_for_testing("othello");- actual_value = options.item_html()(othello_item);+ actual_value = options.item_html("othello")(othello_item);expected_value ='<div class="typeahead-content">\n' +' <div class="typeahead-image">\n' +@@ -1691,7 +1691,7 @@ test("initialize", ({override, override_rewire, mock_template}) => {ct.get_or_set_completing_for_tests("mention");ct.get_or_set_token_for_testing("hamletcharacters");- actual_value = options.item_html()(hamletcharacters);+ actual_value = options.item_html("hamletcharacters")(hamletcharacters);expected_value ='<div class="typeahead-content">\n' +' <i class="typeahead-image zulip-icon zulip-icon-user-group" aria-hidden="true"></i>\n' +@@ -2666,22 +2666,23 @@ test("content_item_html", ({override_rewire}) => {assert.deepEqual(item, emoji);th_render_typeahead_item_called = true;});- ct.content_item_html()(emoji);+ ct.content_item_html("")(emoji);ct.get_or_set_completing_for_tests("mention");let th_render_person_called = false;- override_rewire(typeahead_helper, "render_person", (person) => {+ override_rewire(typeahead_helper, "render_person", (person, opts) => {assert.deepEqual(person, othello_item);+ assert.ok(opts === undefined || typeof opts === "object");th_render_person_called = true;});- ct.content_item_html()(othello_item);+ ct.content_item_html("")(othello_item);let th_render_user_group_called = false;override_rewire(typeahead_helper, "render_user_group", (user_group) => {assert.deepEqual(user_group, backend);th_render_user_group_called = true;});- ct.content_item_html()(backend);+ ct.content_item_html("")(backend);// We don't have any fancy rendering for slash commands yet.ct.get_or_set_completing_for_tests("slash");@@ -2698,7 +2699,7 @@ test("content_item_html", ({override_rewire}) => {});th_render_slash_command_called = true;});- ct.content_item_html()(me_slash);+ ct.content_item_html("")(me_slash);ct.get_or_set_completing_for_tests("stream");let th_render_stream_called = false;@@ -2706,7 +2707,7 @@ test("content_item_html", ({override_rewire}) => {assert.deepEqual(stream, denmark_stream);th_render_stream_called = true;});- ct.content_item_html()(denmark_stream);+ ct.content_item_html("")(denmark_stream);ct.get_or_set_completing_for_tests("syntax");th_render_typeahead_item_called = false;@@ -2717,7 +2718,7 @@ test("content_item_html", ({override_rewire}) => {});th_render_typeahead_item_called = true;});- ct.content_item_html()({type: "syntax", language: "py"});+ ct.content_item_html("")({type: "syntax", language: "py"});// Verify that all stub functions have been called.assert.ok(th_render_typeahead_item_called);diff --git a/web/tests/typeahead.test.cjs b/web/tests/typeahead.test.cjsindex 4c7f28e7f88..a3938d21854 100644--- a/web/tests/typeahead.test.cjs+++ b/web/tests/typeahead.test.cjs@@ -75,6 +75,44 @@ run_test("get_emoji_matcher: spaces equivalent to underscores", () => {assert_equivalent("traffic l");});+run_test("query_matches_string_in_order", () => {+ function assert_matches(query, source, split_char) {+ const should_remove_diacritics = !typeahead.contains_diacritics(query);+ assert.equal(+ typeahead.query_matches_string_in_order(+ query,+ source,+ split_char,+ should_remove_diacritics,+ ),+ true,+ );+ }+ function assert_no_match(query, source, split_char) {+ const should_remove_diacritics = !typeahead.contains_diacritics(query);+ assert.equal(+ typeahead.query_matches_string_in_order(+ query,+ source,+ split_char,+ should_remove_diacritics,+ ),+ false,+ );+ }++ // Query without diacritics should match source with diacritics.+ assert_matches("jose mar", "José María", " ");+ assert_matches("maria gon", "María González", " ");+ assert_matches("jose", "José María", " ");++ // Query with diacritics does diacritic-sensitive matching+ assert_matches("josé", "José María", " ");+ assert_no_match("josé", "Jose Maria", " ");+ assert_matches("josé mar", "José María", " ");+ assert_no_match("josé mar", "Jose Maria", " ");+});+run_test("triage", () => {const alice = {name: "alice"};const alicia = {name: "Alicia"};diff --git a/web/tests/typeahead_helper.test.cjs b/web/tests/typeahead_helper.test.cjsindex 9af87a11b5c..616f88bb8c9 100644--- a/web/tests/typeahead_helper.test.cjs+++ b/web/tests/typeahead_helper.test.cjs@@ -1720,7 +1720,10 @@ test("render_person shows value of custom profile fields in secondary", ({return "typeahead-item-stub";});- assert.equal(th.render_person(a_user_item, "Alpha"), "typeahead-item-stub");+ assert.equal(+ th.render_person(a_user_item, {query: "Alpha", should_remove_diacritics: true}),+ "typeahead-item-stub",+ );assert.ok(rendered);});@@ -1754,7 +1757,10 @@ test("render_person shows both email and custom profile fields as secondary if breturn "typeahead-item-stub";});- assert.equal(th.render_person(a_user_item, "a_user"), "typeahead-item-stub");+ assert.equal(+ th.render_person(a_user_item, {query: "a_user", should_remove_diacritics: true}),+ "typeahead-item-stub",+ );assert.ok(rendered);});@@ -1789,7 +1795,10 @@ test("render_person skips custom profile fields not used for user matching", ({return "typeahead-item-stub";});- assert.equal(th.render_person(a_user_item, "Alpha"), "typeahead-item-stub");+ assert.equal(+ th.render_person(a_user_item, {query: "Alpha", should_remove_diacritics: true}),+ "typeahead-item-stub",+ );assert.ok(rendered);});