alert_words: Highlight overlapping alert words more accurately.

This fixes a bug where alert words that are contained in another alert
word would have only the smaller alert word highlighted.

Note that it does not handle weirder cases, like "one two" and "two
three" being alert words; "two three" will always be highlighted, and
not "one"; this is probably fine in practice.

Fixes: #28415
Co-Authored-By: danieldotcomcoder <daniel_shdeed@hotmail.com>
This commit is contained in:
shashank-23002 2024-03-19 07:05:51 +00:00 committed by Tim Abbott
parent ebafe4d08f
commit b74dabfaa4
3 changed files with 18 additions and 2 deletions

View File

@ -8,7 +8,12 @@ import * as people from "./people";
let my_alert_words: string[] = [];
export function set_words(words: string[]): void {
// This module's highlighting algorithm of greedily created
// highlight spans cannot correctly handle overlapping alert word
// clauses, but processing in order from longest-to-shortest
// reduces some symptoms of this. See #28415 for details.
my_alert_words = words;
my_alert_words.sort((a, b) => b.length - a.length);
}
export function get_word_list(): {word: string}[] {

View File

@ -116,7 +116,18 @@ run_test("notifications", () => {
run_test("munging", () => {
alert_words.initialize(params);
assert.deepEqual(alert_words.get_word_list(), [
{word: "alertthree"},
{word: "alertone"},
{word: "alerttwo"},
{word: "al*rt.*s"},
{word: "emoji"},
{word: `5'11"`},
{word: "FD&C"},
{word: ".+"},
{word: "<3"},
{word: ">8"},
]);
let saved_content = regular_message.content;
alert_words.process_message(regular_message);
assert.equal(saved_content, regular_message.content);

View File

@ -160,7 +160,7 @@ run_test("alert_words", ({override}) => {
const event = event_fixtures.alert_words;
dispatch(event);
assert.deepEqual(alert_words.get_word_list(), [{word: "fire"}, {word: "lunch"}]);
assert.deepEqual(alert_words.get_word_list(), [{word: "lunch"}, {word: "fire"}]);
assert.ok(alert_words.has_alert_word("fire"));
assert.ok(alert_words.has_alert_word("lunch"));
});