LibWeb: Port KeyboardEvent to new String

This commit is contained in:
Kenneth Myhra 2023-03-09 21:35:15 +01:00 committed by Andreas Kling
parent e14be3927a
commit 03d6cb88ff
Notes: sideshowbarker 2024-07-16 23:23:26 +09:00
5 changed files with 26 additions and 23 deletions

View file

@ -1293,7 +1293,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(DeprecatedSt
} else if (Infra::is_ascii_case_insensitive_match(interface, "htmlevents"sv)) {
event = TRY(Event::create(realm, ""));
} else if (Infra::is_ascii_case_insensitive_match(interface, "keyboardevent"sv)) {
event = TRY(UIEvents::KeyboardEvent::create(realm, ""));
event = TRY(UIEvents::KeyboardEvent::create(realm, String {}));
} else if (Infra::is_ascii_case_insensitive_match(interface, "messageevent"sv)) {
event = TRY(HTML::MessageEvent::create(realm, String {}));
} else if (Infra::is_ascii_case_insensitive_match(interface, "mouseevent"sv)

View file

@ -684,12 +684,12 @@ bool EventHandler::fire_keyboard_event(DeprecatedFlyString const& event_name, HT
return fire_keyboard_event(event_name, *browsing_context_container.nested_browsing_context(), key, modifiers, code_point);
}
auto event = UIEvents::KeyboardEvent::create_from_platform_event(document->realm(), event_name, key, modifiers, code_point).release_value_but_fixme_should_propagate_errors();
auto event = UIEvents::KeyboardEvent::create_from_platform_event(document->realm(), FlyString::from_deprecated_fly_string(event_name).release_value_but_fixme_should_propagate_errors(), key, modifiers, code_point).release_value_but_fixme_should_propagate_errors();
return !focused_element->dispatch_event(event);
}
// FIXME: De-duplicate this. This is just to prevent wasting a KeyboardEvent allocation when recursing into an (i)frame.
auto event = UIEvents::KeyboardEvent::create_from_platform_event(document->realm(), event_name, key, modifiers, code_point).release_value_but_fixme_should_propagate_errors();
auto event = UIEvents::KeyboardEvent::create_from_platform_event(document->realm(), FlyString::from_deprecated_fly_string(event_name).release_value_but_fixme_should_propagate_errors(), key, modifiers, code_point).release_value_but_fixme_should_propagate_errors();
if (JS::GCPtr<HTML::HTMLElement> body = document->body())
return !body->dispatch_event(event);

View file

@ -66,11 +66,13 @@ static unsigned long determine_key_code(KeyCode platform_key, u32 code_point)
return platform_key;
}
WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::create_from_platform_event(JS::Realm& realm, DeprecatedFlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
{
auto& vm = realm.vm();
// FIXME: Figure out what these should actually contain.
DeprecatedString event_key = key_code_to_string(platform_key);
DeprecatedString event_code = "FIXME";
auto event_key = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(key_code_to_string(platform_key)));
auto event_code = TRY_OR_THROW_OOM(vm, "FIXME"_string);
auto key_code = determine_key_code(platform_key, code_point);
KeyboardEventInit event_init {};
@ -91,7 +93,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::create_from_
return KeyboardEvent::create(realm, event_name, event_init);
}
bool KeyboardEvent::get_modifier_state(DeprecatedString const& key_arg)
bool KeyboardEvent::get_modifier_state(String const& key_arg)
{
if (key_arg == "Alt")
return m_alt_key;
@ -104,18 +106,18 @@ bool KeyboardEvent::get_modifier_state(DeprecatedString const& key_arg)
return false;
}
WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::create(JS::Realm& realm, DeprecatedFlyString const& event_name, KeyboardEventInit const& event_init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::create(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
{
return MUST_OR_THROW_OOM(realm.heap().allocate<KeyboardEvent>(realm, realm, event_name, event_init));
}
WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::construct_impl(JS::Realm& realm, DeprecatedFlyString const& event_name, KeyboardEventInit const& event_init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> KeyboardEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
{
return create(realm, event_name, event_init);
}
KeyboardEvent::KeyboardEvent(JS::Realm& realm, DeprecatedFlyString const& event_name, KeyboardEventInit const& event_init)
: UIEvent(realm, event_name, event_init)
KeyboardEvent::KeyboardEvent(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
: UIEvent(realm, event_name.to_deprecated_fly_string(), event_init)
, m_key(event_init.key)
, m_code(event_init.code)
, m_location(event_init.location)

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/FlyString.h>
#include <AK/TypeCasts.h>
#include <Kernel/API/KeyCode.h>
#include <LibWeb/UIEvents/EventModifier.h>
@ -14,8 +15,8 @@
namespace Web::UIEvents {
struct KeyboardEventInit : public EventModifierInit {
DeprecatedString key { "" };
DeprecatedString code { "" };
String key;
String code;
u32 location { 0 };
bool repeat { false };
bool is_composing { false };
@ -28,17 +29,17 @@ class KeyboardEvent final : public UIEvent {
WEB_PLATFORM_OBJECT(KeyboardEvent, UIEvent);
public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> create(JS::Realm&, DeprecatedFlyString const& event_name, KeyboardEventInit const& event_init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> construct_impl(JS::Realm&, DeprecatedFlyString const& event_name, KeyboardEventInit const& event_init);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> create_from_platform_event(JS::Realm&, DeprecatedFlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> create(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& event_init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> construct_impl(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& event_init);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> create_from_platform_event(JS::Realm&, FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point);
virtual ~KeyboardEvent() override;
u32 key_code() const { return m_key_code; }
u32 char_code() const { return m_char_code; }
DeprecatedString key() const { return m_key; }
DeprecatedString code() const { return m_code; }
String key() const { return m_key; }
String code() const { return m_code; }
u32 location() const { return m_location; }
bool ctrl_key() const { return m_ctrl_key; }
@ -49,17 +50,17 @@ public:
bool repeat() const { return m_repeat; }
bool is_composing() const { return m_is_composing; }
bool get_modifier_state(DeprecatedString const& key_arg);
bool get_modifier_state(String const& key_arg);
virtual u32 which() const override { return m_key_code; }
private:
KeyboardEvent(JS::Realm&, DeprecatedFlyString const& event_name, KeyboardEventInit const& event_init);
KeyboardEvent(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& event_init);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
DeprecatedString m_key;
DeprecatedString m_code;
String m_key;
String m_code;
u32 m_location { 0 };
bool m_ctrl_key { false };
bool m_shift_key { false };

View file

@ -1,6 +1,6 @@
#import <UIEvents/EventModifier.idl>
[Exposed=Window]
[Exposed=Window, UseNewAKString]
interface KeyboardEvent : UIEvent {
constructor(DOMString type, optional KeyboardEventInit eventInitDict = {});