EventDispatcher.cpp 20 KB

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