1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:07:35 +00:00

LibWeb: Separate "event listener" from "EventListener"

I can't imagine how this happened, but it seems we've managed to
conflate the "event listener" and "EventListener" concepts from the DOM
specification in some parts of the code.

We previously had two things:

    - DOM::EventListener
    - DOM::EventTarget::EventListenerRegistration

DOM::EventListener was roughly the "EventListener" IDL type,
and DOM::EventTarget::EventListenerRegistration was roughly the "event
listener" concept. However, they were used interchangeably (and
incorrectly!) in many places.

After this patch, we now have:

    - DOM::IDLEventListener
    - DOM::DOMEventListener

DOM::IDLEventListener is the "EventListener" IDL type,
and DOM::DOMEventListener is the "event listener" concept.

This patch also updates the addEventListener() and removeEventListener()
functions to follow the spec more closely, along with the "inner invoke"
function in our EventDispatcher.
This commit is contained in:
Andreas Kling 2022-02-16 20:43:24 +01:00
parent 0e2cd5540a
commit e76e8e22b5
21 changed files with 223 additions and 129 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -14,11 +14,12 @@
#include <LibWeb/Bindings/EventWrapperFactory.h>
#include <LibWeb/Bindings/IDLAbstractOperations.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/AbortSignal.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Window.h>
@ -50,37 +51,37 @@ static EventTarget* retarget(EventTarget* left, EventTarget* right)
}
// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
bool EventDispatcher::inner_invoke(Event& event, Vector<EventTarget::EventListenerRegistration>& listeners, Event::Phase phase, bool invocation_target_in_shadow_tree)
bool EventDispatcher::inner_invoke(Event& event, Vector<NonnullRefPtr<DOM::DOMEventListener>>& listeners, Event::Phase phase, bool invocation_target_in_shadow_tree)
{
// 1. Let found be false.
bool found = false;
// 2. For each listener in listeners, whose removed is false:
for (auto& listener : listeners) {
if (listener.listener->removed())
if (listener->removed)
continue;
// 1. If events type attribute value is not listeners type, then continue.
if (event.type() != listener.listener->type())
if (event.type() != listener->type)
continue;
// 2. Set found to true.
found = true;
// 3. If phase is "capturing" and listeners capture is false, then continue.
if (phase == Event::Phase::CapturingPhase && !listener.listener->capture())
if (phase == Event::Phase::CapturingPhase && !listener->capture)
continue;
// 4. If phase is "bubbling" and listeners capture is true, then continue.
if (phase == Event::Phase::BubblingPhase && listener.listener->capture())
if (phase == Event::Phase::BubblingPhase && listener->capture)
continue;
// 5. If listeners once is true, then remove listener from events currentTarget attribute values event listener list.
if (listener.listener->once())
event.current_target()->remove_from_event_listener_list(listener.listener);
if (listener->once)
event.current_target()->remove_from_event_listener_list(listener);
// 6. Let global be listener callbacks associated Realms global object.
auto& callback = listener.listener->callback();
auto& callback = listener->callback->callback();
auto& global = callback.callback.cell()->global_object();
// 7. Let currentEvent be undefined.
@ -100,7 +101,7 @@ bool EventDispatcher::inner_invoke(Event& event, Vector<EventTarget::EventListen
}
// 9. If listeners passive is true, then set events in passive listener flag.
if (listener.listener->passive())
if (listener->passive)
event.set_in_passive_listener(true);
// 10. Call a user objects operation with listeners callback, "handleEvent", « event », and events currentTarget attribute value. If this throws an exception, then:
@ -155,7 +156,7 @@ void EventDispatcher::invoke(Event::PathEntry& struct_, Event& event, Event::Pha
event.set_current_target(struct_.invocation_target);
// NOTE: This is an intentional copy. Any event listeners added after this point will not be invoked.
auto listeners = event.current_target()->listeners();
auto listeners = event.current_target()->event_listener_list();
bool invocation_target_in_shadow_tree = struct_.invocation_target_in_shadow_tree;
bool found = inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);