EventDispatcher.cpp 20 KB

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