EventDispatcher.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/TypeCasts.h>
  28. #include <LibJS/Runtime/Function.h>
  29. #include <LibWeb/Bindings/EventTargetWrapper.h>
  30. #include <LibWeb/Bindings/EventTargetWrapperFactory.h>
  31. #include <LibWeb/Bindings/EventWrapper.h>
  32. #include <LibWeb/Bindings/EventWrapperFactory.h>
  33. #include <LibWeb/Bindings/ScriptExecutionContext.h>
  34. #include <LibWeb/Bindings/WindowObject.h>
  35. #include <LibWeb/DOM/Document.h>
  36. #include <LibWeb/DOM/Event.h>
  37. #include <LibWeb/DOM/EventDispatcher.h>
  38. #include <LibWeb/DOM/EventListener.h>
  39. #include <LibWeb/DOM/EventTarget.h>
  40. #include <LibWeb/DOM/Node.h>
  41. #include <LibWeb/DOM/ShadowRoot.h>
  42. #include <LibWeb/DOM/Window.h>
  43. #include <LibWeb/HTML/EventNames.h>
  44. #include <LibWeb/UIEvents/MouseEvent.h>
  45. namespace Web::DOM {
  46. // FIXME: This shouldn't be here, as retargeting is not only used by the event dispatcher.
  47. // When moving this function, it needs to be generalized. https://dom.spec.whatwg.org/#retarget
  48. static EventTarget* retarget(EventTarget* left, [[maybe_unused]] EventTarget* right)
  49. {
  50. // FIXME
  51. for (;;) {
  52. if (!is<Node>(left))
  53. return left;
  54. auto* left_node = downcast<Node>(left);
  55. auto* left_root = left_node->root();
  56. if (!is<ShadowRoot>(left_root))
  57. return left;
  58. // FIXME: If right is a node and left’s root is a shadow-including inclusive ancestor of right, return left.
  59. auto* left_shadow_root = downcast<ShadowRoot>(left_root);
  60. left = left_shadow_root->host();
  61. }
  62. }
  63. // https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
  64. bool EventDispatcher::inner_invoke(Event& event, Vector<EventTarget::EventListenerRegistration>& listeners, Event::Phase phase, bool invocation_target_in_shadow_tree)
  65. {
  66. bool found = false;
  67. for (auto& listener : listeners) {
  68. if (listener.listener->removed())
  69. continue;
  70. if (event.type() != listener.listener->type())
  71. continue;
  72. found = true;
  73. if (phase == Event::Phase::CapturingPhase && !listener.listener->capture())
  74. continue;
  75. if (phase == Event::Phase::BubblingPhase && listener.listener->capture())
  76. continue;
  77. if (listener.listener->once())
  78. event.current_target()->remove_from_event_listener_list(listener.listener);
  79. auto& function = listener.listener->function();
  80. auto& global = function.global_object();
  81. RefPtr<Event> current_event;
  82. if (is<Bindings::WindowObject>(global)) {
  83. auto& bindings_window_global = downcast<Bindings::WindowObject>(global);
  84. auto& window_impl = bindings_window_global.impl();
  85. current_event = window_impl.current_event();
  86. if (!invocation_target_in_shadow_tree)
  87. window_impl.set_current_event(&event);
  88. }
  89. if (listener.listener->passive())
  90. event.set_in_passive_listener(true);
  91. auto* this_value = Bindings::wrap(global, *event.current_target());
  92. auto* wrapped_event = Bindings::wrap(global, event);
  93. auto& vm = global.vm();
  94. [[maybe_unused]] auto rc = vm.call(listener.listener->function(), this_value, wrapped_event);
  95. if (vm.exception()) {
  96. vm.clear_exception();
  97. // FIXME: Set legacyOutputDidListenersThrowFlag if given. (Only used by IndexedDB currently)
  98. }
  99. vm.run_queued_promise_jobs();
  100. VERIFY(!vm.exception());
  101. event.set_in_passive_listener(false);
  102. if (is<Bindings::WindowObject>(global)) {
  103. auto& bindings_window_global = downcast<Bindings::WindowObject>(global);
  104. auto& window_impl = bindings_window_global.impl();
  105. window_impl.set_current_event(current_event);
  106. }
  107. if (event.should_stop_immediate_propagation())
  108. return found;
  109. }
  110. return found;
  111. }
  112. // https://dom.spec.whatwg.org/#concept-event-listener-invoke
  113. void EventDispatcher::invoke(Event::PathEntry& struct_, Event& event, Event::Phase phase)
  114. {
  115. auto last_valid_shadow_adjusted_target = event.path().last_matching([&struct_](auto& entry) {
  116. return entry.index <= struct_.index && !entry.shadow_adjusted_target.is_null();
  117. });
  118. VERIFY(last_valid_shadow_adjusted_target.has_value());
  119. event.set_target(last_valid_shadow_adjusted_target.value().shadow_adjusted_target);
  120. event.set_related_target(struct_.related_target);
  121. event.set_touch_target_list(struct_.touch_target_list);
  122. if (event.should_stop_propagation())
  123. return;
  124. event.set_current_target(struct_.invocation_target);
  125. // NOTE: This is an intentional copy. Any event listeners added after this point will not be invoked.
  126. auto listeners = event.current_target()->listeners();
  127. bool invocation_target_in_shadow_tree = struct_.invocation_target_in_shadow_tree;
  128. bool found = inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);
  129. if (!found && event.is_trusted()) {
  130. auto original_event_type = event.type();
  131. if (event.type() == "animationend")
  132. event.set_type("webkitAnimationEnd");
  133. else if (event.type() == "animationiteration")
  134. event.set_type("webkitAnimationIteration");
  135. else if (event.type() == "animationstart")
  136. event.set_type("webkitAnimationStart");
  137. else if (event.type() == "transitionend")
  138. event.set_type("webkitTransitionEnd");
  139. else
  140. return;
  141. inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);
  142. event.set_type(original_event_type);
  143. }
  144. }
  145. // https://dom.spec.whatwg.org/#concept-event-dispatch
  146. bool EventDispatcher::dispatch(NonnullRefPtr<EventTarget> target, NonnullRefPtr<Event> event, bool legacy_target_override)
  147. {
  148. event->set_dispatched(true);
  149. RefPtr<EventTarget> target_override;
  150. if (!legacy_target_override) {
  151. target_override = target;
  152. } else {
  153. // NOTE: This can be done because legacy_target_override is only set for events targeted at Window.
  154. target_override = downcast<Window>(*target).document();
  155. }
  156. RefPtr<EventTarget> activation_target;
  157. RefPtr<EventTarget> related_target = retarget(event->related_target(), target);
  158. bool clear_targets = false;
  159. if (related_target != target || event->related_target() == target) {
  160. Event::TouchTargetList touch_targets;
  161. for (auto& touch_target : event->touch_target_list()) {
  162. touch_targets.append(retarget(touch_target, target));
  163. }
  164. event->append_to_path(*target, target_override, related_target, touch_targets, false);
  165. bool is_activation_event = is<UIEvents::MouseEvent>(*event) && event->type() == HTML::EventNames::click;
  166. if (is_activation_event && target->activation_behaviour)
  167. activation_target = target;
  168. // FIXME: Let slottable be target, if target is a slottable and is assigned, and null otherwise.
  169. bool slot_in_closed_tree = false;
  170. auto* parent = target->get_parent(event);
  171. while (parent) {
  172. // FIXME: If slottable is non-null:
  173. // FIXME: If parent is a slottable and is assigned, then set slottable to parent.
  174. related_target = retarget(event->related_target(), parent);
  175. touch_targets.clear();
  176. for (auto& touch_target : event->touch_target_list()) {
  177. touch_targets.append(retarget(touch_target, parent));
  178. }
  179. // FIXME: or parent is a node and target’s root is a shadow-including inclusive ancestor of parent, then:
  180. if (is<Window>(parent)) {
  181. if (is_activation_event && event->bubbles() && !activation_target && parent->activation_behaviour)
  182. activation_target = parent;
  183. event->append_to_path(*parent, nullptr, related_target, touch_targets, slot_in_closed_tree);
  184. } else if (related_target == parent) {
  185. parent = nullptr;
  186. } else {
  187. target = *parent;
  188. if (is_activation_event && !activation_target && target->activation_behaviour)
  189. activation_target = target;
  190. event->append_to_path(*parent, target, related_target, touch_targets, slot_in_closed_tree);
  191. }
  192. if (parent) {
  193. parent = parent->get_parent(event);
  194. }
  195. slot_in_closed_tree = false;
  196. }
  197. auto clear_targets_struct = event->path().last_matching([](auto& entry) {
  198. return !entry.shadow_adjusted_target.is_null();
  199. });
  200. VERIFY(clear_targets_struct.has_value());
  201. if (is<Node>(clear_targets_struct.value().shadow_adjusted_target.ptr())) {
  202. auto& shadow_adjusted_target_node = downcast<Node>(*clear_targets_struct.value().shadow_adjusted_target);
  203. if (is<ShadowRoot>(shadow_adjusted_target_node.root()))
  204. clear_targets = true;
  205. }
  206. if (!clear_targets && is<Node>(clear_targets_struct.value().related_target.ptr())) {
  207. auto& related_target_node = downcast<Node>(*clear_targets_struct.value().related_target);
  208. if (is<ShadowRoot>(related_target_node.root()))
  209. clear_targets = true;
  210. }
  211. if (!clear_targets) {
  212. for (auto touch_target : clear_targets_struct.value().touch_target_list) {
  213. if (is<Node>(*touch_target.ptr())) {
  214. auto& touch_target_node = downcast<Node>(*touch_target.ptr());
  215. if (is<ShadowRoot>(touch_target_node.root())) {
  216. clear_targets = true;
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. if (activation_target && activation_target->legacy_pre_activation_behaviour)
  223. activation_target->legacy_pre_activation_behaviour();
  224. for (ssize_t i = event->path().size() - 1; i >= 0; --i) {
  225. auto& entry = event->path().at(i);
  226. if (entry.shadow_adjusted_target)
  227. event->set_phase(Event::Phase::AtTarget);
  228. else
  229. event->set_phase(Event::Phase::CapturingPhase);
  230. invoke(entry, event, Event::Phase::CapturingPhase);
  231. }
  232. for (auto& entry : event->path()) {
  233. if (entry.shadow_adjusted_target) {
  234. event->set_phase(Event::Phase::AtTarget);
  235. } else {
  236. if (!event->bubbles())
  237. continue;
  238. event->set_phase(Event::Phase::BubblingPhase);
  239. }
  240. invoke(entry, event, Event::Phase::BubblingPhase);
  241. }
  242. }
  243. event->set_phase(Event::Phase::None);
  244. event->set_current_target(nullptr);
  245. event->clear_path();
  246. event->set_dispatched(false);
  247. event->set_stop_propagation(false);
  248. event->set_stop_immediate_propagation(false);
  249. if (clear_targets) {
  250. event->set_target(nullptr);
  251. event->set_related_target(nullptr);
  252. event->clear_touch_target_list();
  253. }
  254. if (activation_target) {
  255. if (!event->cancelled()) {
  256. // NOTE: Since activation_target is set, it will have activation behaviour.
  257. activation_target->activation_behaviour(event);
  258. } else {
  259. if (activation_target->legacy_cancelled_activation_behaviour)
  260. activation_target->legacy_cancelled_activation_behaviour();
  261. }
  262. }
  263. return !event->cancelled();
  264. }
  265. }