Event.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.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. // https://dom.spec.whatwg.org/#concept-event-create
  18. JS::NonnullGCPtr<Event> Event::create(JS::Realm& realm, FlyString const& event_name, EventInit const& event_init)
  19. {
  20. auto event = realm.heap().allocate<Event>(realm, realm, event_name, event_init);
  21. // 4. Initialize event’s isTrusted attribute to true.
  22. event->m_is_trusted = true;
  23. return event;
  24. }
  25. WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Event::construct_impl(JS::Realm& realm, FlyString const& event_name, EventInit const& event_init)
  26. {
  27. return realm.heap().allocate<Event>(realm, realm, event_name, event_init);
  28. }
  29. // https://dom.spec.whatwg.org/#inner-event-creation-steps
  30. Event::Event(JS::Realm& realm, FlyString const& type)
  31. : PlatformObject(realm)
  32. , m_type(type)
  33. , m_initialized(true)
  34. , m_time_stamp(HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*this)))
  35. {
  36. }
  37. // https://dom.spec.whatwg.org/#inner-event-creation-steps
  38. Event::Event(JS::Realm& realm, FlyString const& type, EventInit const& event_init)
  39. : PlatformObject(realm)
  40. , m_type(type)
  41. , m_bubbles(event_init.bubbles)
  42. , m_cancelable(event_init.cancelable)
  43. , m_composed(event_init.composed)
  44. , m_initialized(true)
  45. , m_time_stamp(HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*this)))
  46. {
  47. }
  48. void Event::initialize(JS::Realm& realm)
  49. {
  50. Base::initialize(realm);
  51. WEB_SET_PROTOTYPE_FOR_INTERFACE(Event);
  52. }
  53. void Event::visit_edges(Visitor& visitor)
  54. {
  55. Base::visit_edges(visitor);
  56. visitor.visit(m_target);
  57. visitor.visit(m_related_target);
  58. visitor.visit(m_current_target);
  59. for (auto& it : m_path) {
  60. visitor.visit(it.invocation_target);
  61. visitor.visit(it.shadow_adjusted_target);
  62. visitor.visit(it.related_target);
  63. visitor.visit(it.touch_target_list);
  64. }
  65. visitor.visit(m_touch_target_list);
  66. }
  67. // https://dom.spec.whatwg.org/#concept-event-path-append
  68. 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)
  69. {
  70. // 1. Let invocationTargetInShadowTree be false.
  71. bool invocation_target_in_shadow_tree = false;
  72. // 3. Let root-of-closed-tree be false.
  73. bool root_of_closed_tree = false;
  74. // 2. If invocationTarget is a node and its root is a shadow root, then set invocationTargetInShadowTree to true.
  75. if (is<Node>(invocation_target)) {
  76. auto& invocation_target_node = verify_cast<Node>(invocation_target);
  77. if (is<ShadowRoot>(invocation_target_node.root()))
  78. invocation_target_in_shadow_tree = true;
  79. if (is<ShadowRoot>(invocation_target_node)) {
  80. auto& invocation_target_shadow_root = verify_cast<ShadowRoot>(invocation_target_node);
  81. // 4. If invocationTarget is a shadow root whose mode is "closed", then set root-of-closed-tree to true.
  82. root_of_closed_tree = invocation_target_shadow_root.mode() == Bindings::ShadowRootMode::Closed;
  83. }
  84. }
  85. // 5. Append a new struct to event’s path whose invocation target is invocationTarget, invocation-target-in-shadow-tree is invocationTargetInShadowTree,
  86. // shadow-adjusted target is shadowAdjustedTarget, relatedTarget is relatedTarget, touch target list is touchTargets, root-of-closed-tree is root-of-closed-tree,
  87. // and slot-in-closed-tree is slot-in-closed-tree.
  88. 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() });
  89. }
  90. void Event::set_cancelled_flag()
  91. {
  92. if (m_cancelable && !m_in_passive_listener)
  93. m_cancelled = true;
  94. }
  95. // https://dom.spec.whatwg.org/#concept-event-initialize
  96. void Event::initialize_event(String const& type, bool bubbles, bool cancelable)
  97. {
  98. // 1. Set event’s initialized flag.
  99. m_initialized = true;
  100. // 2. Unset event’s stop propagation flag, stop immediate propagation flag, and canceled flag.
  101. m_stop_propagation = false;
  102. m_stop_immediate_propagation = false;
  103. m_cancelled = false;
  104. // 3. Set event’s isTrusted attribute to false.
  105. m_is_trusted = false;
  106. // 4. Set event’s target to null.
  107. m_target = nullptr;
  108. // 5. Set event’s type attribute to type.
  109. m_type = type;
  110. // 6. Set event’s bubbles attribute to bubbles.
  111. m_bubbles = bubbles;
  112. // 8. Set event’s cancelable attribute to cancelable.
  113. m_cancelable = cancelable;
  114. }
  115. // https://dom.spec.whatwg.org/#dom-event-initevent
  116. void Event::init_event(String const& type, bool bubbles, bool cancelable)
  117. {
  118. // 1. If this’s dispatch flag is set, then return.
  119. if (m_dispatch)
  120. return;
  121. // 2. Initialize this with type, bubbles, and cancelable.
  122. initialize_event(type, bubbles, cancelable);
  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. }