Event.cpp 9.6 KB

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