EventDispatcher.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/FunctionObject.h>
  10. #include <LibWeb/Bindings/EventTargetWrapper.h>
  11. #include <LibWeb/Bindings/EventTargetWrapperFactory.h>
  12. #include <LibWeb/Bindings/EventWrapper.h>
  13. #include <LibWeb/Bindings/EventWrapperFactory.h>
  14. #include <LibWeb/Bindings/IDLAbstractOperations.h>
  15. #include <LibWeb/Bindings/WindowObject.h>
  16. #include <LibWeb/DOM/AbortSignal.h>
  17. #include <LibWeb/DOM/Document.h>
  18. #include <LibWeb/DOM/Event.h>
  19. #include <LibWeb/DOM/EventDispatcher.h>
  20. #include <LibWeb/DOM/EventTarget.h>
  21. #include <LibWeb/DOM/IDLEventListener.h>
  22. #include <LibWeb/DOM/Node.h>
  23. #include <LibWeb/DOM/ShadowRoot.h>
  24. #include <LibWeb/HTML/EventNames.h>
  25. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  26. #include <LibWeb/HTML/Window.h>
  27. #include <LibWeb/UIEvents/MouseEvent.h>
  28. namespace Web::DOM {
  29. // FIXME: This shouldn't be here, as retargeting is not only used by the event dispatcher.
  30. // When moving this function, it needs to be generalized. https://dom.spec.whatwg.org/#retarget
  31. static EventTarget* retarget(EventTarget* left, EventTarget* right)
  32. {
  33. for (;;) {
  34. if (!is<Node>(left))
  35. return left;
  36. auto* left_node = verify_cast<Node>(left);
  37. auto& left_root = left_node->root();
  38. if (!is<ShadowRoot>(left_root))
  39. return left;
  40. if (is<Node>(right) && left_root.is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*right)))
  41. return left;
  42. auto& left_shadow_root = verify_cast<ShadowRoot>(left_root);
  43. left = left_shadow_root.host();
  44. }
  45. }
  46. // https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
  47. bool EventDispatcher::inner_invoke(Event& event, Vector<NonnullRefPtr<DOM::DOMEventListener>>& listeners, Event::Phase phase, bool invocation_target_in_shadow_tree)
  48. {
  49. // 1. Let found be false.
  50. bool found = false;
  51. // 2. For each listener in listeners, whose removed is false:
  52. for (auto& listener : listeners) {
  53. if (listener->removed)
  54. continue;
  55. // 1. If event’s type attribute value is not listener’s type, then continue.
  56. if (event.type() != listener->type)
  57. continue;
  58. // 2. Set found to true.
  59. found = true;
  60. // 3. If phase is "capturing" and listener’s capture is false, then continue.
  61. if (phase == Event::Phase::CapturingPhase && !listener->capture)
  62. continue;
  63. // 4. If phase is "bubbling" and listener’s capture is true, then continue.
  64. if (phase == Event::Phase::BubblingPhase && listener->capture)
  65. continue;
  66. // 5. If listener’s once is true, then remove listener from event’s currentTarget attribute value’s event listener list.
  67. if (listener->once)
  68. event.current_target()->remove_from_event_listener_list(listener);
  69. // 6. Let global be listener callback’s associated Realm’s global object.
  70. auto& callback = listener->callback->callback();
  71. auto& global = callback.callback.cell()->global_object();
  72. // 7. Let currentEvent be undefined.
  73. RefPtr<Event> current_event;
  74. // 8. If global is a Window object, then:
  75. if (is<Bindings::WindowObject>(global)) {
  76. auto& bindings_window_global = verify_cast<Bindings::WindowObject>(global);
  77. auto& window_impl = bindings_window_global.impl();
  78. // 1. Set currentEvent to global’s current event.
  79. current_event = window_impl.current_event();
  80. // 2. If invocationTargetInShadowTree is false, then set global’s current event to event.
  81. if (!invocation_target_in_shadow_tree)
  82. window_impl.set_current_event(&event);
  83. }
  84. // 9. If listener’s passive is true, then set event’s in passive listener flag.
  85. if (listener->passive)
  86. event.set_in_passive_listener(true);
  87. // 10. Call a user object’s operation with listener’s callback, "handleEvent", « event », and event’s currentTarget attribute value. If this throws an exception, then:
  88. // FIXME: These should be wrapped for us in call_user_object_operation, but it currently doesn't do that.
  89. auto* this_value = Bindings::wrap(global, *event.current_target());
  90. auto* wrapped_event = Bindings::wrap(global, event);
  91. auto result = Bindings::IDL::call_user_object_operation(callback, "handleEvent", this_value, wrapped_event);
  92. // If this throws an exception, then:
  93. if (result.is_error()) {
  94. // 1. Report the exception.
  95. HTML::report_exception(result);
  96. // FIXME: 2. Set legacyOutputDidListenersThrowFlag if given. (Only used by IndexedDB currently)
  97. }
  98. // 11. Unset event’s in passive listener flag.
  99. event.set_in_passive_listener(false);
  100. // 12. If global is a Window object, then set global’s current event to currentEvent.
  101. if (is<Bindings::WindowObject>(global)) {
  102. auto& bindings_window_global = verify_cast<Bindings::WindowObject>(global);
  103. auto& window_impl = bindings_window_global.impl();
  104. window_impl.set_current_event(current_event);
  105. }
  106. // 13. If event’s stop immediate propagation flag is set, then return found.
  107. if (event.should_stop_immediate_propagation())
  108. return found;
  109. }
  110. // 3. Return found.
  111. return found;
  112. }
  113. // https://dom.spec.whatwg.org/#concept-event-listener-invoke
  114. void EventDispatcher::invoke(Event::PathEntry& struct_, Event& event, Event::Phase phase)
  115. {
  116. auto last_valid_shadow_adjusted_target = event.path().last_matching([&struct_](auto& entry) {
  117. return entry.index <= struct_.index && !entry.shadow_adjusted_target.is_null();
  118. });
  119. VERIFY(last_valid_shadow_adjusted_target.has_value());
  120. event.set_target(last_valid_shadow_adjusted_target.value().shadow_adjusted_target);
  121. event.set_related_target(struct_.related_target);
  122. event.set_touch_target_list(struct_.touch_target_list);
  123. if (event.should_stop_propagation())
  124. return;
  125. event.set_current_target(struct_.invocation_target);
  126. // NOTE: This is an intentional copy. Any event listeners added after this point will not be invoked.
  127. auto listeners = event.current_target()->event_listener_list();
  128. bool invocation_target_in_shadow_tree = struct_.invocation_target_in_shadow_tree;
  129. bool found = inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);
  130. if (!found && event.is_trusted()) {
  131. auto original_event_type = event.type();
  132. if (event.type() == "animationend")
  133. event.set_type("webkitAnimationEnd");
  134. else if (event.type() == "animationiteration")
  135. event.set_type("webkitAnimationIteration");
  136. else if (event.type() == "animationstart")
  137. event.set_type("webkitAnimationStart");
  138. else if (event.type() == "transitionend")
  139. event.set_type("webkitTransitionEnd");
  140. else
  141. return;
  142. inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);
  143. event.set_type(original_event_type);
  144. }
  145. }
  146. // https://dom.spec.whatwg.org/#concept-event-dispatch
  147. bool EventDispatcher::dispatch(NonnullRefPtr<EventTarget> target, NonnullRefPtr<Event> event, bool legacy_target_override)
  148. {
  149. event->set_dispatched(true);
  150. RefPtr<EventTarget> target_override;
  151. if (!legacy_target_override) {
  152. target_override = target;
  153. } else {
  154. // NOTE: This can be done because legacy_target_override is only set for events targeted at Window.
  155. target_override = verify_cast<HTML::Window>(*target).associated_document();
  156. }
  157. RefPtr<EventTarget> activation_target;
  158. RefPtr<EventTarget> related_target = retarget(event->related_target(), target);
  159. bool clear_targets = false;
  160. if (related_target != target || event->related_target() == target) {
  161. Event::TouchTargetList touch_targets;
  162. for (auto& touch_target : event->touch_target_list()) {
  163. touch_targets.append(retarget(touch_target, target));
  164. }
  165. event->append_to_path(*target, target_override, related_target, touch_targets, false);
  166. bool is_activation_event = is<UIEvents::MouseEvent>(*event) && event->type() == HTML::EventNames::click;
  167. if (is_activation_event && target->activation_behavior)
  168. activation_target = target;
  169. // FIXME: Let slottable be target, if target is a slottable and is assigned, and null otherwise.
  170. bool slot_in_closed_tree = false;
  171. auto* parent = target->get_parent(event);
  172. while (parent) {
  173. // FIXME: If slottable is non-null:
  174. // FIXME: If parent is a slottable and is assigned, then set slottable to parent.
  175. related_target = retarget(event->related_target(), parent);
  176. touch_targets.clear();
  177. for (auto& touch_target : event->touch_target_list()) {
  178. touch_targets.append(retarget(touch_target, parent));
  179. }
  180. if (is<HTML::Window>(parent)
  181. || (is<Node>(parent) && verify_cast<Node>(*target).root().is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*parent)))) {
  182. if (is_activation_event && event->bubbles() && !activation_target && parent->activation_behavior)
  183. activation_target = parent;
  184. event->append_to_path(*parent, nullptr, related_target, touch_targets, slot_in_closed_tree);
  185. } else if (related_target == parent) {
  186. parent = nullptr;
  187. } else {
  188. target = *parent;
  189. if (is_activation_event && !activation_target && target->activation_behavior)
  190. activation_target = target;
  191. event->append_to_path(*parent, target, related_target, touch_targets, slot_in_closed_tree);
  192. }
  193. if (parent) {
  194. parent = parent->get_parent(event);
  195. }
  196. slot_in_closed_tree = false;
  197. }
  198. auto clear_targets_struct = event->path().last_matching([](auto& entry) {
  199. return !entry.shadow_adjusted_target.is_null();
  200. });
  201. VERIFY(clear_targets_struct.has_value());
  202. if (is<Node>(clear_targets_struct.value().shadow_adjusted_target.ptr())) {
  203. auto& shadow_adjusted_target_node = verify_cast<Node>(*clear_targets_struct.value().shadow_adjusted_target);
  204. if (is<ShadowRoot>(shadow_adjusted_target_node.root()))
  205. clear_targets = true;
  206. }
  207. if (!clear_targets && is<Node>(clear_targets_struct.value().related_target.ptr())) {
  208. auto& related_target_node = verify_cast<Node>(*clear_targets_struct.value().related_target);
  209. if (is<ShadowRoot>(related_target_node.root()))
  210. clear_targets = true;
  211. }
  212. if (!clear_targets) {
  213. for (auto touch_target : clear_targets_struct.value().touch_target_list) {
  214. if (is<Node>(*touch_target.ptr())) {
  215. auto& touch_target_node = verify_cast<Node>(*touch_target.ptr());
  216. if (is<ShadowRoot>(touch_target_node.root())) {
  217. clear_targets = true;
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. if (activation_target)
  224. activation_target->legacy_pre_activation_behavior();
  225. for (ssize_t i = event->path().size() - 1; i >= 0; --i) {
  226. auto& entry = event->path().at(i);
  227. if (entry.shadow_adjusted_target)
  228. event->set_phase(Event::Phase::AtTarget);
  229. else
  230. event->set_phase(Event::Phase::CapturingPhase);
  231. invoke(entry, event, Event::Phase::CapturingPhase);
  232. }
  233. for (auto& entry : event->path()) {
  234. if (entry.shadow_adjusted_target) {
  235. event->set_phase(Event::Phase::AtTarget);
  236. } else {
  237. if (!event->bubbles())
  238. continue;
  239. event->set_phase(Event::Phase::BubblingPhase);
  240. }
  241. invoke(entry, event, Event::Phase::BubblingPhase);
  242. }
  243. }
  244. event->set_phase(Event::Phase::None);
  245. event->set_current_target(nullptr);
  246. event->clear_path();
  247. event->set_dispatched(false);
  248. event->set_stop_propagation(false);
  249. event->set_stop_immediate_propagation(false);
  250. if (clear_targets) {
  251. event->set_target(nullptr);
  252. event->set_related_target(nullptr);
  253. event->clear_touch_target_list();
  254. }
  255. if (activation_target) {
  256. if (!event->cancelled()) {
  257. // NOTE: Since activation_target is set, it will have activation behavior.
  258. activation_target->activation_behavior(event);
  259. activation_target->legacy_cancelled_activation_behavior_was_not_called();
  260. } else {
  261. activation_target->legacy_cancelled_activation_behavior();
  262. }
  263. }
  264. return !event->cancelled();
  265. }
  266. }