diff options
author | Andrew Kaster <akaster@serenityos.org> | 2022-09-25 18:06:11 -0600 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-10-01 21:05:32 +0100 |
commit | 6a103527122592501c9f86283a2abf70bd828d56 (patch) | |
tree | 34e8bb455d10dfd62877f8db81e838430ace0af5 | |
parent | d0efc7734aba153934a565fad240e3a586de5afb (diff) | |
download | serenity-6a103527122592501c9f86283a2abf70bd828d56.zip |
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.
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLElement.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/FocusEvent.cpp | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/FocusEvent.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp | 25 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp | 21 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/MouseEvent.h | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/UIEvent.cpp | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/UIEvent.h | 10 |
10 files changed, 70 insertions, 57 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 5c0e711187..c9cd9801fc 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -1178,7 +1178,6 @@ JS::NonnullGCPtr<Range> Document::create_range() WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(String const& interface) { 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. // 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")) { event = Event::create(realm, ""); } else if (interface_lowercase == "focusevent") { - event = UIEvents::FocusEvent::create(window, ""); + event = UIEvents::FocusEvent::create(realm, ""); } else if (interface_lowercase == "hashchangeevent") { event = Event::create(realm, ""); // FIXME: Create HashChangeEvent } else if (interface_lowercase == "htmlevents") { event = Event::create(realm, ""); } else if (interface_lowercase == "keyboardevent") { - event = UIEvents::KeyboardEvent::create(window, ""); + event = UIEvents::KeyboardEvent::create(realm, ""); } else if (interface_lowercase == "messageevent") { event = HTML::MessageEvent::create(realm, ""); } else if (interface_lowercase.is_one_of("mouseevent", "mouseevents")) { - event = UIEvents::MouseEvent::create(window, ""); + event = UIEvents::MouseEvent::create(realm, ""); } else if (interface_lowercase == "storageevent") { event = Event::create(realm, ""); // FIXME: Create StorageEvent } else if (interface_lowercase == "svgevents") { @@ -1222,7 +1221,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(String const } else if (interface_lowercase == "touchevent") { event = Event::create(realm, ""); // FIXME: Create TouchEvent } 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. diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp index 52389e18dd..f0a877a84b 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp @@ -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. if (blur_event_target) { // 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_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. if (focus_event_target) { // 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_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. // 2. Initialize event's type attribute to e. // 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. event->set_bubbles(true); diff --git a/Userland/Libraries/LibWeb/UIEvents/FocusEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/FocusEvent.cpp index ac4d321fb3..4fc2aeefa9 100644 --- a/Userland/Libraries/LibWeb/UIEvents/FocusEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/FocusEvent.cpp @@ -4,20 +4,20 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include <LibWeb/HTML/Window.h> +#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/UIEvents/FocusEvent.h> 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) - : UIEvent(window_object, event_name) +FocusEvent::FocusEvent(JS::Realm& realm, FlyString const& event_name, FocusEventInit const& event_init) + : 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())); } diff --git a/Userland/Libraries/LibWeb/UIEvents/FocusEvent.h b/Userland/Libraries/LibWeb/UIEvents/FocusEvent.h index a3ede95b23..1df41baecb 100644 --- a/Userland/Libraries/LibWeb/UIEvents/FocusEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/FocusEvent.h @@ -18,10 +18,12 @@ class FocusEvent final : public UIEvent { WEB_PLATFORM_OBJECT(FocusEvent, UIEvent); 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; + +private: + FocusEvent(JS::Realm&, FlyString const& event_name, FocusEventInit const&); }; } diff --git a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp index 853a4bd55a..eb8fcbedf3 100644 --- a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp @@ -5,7 +5,7 @@ */ #include <AK/CharacterTypes.h> -#include <LibWeb/HTML/Window.h> +#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/UIEvents/KeyboardEvent.h> namespace Web::UIEvents { @@ -66,7 +66,7 @@ static unsigned long determine_key_code(KeyCode platform_key, u32 code_point) 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. 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.cancelable = 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) @@ -104,18 +109,18 @@ bool KeyboardEvent::get_modifier_state(String const& key_arg) 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) - : UIEvent(window_object, event_name, event_init) +KeyboardEvent::KeyboardEvent(JS::Realm& realm, FlyString const& event_name, KeyboardEventInit const& event_init) + : UIEvent(realm, event_name, event_init) , m_key(event_init.key) , m_code(event_init.code) , 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_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; diff --git a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h index 73442d5660..22eddfc53d 100644 --- a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h @@ -28,12 +28,11 @@ class KeyboardEvent final : public UIEvent { WEB_PLATFORM_OBJECT(KeyboardEvent, UIEvent); public: - static KeyboardEvent* create(HTML::Window&, 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* create(JS::Realm&, 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); - KeyboardEvent(HTML::Window&, FlyString const& event_name, KeyboardEventInit const& event_init); - virtual ~KeyboardEvent() override; u32 key_code() const { return m_key_code; } @@ -56,6 +55,8 @@ public: virtual u32 which() const override { return m_key_code; } private: + KeyboardEvent(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& event_init); + String m_key; String m_code; u32 m_location { 0 }; diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp index 047ff967f8..41ec12a6f6 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp @@ -6,22 +6,22 @@ */ #include <LibGUI/Event.h> +#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/HTML/EventNames.h> -#include <LibWeb/HTML/Window.h> #include <LibWeb/UIEvents/EventNames.h> #include <LibWeb/UIEvents/MouseEvent.h> namespace Web::UIEvents { -MouseEvent::MouseEvent(HTML::Window& window_object, FlyString const& event_name, MouseEventInit const& event_init) - : UIEvent(window_object, event_name, event_init) +MouseEvent::MouseEvent(JS::Realm& realm, FlyString const& event_name, MouseEventInit const& event_init) + : UIEvent(realm, event_name, event_init) , m_offset_x(event_init.offset_x) , m_offset_y(event_init.offset_y) , m_client_x(event_init.client_x) , m_client_y(event_init.client_y) , m_button(event_init.button) { - set_prototype(&window_object.cached_web_prototype("MouseEvent")); + set_prototype(&Bindings::cached_web_prototype(realm, "MouseEvent")); 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 {}; 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_y = client_y; 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() diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h index 7084753e5d..7a4a623a0d 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h @@ -25,11 +25,10 @@ class MouseEvent final : public UIEvent { WEB_PLATFORM_OBJECT(MouseEvent, UIEvent); 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); - MouseEvent(HTML::Window&, FlyString const& event_name, MouseEventInit const& event_init); - virtual ~MouseEvent() override; double offset_x() const { return m_offset_x; } @@ -46,6 +45,8 @@ public: virtual u32 which() const override { return m_button + 1; } private: + MouseEvent(JS::Realm&, FlyString const& event_name, MouseEventInit const& event_init); + void set_event_characteristics(); double m_offset_x { 0 }; diff --git a/Userland/Libraries/LibWeb/UIEvents/UIEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/UIEvent.cpp index 15ede7319d..e7488a3b5b 100644 --- a/Userland/Libraries/LibWeb/UIEvents/UIEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/UIEvent.cpp @@ -4,33 +4,33 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include <LibWeb/HTML/Window.h> +#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/UIEvents/UIEvent.h> 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) - : Event(window_object, event_name) +UIEvent::UIEvent(JS::Realm& realm, FlyString const& 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) - : Event(window_object, event_name, event_init) +UIEvent::UIEvent(JS::Realm& realm, FlyString const& event_name, UIEventInit const& event_init) + : Event(realm, event_name, event_init) , m_view(event_init.view) , m_detail(event_init.detail) { - set_prototype(&window_object.cached_web_prototype("UIEvent")); + set_prototype(&Bindings::cached_web_prototype(realm, "UIEvent")); } UIEvent::~UIEvent() = default; diff --git a/Userland/Libraries/LibWeb/UIEvents/UIEvent.h b/Userland/Libraries/LibWeb/UIEvents/UIEvent.h index 8019452f12..9678696977 100644 --- a/Userland/Libraries/LibWeb/UIEvents/UIEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/UIEvent.h @@ -21,11 +21,8 @@ class UIEvent : public DOM::Event { WEB_PLATFORM_OBJECT(UIEvent, DOM::Event); public: - static UIEvent* create(HTML::Window&, FlyString const& type); - static UIEvent* create_with_global_object(HTML::Window&, 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); + static UIEvent* create(JS::Realm&, FlyString const& type); + static UIEvent* construct_impl(JS::Realm&, FlyString const& event_name, UIEventInit const& event_init); virtual ~UIEvent() override; @@ -41,6 +38,9 @@ public: } 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; JS::GCPtr<HTML::Window> m_view; |