EventTarget.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/Noncopyable.h>
  8. #include <AK/Vector.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/DOM/DOMEventListener.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/HTML/EventHandler.h>
  14. #include <LibWeb/WebIDL/ExceptionOr.h>
  15. namespace Web::DOM {
  16. class EventTarget : public Bindings::PlatformObject {
  17. WEB_PLATFORM_OBJECT(EventTarget, Bindings::PlatformObject);
  18. public:
  19. virtual ~EventTarget() override;
  20. static WebIDL::ExceptionOr<JS::NonnullGCPtr<EventTarget>> construct_impl(JS::Realm&);
  21. virtual bool is_focusable() const { return false; }
  22. void add_event_listener(FlyString const& type, IDLEventListener* callback, Variant<AddEventListenerOptions, bool> const& options);
  23. void remove_event_listener(FlyString const& type, IDLEventListener* callback, Variant<EventListenerOptions, bool> const& options);
  24. // NOTE: These are for internal use only. They operate as though addEventListener(type, callback) was called instead of addEventListener(type, callback, options).
  25. void add_event_listener_without_options(FlyString const& type, IDLEventListener& callback);
  26. void remove_event_listener_without_options(FlyString const& type, IDLEventListener& callback);
  27. virtual bool dispatch_event(Event&);
  28. WebIDL::ExceptionOr<bool> dispatch_event_binding(Event&);
  29. virtual EventTarget* get_parent(Event const&) { return nullptr; }
  30. void add_an_event_listener(DOMEventListener&);
  31. void remove_an_event_listener(DOMEventListener&);
  32. void remove_from_event_listener_list(DOMEventListener&);
  33. Vector<JS::Handle<DOMEventListener>> event_listener_list();
  34. virtual bool has_activation_behavior() const;
  35. virtual void activation_behavior(Event const&);
  36. // NOTE: These only exist for checkbox and radio input elements.
  37. virtual void legacy_pre_activation_behavior() { }
  38. virtual void legacy_cancelled_activation_behavior() { }
  39. virtual void legacy_cancelled_activation_behavior_was_not_called() { }
  40. WebIDL::CallbackType* event_handler_attribute(FlyString const& name);
  41. void set_event_handler_attribute(FlyString const& name, WebIDL::CallbackType*);
  42. bool has_event_listener(FlyString const& type) const;
  43. bool has_event_listeners() const;
  44. protected:
  45. explicit EventTarget(JS::Realm&, MayInterfereWithIndexedPropertyAccess = MayInterfereWithIndexedPropertyAccess::No);
  46. void element_event_handler_attribute_changed(FlyString const& local_name, Optional<String> const& value);
  47. virtual void initialize(JS::Realm&) override;
  48. virtual void visit_edges(Cell::Visitor&) override;
  49. private:
  50. struct Data {
  51. Vector<JS::NonnullGCPtr<DOMEventListener>> 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, JS::NonnullGCPtr<HTML::EventHandler>> event_handler_map;
  55. };
  56. Data& ensure_data();
  57. OwnPtr<Data> m_data;
  58. WebIDL::CallbackType* get_current_value_of_event_handler(FlyString const& name);
  59. void activate_event_handler(FlyString const& name, HTML::EventHandler& event_handler);
  60. void deactivate_event_handler(FlyString const& name);
  61. JS::ThrowCompletionOr<void> process_event_handler_for_event(FlyString const& name, Event& event);
  62. };
  63. bool is_window_reflecting_body_element_event_handler(FlyString const& name);
  64. }