Event.cpp 9.9 KB

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