Event.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <LibWeb/Bindings/WindowObject.h>
  29. #include <LibWeb/Bindings/Wrappable.h>
  30. #include <LibWeb/DOM/EventTarget.h>
  31. namespace Web::DOM {
  32. class Event
  33. : public RefCounted<Event>
  34. , public Bindings::Wrappable {
  35. public:
  36. using WrapperType = Bindings::EventWrapper;
  37. enum Phase : u16 {
  38. None = 0,
  39. CapturingPhase = 1,
  40. AtTarget = 2,
  41. BubblingPhase = 3,
  42. };
  43. using TouchTargetList = Vector<RefPtr<EventTarget>>;
  44. struct PathEntry {
  45. RefPtr<EventTarget> invocation_target;
  46. bool invocation_target_in_shadow_tree { false };
  47. RefPtr<EventTarget> shadow_adjusted_target;
  48. RefPtr<EventTarget> related_target;
  49. TouchTargetList touch_target_list;
  50. bool root_of_closed_tree { false };
  51. bool slot_in_closed_tree { false };
  52. size_t index;
  53. };
  54. using Path = Vector<PathEntry>;
  55. static NonnullRefPtr<Event> create(const FlyString& event_name)
  56. {
  57. return adopt(*new Event(event_name));
  58. }
  59. static NonnullRefPtr<Event> create_with_global_object(Bindings::WindowObject&, const FlyString& event_name)
  60. {
  61. return Event::create(event_name);
  62. }
  63. virtual ~Event() { }
  64. const FlyString& type() const { return m_type; }
  65. void set_type(const StringView& type) { m_type = type; }
  66. RefPtr<EventTarget> target() const { return m_target; }
  67. void set_target(EventTarget* target) { m_target = target; }
  68. // NOTE: This is intended for the JS bindings.
  69. RefPtr<EventTarget> src_target() const { return target(); }
  70. RefPtr<EventTarget> related_target() const { return m_related_target; }
  71. void set_related_target(EventTarget* related_target) { m_related_target = related_target; }
  72. bool should_stop_propagation() const { return m_stop_propagation; }
  73. void set_stop_propagation(bool stop_propagation) { m_stop_propagation = stop_propagation; }
  74. bool should_stop_immediate_propagation() const { return m_stop_immediate_propagation; }
  75. void set_stop_immediate_propagation(bool stop_immediate_propagation) { m_stop_immediate_propagation = stop_immediate_propagation; }
  76. bool cancelled() const { return m_cancelled; }
  77. void set_cancelled(bool cancelled) { m_cancelled = cancelled; }
  78. bool in_passive_listener() const { return m_in_passive_listener; }
  79. void set_in_passive_listener(bool in_passive_listener) { m_in_passive_listener = in_passive_listener; }
  80. bool composed() const { return m_composed; }
  81. void set_composed(bool composed) { m_composed = composed; }
  82. bool initialized() const { return m_initialized; }
  83. void set_initialized(bool initialized) { m_initialized = initialized; }
  84. bool dispatched() const { return m_dispatch; }
  85. void set_dispatched(bool dispatched) { m_dispatch = dispatched; }
  86. void prevent_default() { set_cancelled_flag(); }
  87. bool default_prevented() const { return cancelled(); }
  88. u16 event_phase() const { return m_phase; }
  89. void set_phase(Phase phase) { m_phase = phase; }
  90. RefPtr<EventTarget> current_target() const { return m_current_target; }
  91. void set_current_target(EventTarget* current_target) { m_current_target = current_target; }
  92. bool return_value() const { return !m_cancelled; }
  93. void set_return_value(bool return_value)
  94. {
  95. if (!return_value)
  96. set_cancelled_flag();
  97. }
  98. void append_to_path(EventTarget&, RefPtr<EventTarget>, RefPtr<EventTarget>, TouchTargetList&, bool);
  99. Path& path() { return m_path; }
  100. const Path& path() const { return m_path; }
  101. void clear_path() { m_path.clear(); }
  102. void set_touch_target_list(TouchTargetList& touch_target_list) { m_touch_target_list = touch_target_list; }
  103. TouchTargetList& touch_target_list() { return m_touch_target_list; };
  104. void clear_touch_target_list() { m_touch_target_list.clear(); }
  105. bool bubbles() const { return m_bubbles; }
  106. void set_bubbles(bool bubbles) { m_bubbles = bubbles; }
  107. bool cancelable() const { return m_cancelable; }
  108. void set_cancelable(bool cancelable) { m_cancelable = cancelable; }
  109. bool is_trusted() const { return m_is_trusted; }
  110. void set_is_trusted(bool is_trusted) { m_is_trusted = is_trusted; }
  111. void stop_propagation() { m_stop_propagation = true; }
  112. bool cancel_bubble() const { return m_stop_propagation; }
  113. void set_cancel_bubble(bool cancel_bubble)
  114. {
  115. if (cancel_bubble)
  116. m_stop_propagation = true;
  117. }
  118. void stop_immediate_propagation()
  119. {
  120. m_stop_propagation = true;
  121. m_stop_immediate_propagation = true;
  122. }
  123. protected:
  124. explicit Event(const FlyString& type)
  125. : m_type(type)
  126. , m_initialized(true)
  127. {
  128. }
  129. private:
  130. FlyString m_type;
  131. RefPtr<EventTarget> m_target;
  132. RefPtr<EventTarget> m_related_target;
  133. RefPtr<EventTarget> m_current_target;
  134. Phase m_phase { None };
  135. bool m_bubbles { false };
  136. bool m_cancelable { false };
  137. bool m_stop_propagation { false };
  138. bool m_stop_immediate_propagation { false };
  139. bool m_cancelled { false };
  140. bool m_in_passive_listener { false };
  141. bool m_composed { false };
  142. bool m_initialized { false };
  143. bool m_dispatch { false };
  144. bool m_is_trusted { true };
  145. Path m_path;
  146. TouchTargetList m_touch_target_list;
  147. void set_cancelled_flag();
  148. };
  149. }