Event.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/TypeCasts.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/DOM/Event.h>
  11. #include <LibWeb/DOM/Node.h>
  12. #include <LibWeb/DOM/ShadowRoot.h>
  13. namespace Web::DOM {
  14. JS::NonnullGCPtr<Event> Event::create(JS::Realm& realm, FlyString const& event_name, EventInit const& event_init)
  15. {
  16. return *realm.heap().allocate<Event>(realm, realm, event_name, event_init);
  17. }
  18. JS::NonnullGCPtr<Event> Event::construct_impl(JS::Realm& realm, FlyString const& event_name, EventInit const& event_init)
  19. {
  20. return create(realm, event_name, event_init);
  21. }
  22. Event::Event(JS::Realm& realm, FlyString const& type)
  23. : PlatformObject(Bindings::cached_web_prototype(realm, "Event"))
  24. , m_type(type)
  25. , m_initialized(true)
  26. {
  27. }
  28. Event::Event(JS::Realm& realm, FlyString const& type, EventInit const& event_init)
  29. : PlatformObject(Bindings::cached_web_prototype(realm, "Event"))
  30. , m_type(type)
  31. , m_bubbles(event_init.bubbles)
  32. , m_cancelable(event_init.cancelable)
  33. , m_composed(event_init.composed)
  34. , m_initialized(true)
  35. {
  36. }
  37. void Event::visit_edges(Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. visitor.visit(m_target.ptr());
  41. visitor.visit(m_related_target.ptr());
  42. visitor.visit(m_current_target.ptr());
  43. for (auto& it : m_path) {
  44. visitor.visit(it.invocation_target.ptr());
  45. visitor.visit(it.shadow_adjusted_target.ptr());
  46. visitor.visit(it.related_target.ptr());
  47. for (auto& itit : it.touch_target_list)
  48. visitor.visit(itit.ptr());
  49. }
  50. for (auto& it : m_touch_target_list)
  51. visitor.visit(it.ptr());
  52. }
  53. // https://dom.spec.whatwg.org/#concept-event-path-append
  54. void Event::append_to_path(EventTarget& invocation_target, JS::GCPtr<EventTarget> shadow_adjusted_target, JS::GCPtr<EventTarget> related_target, TouchTargetList& touch_targets, bool slot_in_closed_tree)
  55. {
  56. // 1. Let invocationTargetInShadowTree be false.
  57. bool invocation_target_in_shadow_tree = false;
  58. // 3. Let root-of-closed-tree be false.
  59. bool root_of_closed_tree = false;
  60. // 2. If invocationTarget is a node and its root is a shadow root, then set invocationTargetInShadowTree to true.
  61. if (is<Node>(invocation_target)) {
  62. auto& invocation_target_node = verify_cast<Node>(invocation_target);
  63. if (is<ShadowRoot>(invocation_target_node.root()))
  64. invocation_target_in_shadow_tree = true;
  65. if (is<ShadowRoot>(invocation_target_node)) {
  66. auto& invocation_target_shadow_root = verify_cast<ShadowRoot>(invocation_target_node);
  67. // 4. If invocationTarget is a shadow root whose mode is "closed", then set root-of-closed-tree to true.
  68. root_of_closed_tree = invocation_target_shadow_root.closed();
  69. }
  70. }
  71. // 5. Append a new struct to event’s path whose invocation target is invocationTarget, invocation-target-in-shadow-tree is invocationTargetInShadowTree,
  72. // shadow-adjusted target is shadowAdjustedTarget, relatedTarget is relatedTarget, touch target list is touchTargets, root-of-closed-tree is root-of-closed-tree,
  73. // and slot-in-closed-tree is slot-in-closed-tree.
  74. m_path.append({ invocation_target, invocation_target_in_shadow_tree, shadow_adjusted_target, related_target, touch_targets, root_of_closed_tree, slot_in_closed_tree, m_path.size() });
  75. }
  76. void Event::set_cancelled_flag()
  77. {
  78. if (m_cancelable && !m_in_passive_listener)
  79. m_cancelled = true;
  80. }
  81. // https://dom.spec.whatwg.org/#concept-event-initialize
  82. void Event::initialize_event(String const& type, bool bubbles, bool cancelable)
  83. {
  84. // 1. Set event’s initialized flag.
  85. m_initialized = true;
  86. // 2. Unset event’s stop propagation flag, stop immediate propagation flag, and canceled flag.
  87. m_stop_propagation = false;
  88. m_stop_immediate_propagation = false;
  89. m_cancelled = false;
  90. // 3. Set event’s isTrusted attribute to false.
  91. m_is_trusted = false;
  92. // 4. Set event’s target to null.
  93. m_target = nullptr;
  94. // 5. Set event’s type attribute to type.
  95. m_type = type;
  96. // 6. Set event’s bubbles attribute to bubbles.
  97. m_bubbles = bubbles;
  98. // 8. Set event’s cancelable attribute to cancelable.
  99. m_cancelable = cancelable;
  100. }
  101. // https://dom.spec.whatwg.org/#dom-event-initevent
  102. void Event::init_event(String const& type, bool bubbles, bool cancelable)
  103. {
  104. // 1. If this’s dispatch flag is set, then return.
  105. if (m_dispatch)
  106. return;
  107. // 2. Initialize this with type, bubbles, and cancelable.
  108. initialize_event(type, bubbles, cancelable);
  109. }
  110. // https://dom.spec.whatwg.org/#dom-event-timestamp
  111. double Event::time_stamp() const
  112. {
  113. return m_time_stamp;
  114. }
  115. // https://dom.spec.whatwg.org/#dom-event-composedpath
  116. Vector<JS::Handle<EventTarget>> Event::composed_path() const
  117. {
  118. // 1. Let composedPath be an empty list.
  119. Vector<JS::Handle<EventTarget>> composed_path;
  120. // 2. Let path be this’s path. (NOTE: Not necessary)
  121. // 3. If path is empty, then return composedPath.
  122. if (m_path.is_empty())
  123. return composed_path;
  124. // 4. Let currentTarget be this’s currentTarget attribute value. (NOTE: Not necessary)
  125. // 5. Append currentTarget to composedPath.
  126. // NOTE: If path is not empty, then the event is being dispatched and will have a currentTarget.
  127. VERIFY(m_current_target);
  128. composed_path.append(const_cast<EventTarget*>(m_current_target.ptr()));
  129. // 6. Let currentTargetIndex be 0.
  130. size_t current_target_index = 0;
  131. // 7. Let currentTargetHiddenSubtreeLevel be 0.
  132. size_t current_target_hidden_subtree_level = 0;
  133. // 8. Let index be path’s size − 1.
  134. // 9. While index is greater than or equal to 0:
  135. for (ssize_t index = m_path.size() - 1; index >= 0; --index) {
  136. auto& path_entry = m_path.at(index);
  137. // 1. If path[index]'s root-of-closed-tree is true, then increase currentTargetHiddenSubtreeLevel by 1.
  138. if (path_entry.root_of_closed_tree)
  139. ++current_target_hidden_subtree_level;
  140. // 2. If path[index]'s invocation target is currentTarget, then set currentTargetIndex to index and break.
  141. if (path_entry.invocation_target == m_current_target) {
  142. current_target_index = index;
  143. break;
  144. }
  145. // 3. If path[index]'s slot-in-closed-tree is true, then decrease currentTargetHiddenSubtreeLevel by 1.
  146. if (path_entry.slot_in_closed_tree)
  147. --current_target_hidden_subtree_level;
  148. // 4. Decrease index by 1.
  149. }
  150. // 10. Let currentHiddenLevel and maxHiddenLevel be currentTargetHiddenSubtreeLevel.
  151. size_t current_hidden_level = current_target_hidden_subtree_level;
  152. size_t max_hidden_level = current_target_hidden_subtree_level;
  153. // 11. Set index to currentTargetIndex − 1.
  154. // 12. While index is greater than or equal to 0:
  155. for (ssize_t index = current_target_index - 1; index >= 0; --index) {
  156. auto& path_entry = m_path.at(index);
  157. // 1. If path[index]'s root-of-closed-tree is true, then increase currentHiddenLevel by 1.
  158. if (path_entry.root_of_closed_tree)
  159. ++current_hidden_level;
  160. // 2. If currentHiddenLevel is less than or equal to maxHiddenLevel, then prepend path[index]'s invocation target to composedPath.
  161. if (current_hidden_level <= max_hidden_level) {
  162. VERIFY(path_entry.invocation_target);
  163. composed_path.prepend(const_cast<EventTarget*>(path_entry.invocation_target.ptr()));
  164. }
  165. // 3. If path[index]'s slot-in-closed-tree is true, then:
  166. if (path_entry.slot_in_closed_tree) {
  167. // 1. Decrease currentHiddenLevel by 1.
  168. --current_hidden_level;
  169. // 2. If currentHiddenLevel is less than maxHiddenLevel, then set maxHiddenLevel to currentHiddenLevel.
  170. if (current_hidden_level < max_hidden_level)
  171. max_hidden_level = current_hidden_level;
  172. }
  173. // 4. Decrease index by 1.
  174. }
  175. // 13. Set currentHiddenLevel and maxHiddenLevel to currentTargetHiddenSubtreeLevel.
  176. current_hidden_level = current_target_hidden_subtree_level;
  177. max_hidden_level = current_target_hidden_subtree_level;
  178. // 14. Set index to currentTargetIndex + 1.
  179. // 15. While index is less than path’s size:
  180. for (size_t index = current_target_index + 1; index < m_path.size(); ++index) {
  181. auto& path_entry = m_path.at(index);
  182. // 1. If path[index]'s slot-in-closed-tree is true, then increase currentHiddenLevel by 1.
  183. if (path_entry.slot_in_closed_tree)
  184. ++current_hidden_level;
  185. // 2. If currentHiddenLevel is less than or equal to maxHiddenLevel, then append path[index]'s invocation target to composedPath.
  186. if (current_hidden_level <= max_hidden_level) {
  187. VERIFY(path_entry.invocation_target);
  188. composed_path.append(const_cast<EventTarget*>(path_entry.invocation_target.ptr()));
  189. }
  190. // 3. If path[index]'s root-of-closed-tree is true, then:
  191. if (path_entry.root_of_closed_tree) {
  192. // 1. Decrease currentHiddenLevel by 1.
  193. --current_hidden_level;
  194. // 2. If currentHiddenLevel is less than maxHiddenLevel, then set maxHiddenLevel to currentHiddenLevel.
  195. if (current_hidden_level < max_hidden_level)
  196. max_hidden_level = current_hidden_level;
  197. }
  198. // 4. Increase index by 1.
  199. }
  200. // 16. Return composedPath.
  201. return composed_path;
  202. }
  203. }