1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 18:25:08 +00:00

LibWeb: Add a DOM Event class (instead of events being simple strings)

This patch adds the Event base class, along with a MouseEvent subclass.
We now dispatch MouseEvent objects for mousedown, mouseup and mousemove
and these objects have the .offsetX and .offsetY properties.

Both of those properties are hard-coded at the moment. This will be
fixed in the next patch. :^)
This commit is contained in:
Andreas Kling 2020-03-21 18:17:18 +01:00
parent b196665131
commit 4dde36844b
15 changed files with 311 additions and 10 deletions

View file

@ -29,9 +29,11 @@
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/ScriptFunction.h>
#include <LibWeb/Bindings/EventWrapper.h>
#include <LibWeb/Bindings/NodeWrapper.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/HTMLAnchorElement.h>
#include <LibWeb/DOM/Node.h>
@ -123,21 +125,22 @@ bool Node::is_link() const
return enclosing_link->has_attribute("href");
}
void Node::dispatch_event(String event_name)
void Node::dispatch_event(NonnullRefPtr<Event> event)
{
for (auto& listener : listeners()) {
if (listener.event_name == event_name) {
if (listener.event_name == event->name()) {
auto* function = const_cast<EventListener&>(*listener.listener).function();
static_cast<const JS::ScriptFunction*>(function)->body().dump(0);
auto* this_value = wrap(function->heap(), *this);
dbg() << "calling event listener with this=" << this_value;
document().interpreter().call(function, this_value, {});
auto* event_wrapper = wrap(function->heap(), *event);
document().interpreter().call(function, this_value, { event_wrapper });
}
}
// FIXME: This is a hack. We should follow the real rules of event bubbling.
if (parent())
parent()->dispatch_event(move(event_name));
parent()->dispatch_event(move(event));
}
}