1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:47:34 +00:00

LibWeb: Remove unecessary dependence on Window from UIEvents classes

These classes only needed Window to get at its realm. Pass a realm
directly to construct UIEvents classes.
This commit is contained in:
Andrew Kaster 2022-09-25 18:06:11 -06:00 committed by Linus Groh
parent d0efc7734a
commit 6a10352712
10 changed files with 70 additions and 57 deletions

View file

@ -1178,7 +1178,6 @@ JS::NonnullGCPtr<Range> Document::create_range()
WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(String const& interface) WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(String const& interface)
{ {
auto& realm = this->realm(); auto& realm = this->realm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
// NOTE: This is named event here, since we do step 5 and 6 as soon as possible for each case. // NOTE: This is named event here, since we do step 5 and 6 as soon as possible for each case.
// 1. Let constructor be null. // 1. Let constructor be null.
@ -1202,17 +1201,17 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(String const
} else if (interface_lowercase.is_one_of("event", "events")) { } else if (interface_lowercase.is_one_of("event", "events")) {
event = Event::create(realm, ""); event = Event::create(realm, "");
} else if (interface_lowercase == "focusevent") { } else if (interface_lowercase == "focusevent") {
event = UIEvents::FocusEvent::create(window, ""); event = UIEvents::FocusEvent::create(realm, "");
} else if (interface_lowercase == "hashchangeevent") { } else if (interface_lowercase == "hashchangeevent") {
event = Event::create(realm, ""); // FIXME: Create HashChangeEvent event = Event::create(realm, ""); // FIXME: Create HashChangeEvent
} else if (interface_lowercase == "htmlevents") { } else if (interface_lowercase == "htmlevents") {
event = Event::create(realm, ""); event = Event::create(realm, "");
} else if (interface_lowercase == "keyboardevent") { } else if (interface_lowercase == "keyboardevent") {
event = UIEvents::KeyboardEvent::create(window, ""); event = UIEvents::KeyboardEvent::create(realm, "");
} else if (interface_lowercase == "messageevent") { } else if (interface_lowercase == "messageevent") {
event = HTML::MessageEvent::create(realm, ""); event = HTML::MessageEvent::create(realm, "");
} else if (interface_lowercase.is_one_of("mouseevent", "mouseevents")) { } else if (interface_lowercase.is_one_of("mouseevent", "mouseevents")) {
event = UIEvents::MouseEvent::create(window, ""); event = UIEvents::MouseEvent::create(realm, "");
} else if (interface_lowercase == "storageevent") { } else if (interface_lowercase == "storageevent") {
event = Event::create(realm, ""); // FIXME: Create StorageEvent event = Event::create(realm, ""); // FIXME: Create StorageEvent
} else if (interface_lowercase == "svgevents") { } else if (interface_lowercase == "svgevents") {
@ -1222,7 +1221,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(String const
} else if (interface_lowercase == "touchevent") { } else if (interface_lowercase == "touchevent") {
event = Event::create(realm, ""); // FIXME: Create TouchEvent event = Event::create(realm, ""); // FIXME: Create TouchEvent
} else if (interface_lowercase.is_one_of("uievent", "uievents")) { } else if (interface_lowercase.is_one_of("uievent", "uievents")) {
event = UIEvents::UIEvent::create(window, ""); event = UIEvents::UIEvent::create(realm, "");
} }
// 3. If constructor is null, then throw a "NotSupportedError" DOMException. // 3. If constructor is null, then throw a "NotSupportedError" DOMException.

View file

@ -272,7 +272,7 @@ static void run_focus_update_steps(Vector<JS::Handle<DOM::Node>> old_chain, Vect
// with related blur target as the related target. // with related blur target as the related target.
if (blur_event_target) { if (blur_event_target) {
// FIXME: Implement the "fire a focus event" spec operation. // FIXME: Implement the "fire a focus event" spec operation.
auto blur_event = UIEvents::FocusEvent::create(blur_event_target->global_object(), HTML::EventNames::blur); auto blur_event = UIEvents::FocusEvent::create(blur_event_target->realm(), HTML::EventNames::blur);
blur_event->set_related_target(related_blur_target); blur_event->set_related_target(related_blur_target);
blur_event_target->dispatch_event(*blur_event); blur_event_target->dispatch_event(*blur_event);
} }
@ -315,7 +315,7 @@ static void run_focus_update_steps(Vector<JS::Handle<DOM::Node>> old_chain, Vect
// with related focus target as the related target. // with related focus target as the related target.
if (focus_event_target) { if (focus_event_target) {
// FIXME: Implement the "fire a focus event" spec operation. // FIXME: Implement the "fire a focus event" spec operation.
auto focus_event = UIEvents::FocusEvent::create(focus_event_target->global_object(), HTML::EventNames::focus); auto focus_event = UIEvents::FocusEvent::create(focus_event_target->realm(), HTML::EventNames::focus);
focus_event->set_related_target(related_focus_target); focus_event->set_related_target(related_focus_target);
focus_event_target->dispatch_event(*focus_event); focus_event_target->dispatch_event(*focus_event);
} }
@ -436,7 +436,7 @@ bool HTMLElement::fire_a_synthetic_pointer_event(FlyString const& type, DOM::Ele
// 1. Let event be the result of creating an event using PointerEvent. // 1. Let event be the result of creating an event using PointerEvent.
// 2. Initialize event's type attribute to e. // 2. Initialize event's type attribute to e.
// FIXME: Actually create a PointerEvent! // FIXME: Actually create a PointerEvent!
auto event = UIEvents::MouseEvent::create(window(), type); auto event = UIEvents::MouseEvent::create(realm(), type);
// 3. Initialize event's bubbles and cancelable attributes to true. // 3. Initialize event's bubbles and cancelable attributes to true.
event->set_bubbles(true); event->set_bubbles(true);

View file

@ -4,20 +4,20 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibWeb/HTML/Window.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/UIEvents/FocusEvent.h> #include <LibWeb/UIEvents/FocusEvent.h>
namespace Web::UIEvents { namespace Web::UIEvents {
FocusEvent* FocusEvent::create_with_global_object(HTML::Window& window_object, FlyString const& event_name, FocusEventInit const& event_init) FocusEvent* FocusEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, FocusEventInit const& event_init)
{ {
return window_object.heap().allocate<FocusEvent>(window_object.realm(), window_object, event_name, event_init); return realm.heap().allocate<FocusEvent>(realm, realm, event_name, event_init);
} }
FocusEvent::FocusEvent(HTML::Window& window_object, FlyString const& event_name, FocusEventInit const& event_init) FocusEvent::FocusEvent(JS::Realm& realm, FlyString const& event_name, FocusEventInit const& event_init)
: UIEvent(window_object, event_name) : UIEvent(realm, event_name)
{ {
set_prototype(&window_object.cached_web_prototype("FocusEvent")); set_prototype(&Bindings::cached_web_prototype(realm, "FocusEvent"));
set_related_target(const_cast<DOM::EventTarget*>(event_init.related_target.ptr())); set_related_target(const_cast<DOM::EventTarget*>(event_init.related_target.ptr()));
} }

View file

@ -18,10 +18,12 @@ class FocusEvent final : public UIEvent {
WEB_PLATFORM_OBJECT(FocusEvent, UIEvent); WEB_PLATFORM_OBJECT(FocusEvent, UIEvent);
public: public:
static FocusEvent* create_with_global_object(HTML::Window&, FlyString const& event_name, FocusEventInit const& event_init); static FocusEvent* construct_impl(JS::Realm&, FlyString const& event_name, FocusEventInit const& event_init);
FocusEvent(HTML::Window&, FlyString const& event_name, FocusEventInit const&);
virtual ~FocusEvent() override; virtual ~FocusEvent() override;
private:
FocusEvent(JS::Realm&, FlyString const& event_name, FocusEventInit const&);
}; };
} }

View file

@ -5,7 +5,7 @@
*/ */
#include <AK/CharacterTypes.h> #include <AK/CharacterTypes.h>
#include <LibWeb/HTML/Window.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/UIEvents/KeyboardEvent.h> #include <LibWeb/UIEvents/KeyboardEvent.h>
namespace Web::UIEvents { namespace Web::UIEvents {
@ -66,7 +66,7 @@ static unsigned long determine_key_code(KeyCode platform_key, u32 code_point)
return platform_key; return platform_key;
} }
KeyboardEvent* KeyboardEvent::create_from_platform_event(HTML::Window& window_object, FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point) KeyboardEvent* KeyboardEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
{ {
// FIXME: Figure out what these should actually contain. // FIXME: Figure out what these should actually contain.
String event_key = key_code_to_string(platform_key); String event_key = key_code_to_string(platform_key);
@ -88,7 +88,12 @@ KeyboardEvent* KeyboardEvent::create_from_platform_event(HTML::Window& window_ob
event_init.bubbles = true; event_init.bubbles = true;
event_init.cancelable = true; event_init.cancelable = true;
event_init.composed = true; event_init.composed = true;
return KeyboardEvent::create(window_object, event_name, event_init); return KeyboardEvent::create(realm, event_name, event_init);
}
KeyboardEvent* KeyboardEvent::create_from_platform_event(HTML::Window& window, FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
{
return create_from_platform_event(window.realm(), event_name, platform_key, modifiers, code_point);
} }
bool KeyboardEvent::get_modifier_state(String const& key_arg) bool KeyboardEvent::get_modifier_state(String const& key_arg)
@ -104,18 +109,18 @@ bool KeyboardEvent::get_modifier_state(String const& key_arg)
return false; return false;
} }
KeyboardEvent* KeyboardEvent::create(HTML::Window& window_object, FlyString const& event_name, KeyboardEventInit const& event_init) KeyboardEvent* KeyboardEvent::create(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
{ {
return window_object.heap().allocate<KeyboardEvent>(window_object.realm(), window_object, event_name, event_init); return realm.heap().allocate<KeyboardEvent>(realm, realm, event_name, event_init);
} }
KeyboardEvent* KeyboardEvent::create_with_global_object(HTML::Window& window_object, FlyString const& event_name, KeyboardEventInit const& event_init) KeyboardEvent* KeyboardEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
{ {
return create(window_object, event_name, event_init); return create(realm, event_name, event_init);
} }
KeyboardEvent::KeyboardEvent(HTML::Window& window_object, FlyString const& event_name, KeyboardEventInit const& event_init) KeyboardEvent::KeyboardEvent(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init)
: UIEvent(window_object, event_name, event_init) : UIEvent(realm, event_name, event_init)
, m_key(event_init.key) , m_key(event_init.key)
, m_code(event_init.code) , m_code(event_init.code)
, m_location(event_init.location) , m_location(event_init.location)
@ -128,7 +133,7 @@ KeyboardEvent::KeyboardEvent(HTML::Window& window_object, FlyString const& event
, m_key_code(event_init.key_code) , m_key_code(event_init.key_code)
, m_char_code(event_init.char_code) , m_char_code(event_init.char_code)
{ {
set_prototype(&window_object.cached_web_prototype("KeyboardEvent")); set_prototype(&Bindings::cached_web_prototype(realm, "KeyboardEvent"));
} }
KeyboardEvent::~KeyboardEvent() = default; KeyboardEvent::~KeyboardEvent() = default;

View file

@ -28,12 +28,11 @@ class KeyboardEvent final : public UIEvent {
WEB_PLATFORM_OBJECT(KeyboardEvent, UIEvent); WEB_PLATFORM_OBJECT(KeyboardEvent, UIEvent);
public: public:
static KeyboardEvent* create(HTML::Window&, FlyString const& event_name, KeyboardEventInit const& event_init = {}); static KeyboardEvent* create(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& event_init = {});
static KeyboardEvent* create_with_global_object(HTML::Window&, FlyString const& event_name, KeyboardEventInit const& event_init); static KeyboardEvent* construct_impl(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& event_init);
static KeyboardEvent* create_from_platform_event(JS::Realm&, FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point);
static KeyboardEvent* create_from_platform_event(HTML::Window&, FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point); static KeyboardEvent* create_from_platform_event(HTML::Window&, FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point);
KeyboardEvent(HTML::Window&, FlyString const& event_name, KeyboardEventInit const& event_init);
virtual ~KeyboardEvent() override; virtual ~KeyboardEvent() override;
u32 key_code() const { return m_key_code; } u32 key_code() const { return m_key_code; }
@ -56,6 +55,8 @@ public:
virtual u32 which() const override { return m_key_code; } virtual u32 which() const override { return m_key_code; }
private: private:
KeyboardEvent(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& event_init);
String m_key; String m_key;
String m_code; String m_code;
u32 m_location { 0 }; u32 m_location { 0 };

View file

@ -6,22 +6,22 @@
*/ */
#include <LibGUI/Event.h> #include <LibGUI/Event.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/EventNames.h> #include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/UIEvents/EventNames.h> #include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/MouseEvent.h> #include <LibWeb/UIEvents/MouseEvent.h>
namespace Web::UIEvents { namespace Web::UIEvents {
MouseEvent::MouseEvent(HTML::Window& window_object, FlyString const& event_name, MouseEventInit const& event_init) MouseEvent::MouseEvent(JS::Realm& realm, FlyString const& event_name, MouseEventInit const& event_init)
: UIEvent(window_object, event_name, event_init) : UIEvent(realm, event_name, event_init)
, m_offset_x(event_init.offset_x) , m_offset_x(event_init.offset_x)
, m_offset_y(event_init.offset_y) , m_offset_y(event_init.offset_y)
, m_client_x(event_init.client_x) , m_client_x(event_init.client_x)
, m_client_y(event_init.client_y) , m_client_y(event_init.client_y)
, m_button(event_init.button) , m_button(event_init.button)
{ {
set_prototype(&window_object.cached_web_prototype("MouseEvent")); set_prototype(&Bindings::cached_web_prototype(realm, "MouseEvent"));
set_event_characteristics(); set_event_characteristics();
} }
@ -46,12 +46,12 @@ static i16 determine_button(unsigned mouse_button)
} }
} }
MouseEvent* MouseEvent::create(HTML::Window& window_object, FlyString const& event_name, MouseEventInit const& event_init) MouseEvent* MouseEvent::create(JS::Realm& realm, FlyString const& event_name, MouseEventInit const& event_init)
{ {
return window_object.heap().allocate<MouseEvent>(window_object.realm(), window_object, event_name, event_init); return realm.heap().allocate<MouseEvent>(realm, realm, event_name, event_init);
} }
MouseEvent* MouseEvent::create_from_platform_event(HTML::Window& window_object, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button) MouseEvent* MouseEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button)
{ {
MouseEventInit event_init {}; MouseEventInit event_init {};
event_init.offset_x = offset_x; event_init.offset_x = offset_x;
@ -59,7 +59,12 @@ MouseEvent* MouseEvent::create_from_platform_event(HTML::Window& window_object,
event_init.client_x = client_x; event_init.client_x = client_x;
event_init.client_y = client_y; event_init.client_y = client_y;
event_init.button = determine_button(mouse_button); event_init.button = determine_button(mouse_button);
return MouseEvent::create(window_object, event_name, event_init); return MouseEvent::create(realm, event_name, event_init);
}
MouseEvent* MouseEvent::create_from_platform_event(HTML::Window& window, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button)
{
return create_from_platform_event(window.realm(), event_name, offset_x, offset_y, client_x, client_y, mouse_button);
} }
void MouseEvent::set_event_characteristics() void MouseEvent::set_event_characteristics()

View file

@ -25,11 +25,10 @@ class MouseEvent final : public UIEvent {
WEB_PLATFORM_OBJECT(MouseEvent, UIEvent); WEB_PLATFORM_OBJECT(MouseEvent, UIEvent);
public: public:
static MouseEvent* create(HTML::Window&, FlyString const& event_name, MouseEventInit const& event_init = {}); static MouseEvent* create(JS::Realm&, FlyString const& event_name, MouseEventInit const& event_init = {});
static MouseEvent* create_from_platform_event(JS::Realm&, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button = 1);
static MouseEvent* create_from_platform_event(HTML::Window&, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button = 1); static MouseEvent* create_from_platform_event(HTML::Window&, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button = 1);
MouseEvent(HTML::Window&, FlyString const& event_name, MouseEventInit const& event_init);
virtual ~MouseEvent() override; virtual ~MouseEvent() override;
double offset_x() const { return m_offset_x; } double offset_x() const { return m_offset_x; }
@ -46,6 +45,8 @@ public:
virtual u32 which() const override { return m_button + 1; } virtual u32 which() const override { return m_button + 1; }
private: private:
MouseEvent(JS::Realm&, FlyString const& event_name, MouseEventInit const& event_init);
void set_event_characteristics(); void set_event_characteristics();
double m_offset_x { 0 }; double m_offset_x { 0 };

View file

@ -4,33 +4,33 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibWeb/HTML/Window.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/UIEvents/UIEvent.h> #include <LibWeb/UIEvents/UIEvent.h>
namespace Web::UIEvents { namespace Web::UIEvents {
UIEvent* UIEvent::create(HTML::Window& window_object, FlyString const& event_name) UIEvent* UIEvent::create(JS::Realm& realm, FlyString const& event_name)
{ {
return window_object.heap().allocate<UIEvent>(window_object.realm(), window_object, event_name); return realm.heap().allocate<UIEvent>(realm, realm, event_name);
} }
UIEvent* UIEvent::create_with_global_object(HTML::Window& window_object, FlyString const& event_name, UIEventInit const& event_init) UIEvent* UIEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, UIEventInit const& event_init)
{ {
return window_object.heap().allocate<UIEvent>(window_object.realm(), window_object, event_name, event_init); return realm.heap().allocate<UIEvent>(realm, realm, event_name, event_init);
} }
UIEvent::UIEvent(HTML::Window& window_object, FlyString const& event_name) UIEvent::UIEvent(JS::Realm& realm, FlyString const& event_name)
: Event(window_object, event_name) : Event(realm, event_name)
{ {
set_prototype(&window_object.cached_web_prototype("UIEvent")); set_prototype(&Bindings::cached_web_prototype(realm, "UIEvent"));
} }
UIEvent::UIEvent(HTML::Window& window_object, FlyString const& event_name, UIEventInit const& event_init) UIEvent::UIEvent(JS::Realm& realm, FlyString const& event_name, UIEventInit const& event_init)
: Event(window_object, event_name, event_init) : Event(realm, event_name, event_init)
, m_view(event_init.view) , m_view(event_init.view)
, m_detail(event_init.detail) , m_detail(event_init.detail)
{ {
set_prototype(&window_object.cached_web_prototype("UIEvent")); set_prototype(&Bindings::cached_web_prototype(realm, "UIEvent"));
} }
UIEvent::~UIEvent() = default; UIEvent::~UIEvent() = default;

View file

@ -21,11 +21,8 @@ class UIEvent : public DOM::Event {
WEB_PLATFORM_OBJECT(UIEvent, DOM::Event); WEB_PLATFORM_OBJECT(UIEvent, DOM::Event);
public: public:
static UIEvent* create(HTML::Window&, FlyString const& type); static UIEvent* create(JS::Realm&, FlyString const& type);
static UIEvent* create_with_global_object(HTML::Window&, FlyString const& event_name, UIEventInit const& event_init); static UIEvent* construct_impl(JS::Realm&, FlyString const& event_name, UIEventInit const& event_init);
UIEvent(HTML::Window&, FlyString const& event_name);
UIEvent(HTML::Window&, FlyString const& event_name, UIEventInit const& event_init);
virtual ~UIEvent() override; virtual ~UIEvent() override;
@ -41,6 +38,9 @@ public:
} }
protected: protected:
UIEvent(JS::Realm&, FlyString const& event_name);
UIEvent(JS::Realm&, FlyString const& event_name, UIEventInit const& event_init);
virtual void visit_edges(Cell::Visitor&) override; virtual void visit_edges(Cell::Visitor&) override;
JS::GCPtr<HTML::Window> m_view; JS::GCPtr<HTML::Window> m_view;