EventDispatcher.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. // To retarget an object A against an object B, repeat these steps until they return an object:
  34. for (;;) {
  35. // 1. If one of the following is true then return A.
  36. // - A is not a node
  37. if (!is<Node>(left))
  38. return left;
  39. // - A’s root is not a shadow root
  40. auto* left_node = verify_cast<Node>(left);
  41. auto& left_root = left_node->root();
  42. if (!is<ShadowRoot>(left_root))
  43. return left;
  44. // - B is a node and A’s root is a shadow-including inclusive ancestor of B
  45. if (is<Node>(right) && left_root.is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*right)))
  46. return left;
  47. // 2. Set A to A’s root’s host.
  48. auto& left_shadow_root = verify_cast<ShadowRoot>(left_root);
  49. left = left_shadow_root.host();
  50. }
  51. }
  52. // https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
  53. bool EventDispatcher::inner_invoke(Event& event, Vector<NonnullRefPtr<DOM::DOMEventListener>>& listeners, Event::Phase phase, bool invocation_target_in_shadow_tree)
  54. {
  55. // 1. Let found be false.
  56. bool found = false;
  57. // 2. For each listener in listeners, whose removed is false:
  58. for (auto& listener : listeners) {
  59. if (listener->removed)
  60. continue;
  61. // 1. If event’s type attribute value is not listener’s type, then continue.
  62. if (event.type() != listener->type)
  63. continue;
  64. // 2. Set found to true.
  65. found = true;
  66. // 3. If phase is "capturing" and listener’s capture is false, then continue.
  67. if (phase == Event::Phase::CapturingPhase && !listener->capture)
  68. continue;
  69. // 4. If phase is "bubbling" and listener’s capture is true, then continue.
  70. if (phase == Event::Phase::BubblingPhase && listener->capture)
  71. continue;
  72. // 5. If listener’s once is true, then remove listener from event’s currentTarget attribute value’s event listener list.
  73. if (listener->once)
  74. event.current_target()->remove_from_event_listener_list(listener);
  75. // 6. Let global be listener callback’s associated Realm’s global object.
  76. auto& callback = listener->callback->callback();
  77. auto& realm = callback.callback->shape().realm();
  78. auto& global = realm.global_object();
  79. // 7. Let currentEvent be undefined.
  80. RefPtr<Event> current_event;
  81. // 8. If global is a Window object, then:
  82. if (is<Bindings::WindowObject>(global)) {
  83. auto& bindings_window_global = verify_cast<Bindings::WindowObject>(global);
  84. auto& window_impl = bindings_window_global.impl();
  85. // 1. Set currentEvent to global’s current event.
  86. current_event = window_impl.current_event();
  87. // 2. If invocationTargetInShadowTree is false, then set global’s current event to event.
  88. if (!invocation_target_in_shadow_tree)
  89. window_impl.set_current_event(&event);
  90. }
  91. // 9. If listener’s passive is true, then set event’s in passive listener flag.
  92. if (listener->passive)
  93. event.set_in_passive_listener(true);
  94. // 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:
  95. // FIXME: These should be wrapped for us in call_user_object_operation, but it currently doesn't do that.
  96. auto* this_value = Bindings::wrap(realm, *event.current_target());
  97. auto* wrapped_event = Bindings::wrap(realm, event);
  98. auto result = Bindings::IDL::call_user_object_operation(callback, "handleEvent", this_value, wrapped_event);
  99. // If this throws an exception, then:
  100. if (result.is_error()) {
  101. // 1. Report the exception.
  102. HTML::report_exception(result);
  103. // FIXME: 2. Set legacyOutputDidListenersThrowFlag if given. (Only used by IndexedDB currently)
  104. }
  105. // 11. Unset event’s in passive listener flag.
  106. event.set_in_passive_listener(false);
  107. // 12. If global is a Window object, then set global’s current event to currentEvent.
  108. if (is<Bindings::WindowObject>(global)) {
  109. auto& bindings_window_global = verify_cast<Bindings::WindowObject>(global);
  110. auto& window_impl = bindings_window_global.impl();
  111. window_impl.set_current_event(current_event);
  112. }
  113. // 13. If event’s stop immediate propagation flag is set, then return found.
  114. if (event.should_stop_immediate_propagation())
  115. return found;
  116. }
  117. // 3. Return found.
  118. return found;
  119. }
  120. // https://dom.spec.whatwg.org/#concept-event-listener-invoke
  121. void EventDispatcher::invoke(Event::PathEntry& struct_, Event& event, Event::Phase phase)
  122. {
  123. auto last_valid_shadow_adjusted_target = event.path().last_matching([&struct_](auto& entry) {
  124. return entry.index <= struct_.index && !entry.shadow_adjusted_target.is_null();
  125. });
  126. VERIFY(last_valid_shadow_adjusted_target.has_value());
  127. // 1. Set event’s target to the shadow-adjusted target of the last struct in event’s path,
  128. // that is either struct or preceding struct, whose shadow-adjusted target is non-null.
  129. event.set_target(last_valid_shadow_adjusted_target.value().shadow_adjusted_target);
  130. // 2. Set event’s relatedTarget to struct’s relatedTarget.
  131. event.set_related_target(struct_.related_target);
  132. // 3. Set event’s touch target list to struct’s touch target list.
  133. event.set_touch_target_list(struct_.touch_target_list);
  134. // 4. If event’s stop propagation flag is set, then return.
  135. if (event.should_stop_propagation())
  136. return;
  137. // 5. Initialize event’s currentTarget attribute to struct’s invocation target.
  138. event.set_current_target(struct_.invocation_target);
  139. // 6. Let listeners be a clone of event’s currentTarget attribute value’s event listener list.
  140. // NOTE: This avoids event listeners added after this point from being run. Note that removal still has an effect due to the removed field.
  141. auto listeners = event.current_target()->event_listener_list();
  142. // 7. Let invocationTargetInShadowTree be struct’s invocation-target-in-shadow-tree.
  143. bool invocation_target_in_shadow_tree = struct_.invocation_target_in_shadow_tree;
  144. // 8. Let found be the result of running inner invoke with event, listeners, phase, invocationTargetInShadowTree, and legacyOutputDidListenersThrowFlag if given.
  145. bool found = inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);
  146. // 9. If found is false and event’s isTrusted attribute is true, then:
  147. if (!found && event.is_trusted()) {
  148. // 1. Let originalEventType be event’s type attribute value.
  149. auto original_event_type = event.type();
  150. // 2. If event’s type attribute value is a match for any of the strings in the first column in the following table,
  151. // set event’s type attribute value to the string in the second column on the same row as the matching string, and return otherwise.
  152. if (event.type() == "animationend")
  153. event.set_type("webkitAnimationEnd"sv);
  154. else if (event.type() == "animationiteration")
  155. event.set_type("webkitAnimationIteration"sv);
  156. else if (event.type() == "animationstart")
  157. event.set_type("webkitAnimationStart"sv);
  158. else if (event.type() == "transitionend")
  159. event.set_type("webkitTransitionEnd"sv);
  160. else
  161. return;
  162. // 3. Inner invoke with event, listeners, phase, invocationTargetInShadowTree, and legacyOutputDidListenersThrowFlag if given.
  163. inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);
  164. // 4. Set event’s type attribute value to originalEventType.
  165. event.set_type(original_event_type);
  166. }
  167. }
  168. // https://dom.spec.whatwg.org/#concept-event-dispatch
  169. bool EventDispatcher::dispatch(NonnullRefPtr<EventTarget> target, NonnullRefPtr<Event> event, bool legacy_target_override)
  170. {
  171. // 1. Set event’s dispatch flag.
  172. event->set_dispatched(true);
  173. // 2. Let targetOverride be target, if legacy target override flag is not given, and target’s associated Document otherwise. [HTML]
  174. // NOTE: legacy target override flag is only used by HTML and only when target is a Window object.
  175. RefPtr<EventTarget> target_override;
  176. if (!legacy_target_override) {
  177. target_override = target;
  178. } else {
  179. target_override = verify_cast<HTML::Window>(*target).associated_document();
  180. }
  181. // 3. Let activationTarget be null.
  182. RefPtr<EventTarget> activation_target;
  183. // 4. Let relatedTarget be the result of retargeting event’s relatedTarget against target.
  184. RefPtr<EventTarget> related_target = retarget(event->related_target(), target);
  185. bool clear_targets = false;
  186. // 5. If target is not relatedTarget or target is event’s relatedTarget, then:
  187. if (related_target != target || event->related_target() == target) {
  188. // 1. Let touchTargets be a new list.
  189. Event::TouchTargetList touch_targets;
  190. // 2. For each touchTarget of event’s touch target list, append the result of retargeting touchTarget against target to touchTargets.
  191. for (auto& touch_target : event->touch_target_list()) {
  192. touch_targets.append(retarget(touch_target, target));
  193. }
  194. // 3. Append to an event path with event, target, targetOverride, relatedTarget, touchTargets, and false.
  195. event->append_to_path(*target, target_override, related_target, touch_targets, false);
  196. // 4. Let isActivationEvent be true, if event is a MouseEvent object and event’s type attribute is "click"; otherwise false.
  197. bool is_activation_event = is<UIEvents::MouseEvent>(*event) && event->type() == HTML::EventNames::click;
  198. // 5. If isActivationEvent is true and target has activation behavior, then set activationTarget to target.
  199. if (is_activation_event && target->activation_behavior)
  200. activation_target = target;
  201. // FIXME: 6. Let slottable be target, if target is a slottable and is assigned, and null otherwise.
  202. // 7. Let slot-in-closed-tree be false
  203. bool slot_in_closed_tree = false;
  204. // 8. Let parent be the result of invoking target’s get the parent with event.
  205. auto* parent = target->get_parent(event);
  206. // 9. While parent is non-null:
  207. while (parent) {
  208. // FIXME: 1. If slottable is non-null:
  209. // 1. Assert: parent is a slot.
  210. // 2. Set slottable to null.
  211. // 3. If parent’s root is a shadow root whose mode is "closed", then set slot-in-closed-tree to true.
  212. // FIXME: 2. If parent is a slottable and is assigned, then set slottable to parent.
  213. // 3. Let relatedTarget be the result of retargeting event’s relatedTarget against parent.
  214. related_target = retarget(event->related_target(), parent);
  215. // 4. Let touchTargets be a new list.
  216. touch_targets.clear();
  217. // 5. For each touchTarget of event’s touch target list, append the result of retargeting touchTarget against parent to touchTargets.
  218. for (auto& touch_target : event->touch_target_list()) {
  219. touch_targets.append(retarget(touch_target, parent));
  220. }
  221. // 6. If parent is a Window object, or parent is a node and target’s root is a shadow-including inclusive ancestor of parent, then:
  222. if (is<HTML::Window>(parent)
  223. || (is<Node>(parent) && verify_cast<Node>(*target).root().is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*parent)))) {
  224. // 1. If isActivationEvent is true, event’s bubbles attribute is true, activationTarget is null, and parent has activation behavior, then set activationTarget to parent.
  225. if (is_activation_event && event->bubbles() && !activation_target && parent->activation_behavior)
  226. activation_target = parent;
  227. // 2. Append to an event path with event, parent, null, relatedTarget, touchTargets, and slot-in-closed-tree.
  228. event->append_to_path(*parent, nullptr, related_target, touch_targets, slot_in_closed_tree);
  229. }
  230. // 7. Otherwise, if parent is relatedTarget, then set parent to null.
  231. else if (related_target == parent) {
  232. parent = nullptr;
  233. }
  234. // 8. Otherwise, set target to parent and then:
  235. else {
  236. target = *parent;
  237. // 1. If isActivationEvent is true, activationTarget is null, and target has activation behavior, then set activationTarget to target.
  238. if (is_activation_event && !activation_target && target->activation_behavior)
  239. activation_target = target;
  240. // 2. Append to an event path with event, parent, target, relatedTarget, touchTargets, and slot-in-closed-tree.
  241. event->append_to_path(*parent, target, related_target, touch_targets, slot_in_closed_tree);
  242. }
  243. // 9. If parent is non-null, then set parent to the result of invoking parent’s get the parent with event.
  244. if (parent) {
  245. parent = parent->get_parent(event);
  246. }
  247. // 10. Set slot-in-closed-tree to false.
  248. slot_in_closed_tree = false;
  249. }
  250. // 10. Let clearTargetsStruct be the last struct in event’s path whose shadow-adjusted target is non-null.
  251. auto clear_targets_struct = event->path().last_matching([](auto& entry) {
  252. return !entry.shadow_adjusted_target.is_null();
  253. });
  254. VERIFY(clear_targets_struct.has_value());
  255. // 11. Let clearTargets be true if clearTargetsStruct’s shadow-adjusted target, clearTargetsStruct’s relatedTarget,
  256. // or an EventTarget object in clearTargetsStruct’s touch target list is a node and its root is a shadow root; otherwise false.
  257. if (is<Node>(clear_targets_struct.value().shadow_adjusted_target.ptr())) {
  258. auto& shadow_adjusted_target_node = verify_cast<Node>(*clear_targets_struct.value().shadow_adjusted_target);
  259. if (is<ShadowRoot>(shadow_adjusted_target_node.root()))
  260. clear_targets = true;
  261. }
  262. if (!clear_targets && is<Node>(clear_targets_struct.value().related_target.ptr())) {
  263. auto& related_target_node = verify_cast<Node>(*clear_targets_struct.value().related_target);
  264. if (is<ShadowRoot>(related_target_node.root()))
  265. clear_targets = true;
  266. }
  267. if (!clear_targets) {
  268. for (auto touch_target : clear_targets_struct.value().touch_target_list) {
  269. if (is<Node>(*touch_target.ptr())) {
  270. auto& touch_target_node = verify_cast<Node>(*touch_target.ptr());
  271. if (is<ShadowRoot>(touch_target_node.root())) {
  272. clear_targets = true;
  273. break;
  274. }
  275. }
  276. }
  277. }
  278. // 12. If activationTarget is non-null and activationTarget has legacy-pre-activation behavior, then run activationTarget’s legacy-pre-activation behavior.
  279. if (activation_target)
  280. activation_target->legacy_pre_activation_behavior();
  281. // 13. For each struct in event’s path, in reverse order:
  282. for (auto& entry : event->path().in_reverse()) {
  283. // 1. If struct’s shadow-adjusted target is non-null, then set event’s eventPhase attribute to AT_TARGET.
  284. if (entry.shadow_adjusted_target)
  285. event->set_phase(Event::Phase::AtTarget);
  286. // 2. Otherwise, set event’s eventPhase attribute to CAPTURING_PHASE.
  287. else
  288. event->set_phase(Event::Phase::CapturingPhase);
  289. // 3. Invoke with struct, event, "capturing", and legacyOutputDidListenersThrowFlag if given.
  290. invoke(entry, event, Event::Phase::CapturingPhase);
  291. }
  292. // 14. For each struct in event’s path:
  293. for (auto& entry : event->path()) {
  294. // 1. If struct’s shadow-adjusted target is non-null, then set event’s eventPhase attribute to AT_TARGET.
  295. if (entry.shadow_adjusted_target) {
  296. event->set_phase(Event::Phase::AtTarget);
  297. }
  298. // 2. Otherwise:
  299. else {
  300. // 1. If event’s bubbles attribute is false, then continue.
  301. if (!event->bubbles())
  302. continue;
  303. // 2. Set event’s eventPhase attribute to BUBBLING_PHASE.
  304. event->set_phase(Event::Phase::BubblingPhase);
  305. }
  306. // 3. Invoke with struct, event, "bubbling", and legacyOutputDidListenersThrowFlag if given.
  307. invoke(entry, event, Event::Phase::BubblingPhase);
  308. }
  309. }
  310. // 6. Set event’s eventPhase attribute to NONE.
  311. event->set_phase(Event::Phase::None);
  312. // 7. Set event’s currentTarget attribute to null.
  313. event->set_current_target(nullptr);
  314. // 8. Set event’s path to the empty list.
  315. event->clear_path();
  316. // 9. Unset event’s dispatch flag, stop propagation flag, and stop immediate propagation flag.
  317. event->set_dispatched(false);
  318. event->set_stop_propagation(false);
  319. event->set_stop_immediate_propagation(false);
  320. // 10. If clearTargets, then:
  321. if (clear_targets) {
  322. // 1. Set event’s target to null.
  323. event->set_target(nullptr);
  324. // 2. Set event’s relatedTarget to null.
  325. event->set_related_target(nullptr);
  326. // 3. Set event’s touch target list to the empty list.
  327. event->clear_touch_target_list();
  328. }
  329. // 11. If activationTarget is non-null, then:
  330. if (activation_target) {
  331. // 1. If event’s canceled flag is unset, then run activationTarget’s activation behavior with event.
  332. if (!event->cancelled()) {
  333. activation_target->activation_behavior(event);
  334. activation_target->legacy_cancelled_activation_behavior_was_not_called();
  335. }
  336. // 2. Otherwise, if activationTarget has legacy-canceled-activation behavior, then run activationTarget’s legacy-canceled-activation behavior.
  337. else {
  338. activation_target->legacy_cancelled_activation_behavior();
  339. }
  340. }
  341. // 12. Return false if event’s canceled flag is set; otherwise true.
  342. return !event->cancelled();
  343. }
  344. }