Event.cpp 10.0 KB

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