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