1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

LibWeb: Implement KeyboardEvent.charCode according to spec

It should be 0 for keydown/keyup events.
This commit is contained in:
Timothy Flynn 2024-10-21 16:30:10 -04:00 committed by Jelle Raaijmakers
parent 1bbe8309d4
commit c96c5e45ff
Notes: github-actions[bot] 2024-10-22 10:49:53 +00:00
3 changed files with 26 additions and 9 deletions

View file

@ -1,5 +1,5 @@
keydown A
keypress A
keydown Shift
keydown B
keypress B
keydown key=A charCode=0
keypress key=A charCode=65
keydown key=Shift charCode=0
keydown key=B charCode=0
keypress key=B charCode=66

View file

@ -5,10 +5,10 @@
let input = document.getElementById("input");
input.addEventListener("keydown", e => {
println(`keydown ${e.key}`);
println(`keydown key=${e.key} charCode=${e.charCode}`);
});
input.addEventListener("keypress", e => {
println(`keypress ${e.key}`);
println(`keypress key=${e.key} charCode=${e.charCode}`);
});
internals.sendText(input, "A");

View file

@ -8,6 +8,7 @@
#include <LibUnicode/CharacterTypes.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/KeyboardEventPrototype.h>
#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/KeyboardEvent.h>
namespace Web::UIEvents {
@ -137,6 +138,20 @@ static unsigned long determine_key_code(KeyCode platform_key, u32 code_point)
return platform_key;
}
// https://www.w3.org/TR/uievents/#dom-keyboardevent-charcode
static u32 determine_char_code(FlyString const& event_name, u32 code_point)
{
// charCode holds a character value, for keypress events which generate character input. The value is the Unicode
// reference number (code point) of that character (e.g. event.charCode = event.key.charCodeAt(0) for printable
// characters). For keydown or keyup events, the value of charCode is 0.
if (event_name == UIEvents::EventNames::keypress) {
if (Unicode::code_point_is_printable(code_point))
return code_point;
}
return 0;
}
// 3. Named key Attribute Values, https://www.w3.org/TR/uievents-key/#named-key-attribute-values
static ErrorOr<Optional<String>> get_event_named_key(KeyCode platform_key)
{
@ -657,8 +672,9 @@ JS::NonnullGCPtr<KeyboardEvent> KeyboardEvent::create_from_platform_event(JS::Re
{
auto event_key = MUST(get_event_key(platform_key, code_point));
auto event_code = MUST(get_event_code(platform_key, modifiers));
auto key_code = determine_key_code(platform_key, code_point);
auto char_code = determine_char_code(event_name, code_point);
KeyboardEventInit event_init {};
event_init.key = move(event_key);
event_init.code = move(event_code);
@ -670,10 +686,11 @@ JS::NonnullGCPtr<KeyboardEvent> KeyboardEvent::create_from_platform_event(JS::Re
event_init.repeat = false;
event_init.is_composing = false;
event_init.key_code = key_code;
event_init.char_code = code_point;
event_init.char_code = char_code;
event_init.bubbles = true;
event_init.cancelable = true;
event_init.composed = true;
auto event = KeyboardEvent::create(realm, event_name, event_init);
event->set_is_trusted(true);
return event;