Serenity Operating System
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/DeprecatedFlyString.h>
10#include <AK/Noncopyable.h>
11#include <AK/Vector.h>
12#include <LibJS/Forward.h>
13#include <LibWeb/Bindings/PlatformObject.h>
14#include <LibWeb/DOM/DOMEventListener.h>
15#include <LibWeb/Forward.h>
16#include <LibWeb/HTML/EventHandler.h>
17#include <LibWeb/WebIDL/ExceptionOr.h>
18
19namespace Web::DOM {
20
21class EventTarget : public Bindings::PlatformObject {
22 WEB_PLATFORM_OBJECT(EventTarget, Bindings::PlatformObject);
23
24public:
25 virtual ~EventTarget() override;
26
27 virtual bool is_focusable() const { return false; }
28
29 void add_event_listener(DeprecatedFlyString const& type, IDLEventListener* callback, Variant<AddEventListenerOptions, bool> const& options);
30 void remove_event_listener(DeprecatedFlyString const& type, IDLEventListener* callback, Variant<EventListenerOptions, bool> const& options);
31
32 // NOTE: These are for internal use only. They operate as though addEventListener(type, callback) was called instead of addEventListener(type, callback, options).
33 void add_event_listener_without_options(DeprecatedFlyString const& type, IDLEventListener& callback);
34 void remove_event_listener_without_options(DeprecatedFlyString const& type, IDLEventListener& callback);
35
36 virtual bool dispatch_event(Event&);
37 WebIDL::ExceptionOr<bool> dispatch_event_binding(Event&);
38
39 virtual EventTarget* get_parent(Event const&) { return nullptr; }
40
41 void add_an_event_listener(DOMEventListener&);
42 void remove_an_event_listener(DOMEventListener&);
43 void remove_from_event_listener_list(DOMEventListener&);
44
45 Vector<JS::Handle<DOMEventListener>> event_listener_list();
46
47 Function<void(Event const&)> activation_behavior;
48
49 // NOTE: These only exist for checkbox and radio input elements.
50 virtual void legacy_pre_activation_behavior() { }
51 virtual void legacy_cancelled_activation_behavior() { }
52 virtual void legacy_cancelled_activation_behavior_was_not_called() { }
53
54 WebIDL::CallbackType* event_handler_attribute(DeprecatedFlyString const& name);
55 void set_event_handler_attribute(DeprecatedFlyString const& name, WebIDL::CallbackType*);
56
57 bool has_event_listener(DeprecatedFlyString const& type) const;
58 bool has_event_listeners() const;
59
60protected:
61 explicit EventTarget(JS::Realm&);
62
63 void element_event_handler_attribute_changed(DeprecatedFlyString const& local_name, DeprecatedString const& value);
64
65 virtual void visit_edges(Cell::Visitor&) override;
66
67private:
68 Vector<JS::NonnullGCPtr<DOMEventListener>> m_event_listener_list;
69
70 // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-map
71 // 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.
72 HashMap<DeprecatedFlyString, JS::GCPtr<HTML::EventHandler>> m_event_handler_map;
73
74 WebIDL::CallbackType* get_current_value_of_event_handler(DeprecatedFlyString const& name);
75 void activate_event_handler(DeprecatedFlyString const& name, HTML::EventHandler& event_handler);
76 void deactivate_event_handler(DeprecatedFlyString const& name);
77 JS::ThrowCompletionOr<void> process_event_handler_for_event(DeprecatedFlyString const& name, Event& event);
78};
79
80bool is_window_reflecting_body_element_event_handler(DeprecatedFlyString const& name);
81
82}