Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibWeb/DOM/EventTarget.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/Noncopyable.h>
10
#include <AK/Vector.h>
11
#include <LibJS/Forward.h>
12
#include <LibWeb/Bindings/PlatformObject.h>
13
#include <LibWeb/DOM/DOMEventListener.h>
14
#include <LibWeb/Forward.h>
15
#include <LibWeb/HTML/EventHandler.h>
16
#include <LibWeb/WebIDL/ExceptionOr.h>
17
18
namespace Web::DOM {
19
20
class EventTarget : public Bindings::PlatformObject {
21
    WEB_PLATFORM_OBJECT(EventTarget, Bindings::PlatformObject);
22
    JS_DECLARE_ALLOCATOR(EventTarget);
23
24
public:
25
    virtual ~EventTarget() override;
26
27
    static WebIDL::ExceptionOr<JS::NonnullGCPtr<EventTarget>> construct_impl(JS::Realm&);
28
29
0
    virtual bool is_focusable() const { return false; }
30
31
    void add_event_listener(FlyString const& type, IDLEventListener* callback, Variant<AddEventListenerOptions, bool> const& options);
32
    void remove_event_listener(FlyString const& type, IDLEventListener* callback, Variant<EventListenerOptions, bool> const& options);
33
34
    // NOTE: These are for internal use only. They operate as though addEventListener(type, callback) was called instead of addEventListener(type, callback, options).
35
    void add_event_listener_without_options(FlyString const& type, IDLEventListener& callback);
36
    void remove_event_listener_without_options(FlyString const& type, IDLEventListener& callback);
37
38
    virtual bool dispatch_event(Event&);
39
    WebIDL::ExceptionOr<bool> dispatch_event_binding(Event&);
40
41
0
    virtual EventTarget* get_parent(Event const&) { return nullptr; }
42
43
    void add_an_event_listener(DOMEventListener&);
44
    void remove_an_event_listener(DOMEventListener&);
45
    void remove_from_event_listener_list(DOMEventListener&);
46
47
    Vector<JS::Handle<DOMEventListener>> event_listener_list();
48
49
    virtual bool has_activation_behavior() const;
50
    virtual void activation_behavior(Event const&);
51
52
    // NOTE: These only exist for checkbox and radio input elements.
53
0
    virtual void legacy_pre_activation_behavior() { }
54
0
    virtual void legacy_cancelled_activation_behavior() { }
55
0
    virtual void legacy_cancelled_activation_behavior_was_not_called() { }
56
57
    WebIDL::CallbackType* event_handler_attribute(FlyString const& name);
58
    void set_event_handler_attribute(FlyString const& name, WebIDL::CallbackType*);
59
60
    bool has_event_listener(FlyString const& type) const;
61
    bool has_event_listeners() const;
62
63
protected:
64
    explicit EventTarget(JS::Realm&, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
65
66
    void element_event_handler_attribute_changed(FlyString const& local_name, Optional<String> const& value);
67
68
    virtual void initialize(JS::Realm&) override;
69
    virtual void visit_edges(Cell::Visitor&) override;
70
71
private:
72
    struct Data {
73
        Vector<JS::NonnullGCPtr<DOMEventListener>> event_listener_list;
74
75
        // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-map
76
        // Spec Note: The order of the entries of event handler map could be arbitrary. It is not observable through any algorithms that operate on the map.
77
        HashMap<FlyString, JS::NonnullGCPtr<HTML::EventHandler>> event_handler_map;
78
    };
79
80
    Data& ensure_data();
81
    OwnPtr<Data> m_data;
82
83
    WebIDL::CallbackType* get_current_value_of_event_handler(FlyString const& name);
84
    void activate_event_handler(FlyString const& name, HTML::EventHandler& event_handler);
85
    void deactivate_event_handler(FlyString const& name);
86
    JS::ThrowCompletionOr<void> process_event_handler_for_event(FlyString const& name, Event& event);
87
};
88
89
bool is_window_reflecting_body_element_event_handler(FlyString const& name);
90
91
}