EventTarget.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/Noncopyable.h>
  9. #include <AK/Vector.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibWeb/DOM/DOMEventListener.h>
  12. #include <LibWeb/DOM/ExceptionOr.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/HTML/EventHandler.h>
  15. namespace Web::DOM {
  16. class EventTarget {
  17. AK_MAKE_NONCOPYABLE(EventTarget);
  18. AK_MAKE_NONMOVABLE(EventTarget);
  19. public:
  20. virtual ~EventTarget();
  21. void ref() { ref_event_target(); }
  22. void unref() { unref_event_target(); }
  23. virtual bool is_focusable() const { return false; }
  24. void add_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback, Variant<AddEventListenerOptions, bool> const& options);
  25. void remove_event_listener(FlyString const& type, RefPtr<IDLEventListener> callback, Variant<EventListenerOptions, bool> const& options);
  26. // NOTE: These are for internal use only. They operate as though addEventListener(type, callback) was called instead of addEventListener(type, callback, options).
  27. void add_event_listener_without_options(FlyString const& type, RefPtr<IDLEventListener> callback);
  28. void remove_event_listener_without_options(FlyString const& type, RefPtr<IDLEventListener> callback);
  29. virtual bool dispatch_event(NonnullRefPtr<Event>);
  30. ExceptionOr<bool> dispatch_event_binding(NonnullRefPtr<Event>);
  31. virtual JS::Object* create_wrapper(JS::GlobalObject&) = 0;
  32. virtual EventTarget* get_parent(const Event&) { return nullptr; }
  33. void add_an_event_listener(NonnullRefPtr<DOMEventListener>);
  34. void remove_an_event_listener(DOMEventListener&);
  35. void remove_from_event_listener_list(DOMEventListener&);
  36. auto& event_listener_list() { return m_event_listener_list; }
  37. auto const& event_listener_list() const { return m_event_listener_list; }
  38. Function<void(const Event&)> activation_behavior;
  39. // NOTE: These only exist for checkbox and radio input elements.
  40. virtual void legacy_pre_activation_behavior() { }
  41. virtual void legacy_cancelled_activation_behavior() { }
  42. virtual void legacy_cancelled_activation_behavior_was_not_called() { }
  43. Bindings::CallbackType* event_handler_attribute(FlyString const& name);
  44. void set_event_handler_attribute(FlyString const& name, Optional<Bindings::CallbackType>);
  45. protected:
  46. EventTarget();
  47. virtual void ref_event_target() = 0;
  48. virtual void unref_event_target() = 0;
  49. void element_event_handler_attribute_changed(FlyString const& local_name, String const& value);
  50. private:
  51. Vector<NonnullRefPtr<DOMEventListener>> m_event_listener_list;
  52. // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-map
  53. // 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.
  54. HashMap<FlyString, HTML::EventHandler> m_event_handler_map;
  55. enum class IsAttribute {
  56. No,
  57. Yes,
  58. };
  59. Bindings::CallbackType* get_current_value_of_event_handler(FlyString const& name);
  60. void activate_event_handler(FlyString const& name, HTML::EventHandler& event_handler, IsAttribute is_attribute);
  61. void deactivate_event_handler(FlyString const& name);
  62. JS::ThrowCompletionOr<void> process_event_handler_for_event(FlyString const& name, Event& event);
  63. };
  64. }