summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2023-05-03 21:10:26 +0100
committerAndreas Kling <kling@serenityos.org>2023-05-04 16:12:40 +0200
commit344785ae3a513f0fc253575b3009f38dc6535e9e (patch)
treef7fa3482aa198812313dd9363d812374aab18e52 /Userland/Libraries/LibWeb/DOM
parent1127bdb65cc0f42ebaa90e9b7508254a4bf87495 (diff)
downloadserenity-344785ae3a513f0fc253575b3009f38dc6535e9e.zip
LibWeb: Implement the EventTarget constructor
This is typically used as `class A extends EventTarget`. It's usage can be found on websites such as https://loadout.tf/ This has the quirk that we don't do set the EventTarget prototype for HTML::Window, as it would cause a null deref on startup. However, given it wasn't doing this before, I don't think it should cause any issues.
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r--Userland/Libraries/LibWeb/DOM/EventTarget.cpp19
-rw-r--r--Userland/Libraries/LibWeb/DOM/EventTarget.h3
-rw-r--r--Userland/Libraries/LibWeb/DOM/EventTarget.idl2
3 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/EventTarget.cpp b/Userland/Libraries/LibWeb/DOM/EventTarget.cpp
index 742e1a70be..ce1c7fa08e 100644
--- a/Userland/Libraries/LibWeb/DOM/EventTarget.cpp
+++ b/Userland/Libraries/LibWeb/DOM/EventTarget.cpp
@@ -42,6 +42,25 @@ EventTarget::EventTarget(JS::Realm& realm)
EventTarget::~EventTarget() = default;
+// https://dom.spec.whatwg.org/#dom-eventtarget-eventtarget
+WebIDL::ExceptionOr<JS::NonnullGCPtr<EventTarget>> EventTarget::construct_impl(JS::Realm& realm)
+{
+ // The new EventTarget() constructor steps are to do nothing.
+ return MUST_OR_THROW_OOM(realm.heap().allocate<EventTarget>(realm, realm));
+}
+
+JS::ThrowCompletionOr<void> EventTarget::initialize(JS::Realm& realm)
+{
+ MUST_OR_THROW_OOM(Base::initialize(realm));
+
+ // FIXME: We can't do this for HTML::Window currently, as this will run when creating the initial global object.
+ // During this time, the ESO is not setup, so it will cause a nullptr dereference in host_defined_intrinsics.
+ if (!is<HTML::Window>(this))
+ set_prototype(&Bindings::ensure_web_prototype<Bindings::EventTargetPrototype>(realm, "EventTarget"));
+
+ return {};
+}
+
void EventTarget::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
diff --git a/Userland/Libraries/LibWeb/DOM/EventTarget.h b/Userland/Libraries/LibWeb/DOM/EventTarget.h
index 5b04d128f9..e380f6f321 100644
--- a/Userland/Libraries/LibWeb/DOM/EventTarget.h
+++ b/Userland/Libraries/LibWeb/DOM/EventTarget.h
@@ -24,6 +24,8 @@ class EventTarget : public Bindings::PlatformObject {
public:
virtual ~EventTarget() override;
+ static WebIDL::ExceptionOr<JS::NonnullGCPtr<EventTarget>> construct_impl(JS::Realm&);
+
virtual bool is_focusable() const { return false; }
void add_event_listener(FlyString const& type, IDLEventListener* callback, Variant<AddEventListenerOptions, bool> const& options);
@@ -62,6 +64,7 @@ protected:
void element_event_handler_attribute_changed(FlyString const& local_name, Optional<String> const& value);
+ virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
private:
diff --git a/Userland/Libraries/LibWeb/DOM/EventTarget.idl b/Userland/Libraries/LibWeb/DOM/EventTarget.idl
index 183a3aff1f..48c82a4d41 100644
--- a/Userland/Libraries/LibWeb/DOM/EventTarget.idl
+++ b/Userland/Libraries/LibWeb/DOM/EventTarget.idl
@@ -4,6 +4,8 @@
[Exposed=*, UseNewAKString]
interface EventTarget {
+ constructor();
+
undefined addEventListener(DOMString type, EventListener? callback, optional (AddEventListenerOptions or boolean) options = {});
undefined removeEventListener(DOMString type, EventListener? callback, optional (EventListenerOptions or boolean) options = {});