EventTarget.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/DeprecatedFlyString.h>
  8. #include <AK/Noncopyable.h>
  9. #include <AK/Vector.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibWeb/Bindings/PlatformObject.h>
  12. #include <LibWeb/DOM/DOMEventListener.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/HTML/EventHandler.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. namespace Web::DOM {
  17. class EventTarget : public Bindings::PlatformObject {
  18. WEB_PLATFORM_OBJECT(EventTarget, Bindings::PlatformObject);
  19. public:
  20. virtual ~EventTarget() override;
  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. Function<void(Event const&)> activation_behavior;
  35. // NOTE: These only exist for checkbox and radio input elements.
  36. virtual void legacy_pre_activation_behavior() { }
  37. virtual void legacy_cancelled_activation_behavior() { }
  38. virtual void legacy_cancelled_activation_behavior_was_not_called() { }
  39. WebIDL::CallbackType* event_handler_attribute(FlyString const& name);
  40. void set_event_handler_attribute(FlyString const& name, WebIDL::CallbackType*);
  41. bool has_event_listener(FlyString const& type) const;
  42. bool has_event_listeners() const;
  43. protected:
  44. explicit EventTarget(JS::Realm&);
  45. void element_event_handler_attribute_changed(FlyString const& local_name, Optional<String> const& value);
  46. virtual void visit_edges(Cell::Visitor&) override;
  47. private:
  48. Vector<JS::NonnullGCPtr<DOMEventListener>> m_event_listener_list;
  49. // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-map
  50. // 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.
  51. HashMap<FlyString, JS::GCPtr<HTML::EventHandler>> m_event_handler_map;
  52. WebIDL::CallbackType* get_current_value_of_event_handler(FlyString const& name);
  53. void activate_event_handler(FlyString const& name, HTML::EventHandler& event_handler);
  54. void deactivate_event_handler(FlyString const& name);
  55. JS::ThrowCompletionOr<void> process_event_handler_for_event(FlyString const& name, Event& event);
  56. };
  57. bool is_window_reflecting_body_element_event_handler(FlyString const& name);
  58. }