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.

keydown_util: Check Enter events properly for IME edge cases.

Rohitt Vashishtha1db901cdedab
diff --git a/frontend_tests/node_tests/keydown_util.js b/frontend_tests/node_tests/keydown_util.js
index 96b03d6055b..72375da5ccf 100644
--- a/frontend_tests/node_tests/keydown_util.js
+++ b/frontend_tests/node_tests/keydown_util.js
@@ -1,5 +1,7 @@
"use strict";
+const {strict: assert} = require("assert");
+
const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
@@ -41,3 +43,36 @@ run_test("test_early_returns", () => {
$stub.trigger(e3);
});
+
+run_test("test_ime_enter_events", () => {
+ // these events shouldn't be recognized as a return keypress.
+ const event_1 = {
+ key: "Enter",
+ originalEvent: {
+ isComposing: true,
+ },
+ };
+
+ const event_2 = {
+ key: "Random",
+ originalEvent: {
+ isComposing: false,
+ },
+ };
+ assert.ok(!keydown_util.is_enter_event(event_1));
+ assert.ok(!keydown_util.is_enter_event(event_2));
+
+ // these are valid return keypress events.
+ const event_3 = {
+ key: "Enter",
+ originalEvent: {
+ isComposing: false,
+ },
+ };
+ const event_4 = {
+ key: "Enter",
+ // Edgacase: if there is no originalEvent, JQuery didn't provide the object.
+ };
+ assert.ok(keydown_util.is_enter_event(event_3));
+ assert.ok(keydown_util.is_enter_event(event_4));
+});
diff --git a/static/js/keydown_util.ts b/static/js/keydown_util.ts
index 4793b1b3ff2..fc7f6cd7107 100644
--- a/static/js/keydown_util.ts
+++ b/static/js/keydown_util.ts
@@ -33,5 +33,12 @@ export function handle(opts: {
}
export function is_enter_event(event: JQuery.KeyDownEvent): boolean {
- return event.key === "Enter";
+ // In addition to checking whether the key pressed was an Enter
+ // key, we need to check whether the keypress was part of an IME
+ // composing session, such as selecting a character using a
+ // phonetic input method like ZhuYin in a character-based
+ // language. See #22062 for details. Further reading:
+ // https://developer.mozilla.org/en-US/docs/Glossary/Input_method_editor
+ const isComposing: boolean = event.originalEvent?.isComposing || false;
+ return !isComposing && event.key === "Enter";
}