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.

util: Extract convert_emoji_code_to_unicode helper.

Pratik Chanda5ad4f2f8e8a9
diff --git a/web/src/emoji_picker.ts b/web/src/emoji_picker.ts
index e39459576bf..a976b6e713b 100644
--- a/web/src/emoji_picker.ts
+++ b/web/src/emoji_picker.ts
@@ -419,9 +419,9 @@ export function is_emoji_present_in_text(text: string, emoji_dict: EmojiDict): b
const emoji_info = emoji.get_emoji_details_by_name(emoji_dict.name);
if (emoji_info.reaction_type === "unicode_emoji") {
// convert emoji_dict to an actual emoji character
- const parsed_emoji_code = typeahead.parse_unicode_emoji_code(emoji_info.emoji_code);
+ const parsed_emoji_code = util.convert_emoji_code_to_unicode(emoji_info.emoji_code);
- return text.includes(parsed_emoji_code);
+ return parsed_emoji_code !== undefined && text.includes(parsed_emoji_code);
}
return false;
diff --git a/web/src/typeahead.ts b/web/src/typeahead.ts
index 3604e1e17e7..9f87d63eae4 100644
--- a/web/src/typeahead.ts
+++ b/web/src/typeahead.ts
@@ -1,5 +1,7 @@
import _ from "lodash";
+import * as util from "./util.ts";
+
/*
We hand selected the following emojis a few years
ago to be given extra precedence in our typeahead
@@ -222,12 +224,6 @@ export function clean_query_lowercase(query: string): string {
return query;
}
-export const parse_unicode_emoji_code = (code: string): string =>
- code
- .split("-")
- .map((hex) => String.fromCodePoint(Number.parseInt(hex, 16)))
- .join("");
-
export function get_emoji_matcher(query: string): (emoji: EmojiSuggestion) => boolean {
// replace spaces with underscores for emoji matching
query = query.replaceAll(" ", "_");
@@ -237,7 +233,7 @@ export function get_emoji_matcher(query: string): (emoji: EmojiSuggestion) => bo
return function (emoji) {
const matches_emoji_literal =
emoji.reaction_type === "unicode_emoji" &&
- parse_unicode_emoji_code(emoji.emoji_code) === query;
+ util.convert_emoji_code_to_unicode(emoji.emoji_code) === query;
return (
matches_emoji_literal ||
query_matches_string_in_order(query, emoji.emoji_name, "_", should_remove_diacritics)
diff --git a/web/src/ui_util.ts b/web/src/ui_util.ts
index 8d10d6e2100..0de45c06dc7 100644
--- a/web/src/ui_util.ts
+++ b/web/src/ui_util.ts
@@ -5,6 +5,7 @@ import type * as tippy from "tippy.js";
import * as blueslip from "./blueslip.ts";
import * as hash_parser from "./hash_parser.ts";
import * as keydown_util from "./keydown_util.ts";
+import * as util from "./util.ts";
// Add functions to this that have no non-trivial
// dependencies other than jQuery.
@@ -54,24 +55,13 @@ export function convert_emoji_element_to_unicode($emoji_elt: JQuery): string {
return $emoji_elt.text();
}
- const emoji_code_parts = emoji_code_hex_string.split("-");
- const emoji_unicode = emoji_code_parts
- .map((emoji_code) => {
- const emoji_code_int = Number.parseInt(emoji_code, 16);
- // Validate the parameter passed to String.fromCodePoint() (here, emoji_code_int).
- // "An integer between 0 and 0x10FFFF (inclusive) representing a Unicode code point."
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
- // for details.
- if (
- Number.isNaN(emoji_code_int) ||
- !(emoji_code_int >= 0 && emoji_code_int <= 0x10ffff)
- ) {
- blueslip.error("Invalid unicode codepoint for emoji", {emoji_code_int});
- return $emoji_elt.text();
- }
- return String.fromCodePoint(emoji_code_int);
- })
- .join("");
+ const emoji_unicode = util.convert_emoji_code_to_unicode(emoji_code_hex_string);
+ if (emoji_unicode === undefined) {
+ blueslip.error("Invalid unicode codepoint for emoji", {
+ emoji_code: emoji_code_hex_string,
+ });
+ return $emoji_elt.text();
+ }
return emoji_unicode;
}
diff --git a/web/src/util.ts b/web/src/util.ts
index aef83b32b2a..ca8453c43ad 100644
--- a/web/src/util.ts
+++ b/web/src/util.ts
@@ -644,3 +644,22 @@ export let max_text_content_width = (candidates: string[], css_class?: string):
export function rewire_max_text_content_width(value: typeof max_text_content_width): void {
max_text_content_width = value;
}
+
+export function convert_emoji_code_to_unicode(emoji_code: string): string | undefined {
+ // Convert a "-"-separated string of hex Unicode codepoints (e.g.
+ // "1f468-200d-1f373") into the emoji it represents, or undefined if
+ // any codepoint is invalid.
+ const emoji_unicode_parts: string[] = [];
+ for (const part of emoji_code.split("-")) {
+ const emoji_code_int = Number.parseInt(part, 16);
+ // Validate the parameter passed to String.fromCodePoint() (here, emoji_code_int).
+ // "An integer between 0 and 0x10FFFF (inclusive) representing a Unicode code point."
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
+ // for details.
+ if (Number.isNaN(emoji_code_int) || !(emoji_code_int >= 0 && emoji_code_int <= 0x10ffff)) {
+ return undefined;
+ }
+ emoji_unicode_parts.push(String.fromCodePoint(emoji_code_int));
+ }
+ return emoji_unicode_parts.join("");
+}
diff --git a/web/tests/typeahead.test.cjs b/web/tests/typeahead.test.cjs
index b5200869c12..8e2aa924485 100644
--- a/web/tests/typeahead.test.cjs
+++ b/web/tests/typeahead.test.cjs
@@ -59,6 +59,16 @@ run_test("matches starting at non-first word, too", () => {
run_test("matches literal unicode emoji", () => {
assert_emoji_matches("🐼", ["panda_face"]);
+
+ // An emoji whose code is not a valid codepoint doesn't match the
+ // query literally, but can still match by name.
+ const invalid_emoji = {
+ emoji_name: "invalid_emoji",
+ emoji_code: "110000",
+ reaction_type: "unicode_emoji",
+ };
+ assert.equal(typeahead.get_emoji_matcher("🐼")(invalid_emoji), false);
+ assert.equal(typeahead.get_emoji_matcher("invalid")(invalid_emoji), true);
});
run_test("get_emoji_matcher: spaces equivalent to underscores", () => {
diff --git a/web/tests/ui_util.test.cjs b/web/tests/ui_util.test.cjs
index 5188159b6c7..96c72423b84 100644
--- a/web/tests/ui_util.test.cjs
+++ b/web/tests/ui_util.test.cjs
@@ -4,6 +4,7 @@ const assert = require("node:assert/strict");
const {zrequire} = require("./lib/namespace.cjs");
const {run_test} = require("./lib/test.cjs");
+const blueslip = require("./lib/zblueslip.cjs");
const {$} = require("./lib/zjquery.cjs");
const ui_util = zrequire("ui_util");
@@ -118,4 +119,10 @@ run_test("replace_emoji_name_with_emoji_unicode", () => {
$emoji.attr("class", "emoji emoji-1f468-200d-1f373");
const man_cook_emoji = "πŸ‘¨β€πŸ³";
assert.equal(man_cook_emoji, ui_util.convert_emoji_element_to_unicode($emoji));
+
+ // An invalid codepoint falls back to the element's text.
+ $emoji.attr("class", "emoji emoji-1f468-110000-1f373");
+ $emoji.text(":man_cook:");
+ blueslip.expect("error", "Invalid unicode codepoint for emoji");
+ assert.equal(ui_util.convert_emoji_element_to_unicode($emoji), ":man_cook:");
});
diff --git a/web/tests/util.test.cjs b/web/tests/util.test.cjs
index 7a2789035dd..64628526181 100644
--- a/web/tests/util.test.cjs
+++ b/web/tests/util.test.cjs
@@ -709,3 +709,17 @@ run_test("parse_youtube_start_time", () => {
assert.equal(util.parse_youtube_start_time("https://youtu.be/ID?t=1h30m"), 5400);
assert.equal(util.parse_youtube_start_time("https://youtu.be/ID?t=invalid"), undefined);
});
+
+run_test("convert_emoji_code_to_unicode", () => {
+ assert.equal(util.convert_emoji_code_to_unicode("1f419"), "πŸ™");
+ assert.equal(util.convert_emoji_code_to_unicode("1F419"), "πŸ™");
+ assert.equal(util.convert_emoji_code_to_unicode("1f468-200d-1f373"), "πŸ‘¨β€πŸ³");
+
+ // Codepoints must be valid hex within the Unicode range.
+ assert.equal(util.convert_emoji_code_to_unicode("110000"), undefined);
+ assert.equal(util.convert_emoji_code_to_unicode("zzzz"), undefined);
+ assert.equal(util.convert_emoji_code_to_unicode(""), undefined);
+ // A single invalid codepoint invalidates the whole emoji.
+ assert.equal(util.convert_emoji_code_to_unicode("1f468-110000-1f373"), undefined);
+ assert.equal(util.convert_emoji_code_to_unicode("1f468--1f373"), undefined);
+});