1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:27:45 +00:00

LibWeb: Make the Window object "inherit" from EventTarget :^)

Since Web::Bindings::WindowObject inherits from JS::GlobalObject, it
cannot also inherit from Web::Bindings::EventTargetWrapper.

However, that's not actually necessary. Instead, we simply set the
Window object's prototype to the EventTargetPrototype, and add a little
extra branch in the impl_from() function that turns the JS "this" value
into a DOM::EventTarget*.

With this, you can now call window.addEventListener()! Very cool :^)

Fixes #4758.
This commit is contained in:
Andreas Kling 2021-01-18 12:15:02 +01:00
parent fd83918476
commit 0639e77898
13 changed files with 26 additions and 14 deletions

View file

@ -51,7 +51,7 @@ public:
void remove_from_event_listener_list(NonnullRefPtr<EventListener>);
virtual bool dispatch_event(NonnullRefPtr<Event>) = 0;
virtual Bindings::EventTargetWrapper* create_wrapper(JS::GlobalObject&) = 0;
virtual JS::Object* create_wrapper(JS::GlobalObject&) = 0;
Bindings::ScriptExecutionContext* script_execution_context() { return m_script_execution_context; }
virtual EventTarget* get_parent(const Event&) { return nullptr; }

View file

@ -213,7 +213,7 @@ bool Node::is_editable() const
return parent() && parent()->is_editable();
}
Bindings::EventTargetWrapper* Node::create_wrapper(JS::GlobalObject& global_object)
JS::Object* Node::create_wrapper(JS::GlobalObject& global_object)
{
return wrap(global_object, *this);
}

View file

@ -61,7 +61,7 @@ public:
virtual void ref_event_target() final { ref(); }
virtual void unref_event_target() final { unref(); }
virtual bool dispatch_event(NonnullRefPtr<Event>) final;
virtual Bindings::EventTargetWrapper* create_wrapper(JS::GlobalObject&) override;
virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
virtual ~Node();

View file

@ -169,9 +169,9 @@ bool Window::dispatch_event(NonnullRefPtr<Event> event)
return EventDispatcher::dispatch(*this, event, true);
}
Bindings::EventTargetWrapper* Window::create_wrapper(JS::GlobalObject&)
JS::Object* Window::create_wrapper(JS::GlobalObject& global_object)
{
ASSERT_NOT_REACHED();
return &global_object;
}
}

View file

@ -50,7 +50,7 @@ public:
virtual void ref_event_target() override { RefCounted::ref(); }
virtual void unref_event_target() override { RefCounted::unref(); }
virtual bool dispatch_event(NonnullRefPtr<Event>) override;
virtual Bindings::EventTargetWrapper* create_wrapper(JS::GlobalObject&) override;
virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
const Document& document() const { return m_document; }
Document& document() { return m_document; }

View file

@ -113,7 +113,7 @@ bool XMLHttpRequest::dispatch_event(NonnullRefPtr<DOM::Event> event)
return DOM::EventDispatcher::dispatch(*this, move(event));
}
Bindings::EventTargetWrapper* XMLHttpRequest::create_wrapper(JS::GlobalObject& global_object)
JS::Object* XMLHttpRequest::create_wrapper(JS::GlobalObject& global_object)
{
return wrap(global_object, *this);
}

View file

@ -66,7 +66,7 @@ private:
virtual void ref_event_target() override { ref(); }
virtual void unref_event_target() override { unref(); }
virtual bool dispatch_event(NonnullRefPtr<DOM::Event>) override;
virtual Bindings::EventTargetWrapper* create_wrapper(JS::GlobalObject&) override;
virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
void set_ready_state(ReadyState);