LibWeb: Move DOM event dispatch to its own class

For now, the new DOM::EventDispatcher is very simple, it just iterates
over the set of listeners on an EventTarget and invokes the callbacks
as it goes.

This simplifies EventTarget subclasses since they no longer have to
implement the callback mechanism themselves.
This commit is contained in:
Andreas Kling 2020-09-06 14:28:41 +02:00
parent 521475dc01
commit 8a6a9a8fb6
Notes: sideshowbarker 2024-07-19 02:53:32 +09:00
10 changed files with 185 additions and 30 deletions

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibWeb/Bindings/EventTargetWrapperFactory.h>
#include <LibWeb/DOM/EventTarget.h>
namespace Web::Bindings {
EventTargetWrapper* wrap(JS::GlobalObject& global_object, DOM::EventTarget& target)
{
return target.create_wrapper(global_object);
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibJS/Forward.h>
#include <LibWeb/Forward.h>
namespace Web::Bindings {
EventTargetWrapper* wrap(JS::GlobalObject&, DOM::EventTarget&);
}

View file

@ -1,6 +1,7 @@
set(SOURCES
Bindings/EventListenerWrapper.cpp
Bindings/EventWrapperFactory.cpp
Bindings/EventTargetWrapperFactory.cpp
Bindings/LocationObject.cpp
Bindings/NavigatorObject.cpp
Bindings/NodeWrapperFactory.cpp
@ -31,6 +32,7 @@ set(SOURCES
DOM/DocumentType.cpp
DOM/Element.cpp
DOM/ElementFactory.cpp
DOM/EventDispatcher.cpp
DOM/EventListener.cpp
DOM/EventTarget.cpp
DOM/Node.cpp

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibJS/Runtime/Function.h>
#include <LibWeb/Bindings/EventTargetWrapper.h>
#include <LibWeb/Bindings/EventTargetWrapperFactory.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/EventTarget.h>
namespace Web::DOM {
void EventDispatcher::dispatch(EventTarget& target, NonnullRefPtr<Event> event)
{
auto listeners = target.listeners();
for (auto& listener : listeners) {
if (listener.event_name != event->type())
continue;
auto& function = listener.listener->function();
auto& global_object = function.global_object();
auto* this_value = Bindings::wrap(global_object, target);
auto& interpreter = function.interpreter();
(void)interpreter.call(function, this_value, Bindings::wrap(global_object, target));
if (interpreter.exception())
interpreter.clear_exception();
}
}
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <LibWeb/Forward.h>
namespace Web::DOM {
class EventDispatcher {
public:
static void dispatch(EventTarget&, NonnullRefPtr<Event>);
};
}

View file

@ -29,6 +29,7 @@
#include <AK/FlyString.h>
#include <AK/Noncopyable.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibWeb/Forward.h>
namespace Web::DOM {
@ -47,6 +48,7 @@ public:
void remove_event_listener(const FlyString& event_name, NonnullRefPtr<EventListener>);
virtual void dispatch_event(NonnullRefPtr<Event>) = 0;
virtual Bindings::EventTargetWrapper* create_wrapper(JS::GlobalObject&) = 0;
struct EventListenerRegistration {
FlyString event_name;

View file

@ -36,6 +36,7 @@
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
@ -119,25 +120,7 @@ bool Node::is_link() const
void Node::dispatch_event(NonnullRefPtr<Event> event)
{
for (auto& listener : listeners()) {
if (listener.event_name == event->type()) {
auto& function = const_cast<EventListener&>(*listener.listener).function();
#ifdef EVENT_DEBUG
static_cast<const JS::ScriptFunction&>(function).body().dump(0);
#endif
auto& global_object = function.global_object();
auto* this_value = wrap(global_object, *this);
#ifdef EVENT_DEBUG
dbg() << "calling event listener with this=" << this_value;
#endif
auto* event_wrapper = wrap(global_object, *event);
auto& interpreter = document().interpreter();
(void)interpreter.call(function, this_value, event_wrapper);
if (interpreter.exception())
interpreter.clear_exception();
}
}
EventDispatcher::dispatch(*this, event);
// FIXME: This is a hack. We should follow the real rules of event bubbling.
if (parent())
parent()->dispatch_event(move(event));
@ -218,4 +201,9 @@ bool Node::is_editable() const
return parent() && parent()->is_editable();
}
Bindings::EventTargetWrapper* Node::create_wrapper(JS::GlobalObject& global_object)
{
return wrap(global_object, *this);
}
}

View file

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

View file

@ -31,6 +31,7 @@
#include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/DOM/XMLHttpRequest.h>
@ -91,17 +92,12 @@ void XMLHttpRequest::send()
void XMLHttpRequest::dispatch_event(NonnullRefPtr<DOM::Event> event)
{
for (auto& listener : listeners()) {
if (listener.event_name == event->type()) {
auto& function = const_cast<DOM::EventListener&>(*listener.listener).function();
auto& global_object = function.global_object();
auto* this_value = wrap(global_object, *this);
auto& interpreter = function.interpreter();
(void)interpreter.call(function, this_value, wrap(global_object, *this));
if (interpreter.exception())
interpreter.clear_exception();
}
}
DOM::EventDispatcher::dispatch(*this, move(event));
}
Bindings::EventTargetWrapper* XMLHttpRequest::create_wrapper(JS::GlobalObject& global_object)
{
return wrap(global_object, *this);
}
}

View file

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