Event.cpp 9.8 KB

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