Event.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/DOM/EventTarget.h>
  10. namespace Web::DOM {
  11. struct EventInit {
  12. bool bubbles { false };
  13. bool cancelable { false };
  14. bool composed { false };
  15. };
  16. class Event : public Bindings::PlatformObject {
  17. WEB_PLATFORM_OBJECT(Event, Bindings::PlatformObject);
  18. public:
  19. enum Phase : u16 {
  20. None = 0,
  21. CapturingPhase = 1,
  22. AtTarget = 2,
  23. BubblingPhase = 3,
  24. };
  25. // FIXME: These need explicit marking somehow.
  26. using TouchTargetList = Vector<JS::GCPtr<EventTarget>>;
  27. struct PathEntry {
  28. JS::GCPtr<EventTarget> invocation_target;
  29. bool invocation_target_in_shadow_tree { false };
  30. JS::GCPtr<EventTarget> shadow_adjusted_target;
  31. JS::GCPtr<EventTarget> related_target;
  32. TouchTargetList touch_target_list;
  33. bool root_of_closed_tree { false };
  34. bool slot_in_closed_tree { false };
  35. size_t index;
  36. };
  37. using Path = Vector<PathEntry>;
  38. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> create(JS::Realm&, FlyString const& event_name, EventInit const& event_init = {});
  39. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> construct_impl(JS::Realm&, FlyString const& event_name, EventInit const& event_init);
  40. Event(JS::Realm&, FlyString const& type);
  41. Event(JS::Realm&, FlyString const& type, EventInit const& event_init);
  42. virtual ~Event() = default;
  43. double time_stamp() const;
  44. FlyString const& type() const { return m_type; }
  45. void set_type(FlyString const& type) { m_type = type; }
  46. JS::GCPtr<EventTarget> target() const { return m_target; }
  47. void set_target(EventTarget* target) { m_target = target; }
  48. // NOTE: This is intended for the JS bindings.
  49. JS::GCPtr<EventTarget> src_element() const { return target(); }
  50. JS::GCPtr<EventTarget> related_target() const { return m_related_target; }
  51. void set_related_target(EventTarget* related_target) { m_related_target = related_target; }
  52. bool should_stop_propagation() const { return m_stop_propagation; }
  53. void set_stop_propagation(bool stop_propagation) { m_stop_propagation = stop_propagation; }
  54. bool should_stop_immediate_propagation() const { return m_stop_immediate_propagation; }
  55. void set_stop_immediate_propagation(bool stop_immediate_propagation) { m_stop_immediate_propagation = stop_immediate_propagation; }
  56. bool cancelled() const { return m_cancelled; }
  57. void set_cancelled(bool cancelled) { m_cancelled = cancelled; }
  58. bool in_passive_listener() const { return m_in_passive_listener; }
  59. void set_in_passive_listener(bool in_passive_listener) { m_in_passive_listener = in_passive_listener; }
  60. bool composed() const { return m_composed; }
  61. void set_composed(bool composed) { m_composed = composed; }
  62. bool initialized() const { return m_initialized; }
  63. void set_initialized(bool initialized) { m_initialized = initialized; }
  64. bool dispatched() const { return m_dispatch; }
  65. void set_dispatched(bool dispatched) { m_dispatch = dispatched; }
  66. void prevent_default() { set_cancelled_flag(); }
  67. bool default_prevented() const { return cancelled(); }
  68. u16 event_phase() const { return m_phase; }
  69. void set_phase(Phase phase) { m_phase = phase; }
  70. JS::GCPtr<EventTarget> current_target() const { return m_current_target; }
  71. void set_current_target(EventTarget* current_target) { m_current_target = current_target; }
  72. bool return_value() const { return !m_cancelled; }
  73. void set_return_value(bool return_value)
  74. {
  75. if (!return_value)
  76. set_cancelled_flag();
  77. }
  78. void append_to_path(EventTarget&, JS::GCPtr<EventTarget>, JS::GCPtr<EventTarget>, TouchTargetList&, bool);
  79. Path& path() { return m_path; }
  80. Path const& path() const { return m_path; }
  81. void clear_path() { m_path.clear(); }
  82. void set_touch_target_list(TouchTargetList& touch_target_list) { m_touch_target_list = touch_target_list; }
  83. TouchTargetList& touch_target_list() { return m_touch_target_list; };
  84. void clear_touch_target_list() { m_touch_target_list.clear(); }
  85. bool bubbles() const { return m_bubbles; }
  86. void set_bubbles(bool bubbles) { m_bubbles = bubbles; }
  87. bool cancelable() const { return m_cancelable; }
  88. void set_cancelable(bool cancelable) { m_cancelable = cancelable; }
  89. bool is_trusted() const { return m_is_trusted; }
  90. void set_is_trusted(bool is_trusted) { m_is_trusted = is_trusted; }
  91. void stop_propagation() { m_stop_propagation = true; }
  92. bool cancel_bubble() const { return m_stop_propagation; }
  93. void set_cancel_bubble(bool cancel_bubble)
  94. {
  95. if (cancel_bubble)
  96. m_stop_propagation = true;
  97. }
  98. void stop_immediate_propagation()
  99. {
  100. m_stop_propagation = true;
  101. m_stop_immediate_propagation = true;
  102. }
  103. void init_event(String const&, bool, bool);
  104. void set_time_stamp(double time_stamp) { m_time_stamp = time_stamp; }
  105. Vector<JS::Handle<EventTarget>> composed_path() const;
  106. protected:
  107. void initialize_event(String const&, bool, bool);
  108. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  109. virtual void visit_edges(Visitor&) override;
  110. private:
  111. FlyString m_type;
  112. JS::GCPtr<EventTarget> m_target;
  113. JS::GCPtr<EventTarget> m_related_target;
  114. JS::GCPtr<EventTarget> m_current_target;
  115. Phase m_phase { None };
  116. bool m_bubbles { false };
  117. bool m_cancelable { false };
  118. bool m_stop_propagation { false };
  119. bool m_stop_immediate_propagation { false };
  120. bool m_cancelled { false };
  121. bool m_in_passive_listener { false };
  122. bool m_composed { false };
  123. bool m_initialized { false };
  124. bool m_dispatch { false };
  125. bool m_is_trusted { true };
  126. Path m_path;
  127. TouchTargetList m_touch_target_list;
  128. double m_time_stamp { 0 };
  129. void set_cancelled_flag();
  130. };
  131. }