Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typeahead: Make code-language filtering diacritics-agnostic.

Aadyot1c8bd32da742d
diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.ts
index e0d79c7acd7..cdef3d3fd68 100644
--- a/web/src/composebox_typeahead.ts
+++ b/web/src/composebox_typeahead.ts
@@ -207,15 +207,17 @@ export function topics_seen_for(stream_id?: number): string[] {
}
export function get_language_matcher(query: string): (language: string) => boolean {
- query = query.toLowerCase();
+ // Filtering is diacritics-agnostic: strip diacritics from both the query
+ // and the language name so ASCII and diacritic spellings match each other.
+ query = typeahead.remove_diacritics(query.toLowerCase());
return function (language: string): boolean {
- return language.includes(query);
+ return typeahead.remove_diacritics(language.toLowerCase()).includes(query);
};
}
export function get_stream_matcher(query: string): (stream: StreamPillData) => boolean {
// Case-insensitive.
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
const should_remove_diacritics = !typeahead.contains_diacritics(query);
return function (stream: StreamPillData) {
@@ -224,7 +226,7 @@ export function get_stream_matcher(query: string): (stream: StreamPillData) => b
}
export function get_slash_matcher(query: string): (item: SlashCommand) => boolean {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
const should_remove_diacritics = !typeahead.contains_diacritics(query);
return function (item: SlashCommand) {
@@ -246,7 +248,7 @@ export function get_slash_matcher(query: string): (item: SlashCommand) => boolea
}
function get_topic_matcher(query: string): (topic: string) => boolean {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
const should_remove_diacritics = !typeahead.contains_diacritics(query);
return function (topic: string): boolean {
@@ -712,7 +714,7 @@ function filter_persons<T>(
}
export function get_person_suggestion_for_topic_typeahead(query: string): UserPillData[] {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
const filterer = (person_items: UserPillData[]): UserPillData[] =>
person_items.filter((item) =>
@@ -783,7 +785,7 @@ export function get_person_suggestions(
opts: PersonSuggestionOpts,
exclude_non_welcome_bots = false,
): (UserOrMentionPillData | UserGroupPillData)[] {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
let groups: UserGroup[];
if (opts.filter_groups_for_mention) {
diff --git a/web/src/pill_typeahead.ts b/web/src/pill_typeahead.ts
index d966686825e..4f5d8569355 100644
--- a/web/src/pill_typeahead.ts
+++ b/web/src/pill_typeahead.ts
@@ -51,7 +51,7 @@ export function set_up_user(
return (item: UserPillData) => typeahead_helper.render_person(item);
},
matcher(query: string): (item: UserPillData) => boolean {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
return (item: UserPillData) => person_matcher(query, item);
},
sorter(matches: UserPillData[], query: string): UserPillData[] {
@@ -146,7 +146,7 @@ export function set_up_user_group(
return (item: UserGroupPillData) => typeahead_helper.render_user_group(item);
},
matcher(query: string): (item: UserGroupPillData) => boolean {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
return (item: UserGroupPillData) => group_matcher(query, item);
},
sorter(matches: UserGroupPillData[], query: string): UserGroupPillData[] {
@@ -194,7 +194,7 @@ export function set_up_group_setting_typeahead(
};
},
matcher(query: string): (item: GroupSettingTypeaheadItem) => boolean {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
return (item: GroupSettingTypeaheadItem): boolean => {
let matches = false;
@@ -338,7 +338,7 @@ export function set_up_combined(
};
},
matcher(query: string): (item: TypeaheadItem) => boolean {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
return (item: TypeaheadItem): boolean => {
if (include_streams(query) && item.type === "stream") {
diff --git a/web/src/realm_playground.ts b/web/src/realm_playground.ts
index caa5e195c94..61d0fb911a5 100644
--- a/web/src/realm_playground.ts
+++ b/web/src/realm_playground.ts
@@ -74,6 +74,8 @@ export function get_pygments_typeahead_list_for_settings(query: string): Map<str
// Adds a typeahead that allows selecting a custom language, by adding a
// "Custom language" label in the first position of the typeahead list.
+ // Selecting this option registers the query as typed, diacritics
+ // included; the matcher strips diacritics on both sides instead.
const clean_query = typeahead.clean_query_lowercase(query);
if (clean_query !== "") {
language_labels.set(
diff --git a/web/src/settings_playgrounds.ts b/web/src/settings_playgrounds.ts
index 90c28c9b0ce..3f15d691262 100644
--- a/web/src/settings_playgrounds.ts
+++ b/web/src/settings_playgrounds.ts
@@ -169,17 +169,22 @@ function build_page(): void {
return (item: string) => render_typeahead_item({primary: language_labels.get(item)});
},
matcher(query: string): (item: string) => boolean {
- const q = query.trim().toLowerCase();
+ // Filtering is diacritics-agnostic: strip diacritics from both the query
+ // and the language name so ASCII and diacritic spellings match each other.
+ const q = typeahead.remove_diacritics(query.trim().toLowerCase());
if (q === "") {
return () => true;
}
+ const begins_with_query = (name: string): boolean =>
+ typeahead.remove_diacritics(name.toLowerCase()).startsWith(q);
+
return (item: string) =>
- item.toLowerCase().startsWith(q) ||
+ begins_with_query(item) ||
realm_playground
.get_aliases_for_pretty_name(item)
- .some((alias) => alias.toLowerCase().startsWith(q));
+ .some((alias) => begins_with_query(alias));
},
sorter(items: string[], query: string): string[] {
const q = query.trim().toLowerCase();
diff --git a/web/src/typeahead.ts b/web/src/typeahead.ts
index 0241749671d..3604e1e17e7 100644
--- a/web/src/typeahead.ts
+++ b/web/src/typeahead.ts
@@ -205,10 +205,7 @@ export function query_matches_string_in_any_order(
return true;
}
-function clean_query(query: string, should_remove_diacritics: boolean): string {
- if (should_remove_diacritics) {
- query = remove_diacritics(query);
- }
+function clean_query(query: string): string {
// 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
@@ -219,9 +216,9 @@ function clean_query(query: string, should_remove_diacritics: boolean): string {
return query;
}
-export function clean_query_lowercase(query: string, remove_diacritics = true): string {
+export function clean_query_lowercase(query: string): string {
query = query.toLowerCase();
- query = clean_query(query, remove_diacritics);
+ query = clean_query(query);
return query;
}
@@ -234,7 +231,7 @@ 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 matching
query = query.replaceAll(" ", "_");
- query = clean_query_lowercase(query, false);
+ query = clean_query_lowercase(query);
const should_remove_diacritics = !contains_diacritics(query);
return function (emoji) {
diff --git a/web/tests/composebox_typeahead.test.cjs b/web/tests/composebox_typeahead.test.cjs
index 0fd1a7fa206..9ac05043f54 100644
--- a/web/tests/composebox_typeahead.test.cjs
+++ b/web/tests/composebox_typeahead.test.cjs
@@ -1602,7 +1602,7 @@ test("initialize", ({override, override_rewire, mock_template}) => {
assert.deepEqual(actual_value, expected_value);
function matcher(query, person) {
- query = typeahead.clean_query_lowercase(query, false);
+ query = typeahead.clean_query_lowercase(query);
return typeahead_helper.query_matches_person(query, person);
}
@@ -1804,6 +1804,13 @@ test("initialize", ({override, override_rewire, mock_template}) => {
assert.equal(matcher("python"), true);
assert.equal(matcher("javascript"), false);
+ // Language matching is diacritics-agnostic: a diacritic query
+ // matches an ASCII language name and vice versa.
+ matcher = ct.get_language_matcher("café");
+ assert.equal(matcher("cafe"), true);
+ matcher = ct.get_language_matcher("cafe");
+ assert.equal(matcher("café"), true);
+
// options.sorter()
actual_value = typeahead.sort_emojis(
[make_emoji(emoji_stadium), make_emoji(emoji_tada)],
diff --git a/web/tests/realm_playground.test.cjs b/web/tests/realm_playground.test.cjs
index cb23b193f07..7a3128d99a2 100644
--- a/web/tests/realm_playground.test.cjs
+++ b/web/tests/realm_playground.test.cjs
@@ -118,4 +118,12 @@ run_test("get_pygments_typeahead_list_for_settings", () => {
assert.equal(iterator.next().value[1], "Text only (text, text)");
assert.equal(iterator.next().value[1], "quote (quote, quote)");
assert.equal(iterator.next().value[1], "spoiler (spoiler, spoiler)");
+
+ // Test typing a query with diacritics. The "Custom language" option keeps them.
+ candidates = realm_playground.get_pygments_typeahead_list_for_settings("Français");
+ iterator = candidates.entries();
+ assert.deepEqual(iterator.next().value, [
+ "français",
+ $t({defaultMessage: "Custom language: {query}"}, {query: "français"}),
+ ]);
});