From a11e5055c7f225da3b96571e892042868fd2af18 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 9 Oct 2024 15:45:07 -0400 Subject: [PATCH] LibWeb: Allow multi-byte code point events to have their key field set Multi-byte code point presses do not have a UIEvents::KeyCode value, so we would previously set the event's key field to "Unidentified". --- .../expected/UIEvents/KeyEvent-non-ascii.txt | 6 ++++++ .../input/UIEvents/KeyEvent-non-ascii.html | 18 ++++++++++++++++++ .../LibWeb/UIEvents/KeyboardEvent.cpp | 18 ++++++++++-------- 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/UIEvents/KeyEvent-non-ascii.txt create mode 100644 Tests/LibWeb/Text/input/UIEvents/KeyEvent-non-ascii.html diff --git a/Tests/LibWeb/Text/expected/UIEvents/KeyEvent-non-ascii.txt b/Tests/LibWeb/Text/expected/UIEvents/KeyEvent-non-ascii.txt new file mode 100644 index 00000000000..72ec9c1971a --- /dev/null +++ b/Tests/LibWeb/Text/expected/UIEvents/KeyEvent-non-ascii.txt @@ -0,0 +1,6 @@ +keydown à +keydown á +keydown â +keydown ã +keydown ä +keydown å diff --git a/Tests/LibWeb/Text/input/UIEvents/KeyEvent-non-ascii.html b/Tests/LibWeb/Text/input/UIEvents/KeyEvent-non-ascii.html new file mode 100644 index 00000000000..69bbf48e136 --- /dev/null +++ b/Tests/LibWeb/Text/input/UIEvents/KeyEvent-non-ascii.html @@ -0,0 +1,18 @@ + + + diff --git a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp index a8ff1cbdc56..98e49be8499 100644 --- a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp @@ -272,18 +272,22 @@ static ErrorOr get_event_key(KeyCode platform_key, u32 code_point) { // 1. Let key be a DOMString initially set to "Unidentified". // NOTE: We return "Unidentified" at the end to avoid needlessly allocating it here. - Optional key; // 2. If there exists an appropriate named key attribute value for this key event, then - if (auto named_key = TRY(get_event_named_key(platform_key)); named_key.has_value()) { - // 1. Set key to that named key attribute value. - key = named_key.release_value(); + // AD-HOC: Key_Invalid would be interpreted as "Unidentified" here. But we also use Key_Invalid for key presses that + // are not on a standard US keyboard. If such a key would generate a valid key string below, let's allow that + // to happen; otherwise, we will still return "Unidentified" at the end. + if (platform_key != KeyCode::Key_Invalid) { + if (auto named_key = TRY(get_event_named_key(platform_key)); named_key.has_value()) { + // 1. Set key to that named key attribute value. + return named_key.release_value(); + } } // 3. Else, if the key event generates a valid key string, then - else if (auto key_string = TRY(get_event_key_string(code_point)); key_string.has_value()) { + if (auto key_string = TRY(get_event_key_string(code_point)); key_string.has_value()) { // 1. Set key to that key string value. - key = key_string.release_value(); + return key_string.release_value(); } // FIXME: 4. Else, if the key event has any modifier keys other than glyph modifier keys, then @@ -291,8 +295,6 @@ static ErrorOr get_event_key(KeyCode platform_key, u32 code_point) // modifer keys removed except for glyph modifier keys. // 5. Return key as the key attribute value for this key event. - if (key.has_value()) - return key.release_value(); return "Unidentified"_string; }