Event.cpp 9.9 KB

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