summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM/EventDispatcher.cpp
blob: 9584813bedaee5af647f8c559c44a0774450652f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Assertions.h>
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibWeb/Bindings/EventTargetWrapper.h>
#include <LibWeb/Bindings/EventTargetWrapperFactory.h>
#include <LibWeb/Bindings/EventWrapper.h>
#include <LibWeb/Bindings/EventWrapperFactory.h>
#include <LibWeb/Bindings/IDLAbstractOperations.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/AbortSignal.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/UIEvents/MouseEvent.h>

namespace Web::DOM {

// FIXME: This shouldn't be here, as retargeting is not only used by the event dispatcher.
//        When moving this function, it needs to be generalized. https://dom.spec.whatwg.org/#retarget
static EventTarget* retarget(EventTarget* left, EventTarget* right)
{
    for (;;) {
        if (!is<Node>(left))
            return left;

        auto* left_node = verify_cast<Node>(left);
        auto& left_root = left_node->root();
        if (!is<ShadowRoot>(left_root))
            return left;

        if (is<Node>(right) && left_root.is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*right)))
            return left;

        auto& left_shadow_root = verify_cast<ShadowRoot>(left_root);
        left = left_shadow_root.host();
    }
}

// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
bool EventDispatcher::inner_invoke(Event& event, Vector<NonnullRefPtr<DOM::DOMEventListener>>& listeners, Event::Phase phase, bool invocation_target_in_shadow_tree)
{
    // 1. Let found be false.
    bool found = false;

    // 2. For each listener in listeners, whose removed is false:
    for (auto& listener : listeners) {
        if (listener->removed)
            continue;

        // 1. If event’s type attribute value is not listener’s type, then continue.
        if (event.type() != listener->type)
            continue;

        // 2. Set found to true.
        found = true;

        // 3. If phase is "capturing" and listener’s capture is false, then continue.
        if (phase == Event::Phase::CapturingPhase && !listener->capture)
            continue;

        // 4. If phase is "bubbling" and listener’s capture is true, then continue.
        if (phase == Event::Phase::BubblingPhase && listener->capture)
            continue;

        // 5. If listener’s once is true, then remove listener from event’s currentTarget attribute value’s event listener list.
        if (listener->once)
            event.current_target()->remove_from_event_listener_list(listener);

        // 6. Let global be listener callback’s associated Realm’s global object.
        auto& callback = listener->callback->callback();
        auto& global = callback.callback.cell()->global_object();

        // 7. Let currentEvent be undefined.
        RefPtr<Event> current_event;

        // 8. If global is a Window object, then:
        if (is<Bindings::WindowObject>(global)) {
            auto& bindings_window_global = verify_cast<Bindings::WindowObject>(global);
            auto& window_impl = bindings_window_global.impl();

            // 1. Set currentEvent to global’s current event.
            current_event = window_impl.current_event();

            // 2. If invocationTargetInShadowTree is false, then set global’s current event to event.
            if (!invocation_target_in_shadow_tree)
                window_impl.set_current_event(&event);
        }

        // 9. If listener’s passive is true, then set event’s in passive listener flag.
        if (listener->passive)
            event.set_in_passive_listener(true);

        // 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:
        // FIXME: These should be wrapped for us in call_user_object_operation, but it currently doesn't do that.
        auto* this_value = Bindings::wrap(global, *event.current_target());
        auto* wrapped_event = Bindings::wrap(global, event);
        auto result = Bindings::IDL::call_user_object_operation(callback, "handleEvent", this_value, wrapped_event);

        // If this throws an exception, then:
        if (result.is_error()) {
            // 1. Report the exception.
            HTML::report_exception(result);

            // FIXME: 2. Set legacyOutputDidListenersThrowFlag if given. (Only used by IndexedDB currently)
        }

        // 11. Unset event’s in passive listener flag.
        event.set_in_passive_listener(false);

        // 12. If global is a Window object, then set global’s current event to currentEvent.
        if (is<Bindings::WindowObject>(global)) {
            auto& bindings_window_global = verify_cast<Bindings::WindowObject>(global);
            auto& window_impl = bindings_window_global.impl();
            window_impl.set_current_event(current_event);
        }

        // 13. If event’s stop immediate propagation flag is set, then return found.
        if (event.should_stop_immediate_propagation())
            return found;
    }

    // 3. Return found.
    return found;
}

// https://dom.spec.whatwg.org/#concept-event-listener-invoke
void EventDispatcher::invoke(Event::PathEntry& struct_, Event& event, Event::Phase phase)
{
    auto last_valid_shadow_adjusted_target = event.path().last_matching([&struct_](auto& entry) {
        return entry.index <= struct_.index && !entry.shadow_adjusted_target.is_null();
    });

    VERIFY(last_valid_shadow_adjusted_target.has_value());

    event.set_target(last_valid_shadow_adjusted_target.value().shadow_adjusted_target);
    event.set_related_target(struct_.related_target);
    event.set_touch_target_list(struct_.touch_target_list);

    if (event.should_stop_propagation())
        return;

    event.set_current_target(struct_.invocation_target);

    // NOTE: This is an intentional copy. Any event listeners added after this point will not be invoked.
    auto listeners = event.current_target()->event_listener_list();
    bool invocation_target_in_shadow_tree = struct_.invocation_target_in_shadow_tree;

    bool found = inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);

    if (!found && event.is_trusted()) {
        auto original_event_type = event.type();

        if (event.type() == "animationend")
            event.set_type("webkitAnimationEnd");
        else if (event.type() == "animationiteration")
            event.set_type("webkitAnimationIteration");
        else if (event.type() == "animationstart")
            event.set_type("webkitAnimationStart");
        else if (event.type() == "transitionend")
            event.set_type("webkitTransitionEnd");
        else
            return;

        inner_invoke(event, listeners, phase, invocation_target_in_shadow_tree);
        event.set_type(original_event_type);
    }
}

// https://dom.spec.whatwg.org/#concept-event-dispatch
bool EventDispatcher::dispatch(NonnullRefPtr<EventTarget> target, NonnullRefPtr<Event> event, bool legacy_target_override)
{
    event->set_dispatched(true);
    RefPtr<EventTarget> target_override;

    if (!legacy_target_override) {
        target_override = target;
    } else {
        // NOTE: This can be done because legacy_target_override is only set for events targeted at Window.
        target_override = verify_cast<HTML::Window>(*target).associated_document();
    }

    RefPtr<EventTarget> activation_target;
    RefPtr<EventTarget> related_target = retarget(event->related_target(), target);

    bool clear_targets = false;

    if (related_target != target || event->related_target() == target) {
        Event::TouchTargetList touch_targets;

        for (auto& touch_target : event->touch_target_list()) {
            touch_targets.append(retarget(touch_target, target));
        }

        event->append_to_path(*target, target_override, related_target, touch_targets, false);

        bool is_activation_event = is<UIEvents::MouseEvent>(*event) && event->type() == HTML::EventNames::click;

        if (is_activation_event && target->activation_behavior)
            activation_target = target;

        // FIXME: Let slottable be target, if target is a slottable and is assigned, and null otherwise.

        bool slot_in_closed_tree = false;
        auto* parent = target->get_parent(event);

        while (parent) {
            // FIXME: If slottable is non-null:

            // FIXME: If parent is a slottable and is assigned, then set slottable to parent.

            related_target = retarget(event->related_target(), parent);
            touch_targets.clear();

            for (auto& touch_target : event->touch_target_list()) {
                touch_targets.append(retarget(touch_target, parent));
            }

            if (is<HTML::Window>(parent)
                || (is<Node>(parent) && verify_cast<Node>(*target).root().is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*parent)))) {
                if (is_activation_event && event->bubbles() && !activation_target && parent->activation_behavior)
                    activation_target = parent;

                event->append_to_path(*parent, nullptr, related_target, touch_targets, slot_in_closed_tree);
            } else if (related_target == parent) {
                parent = nullptr;
            } else {
                target = *parent;

                if (is_activation_event && !activation_target && target->activation_behavior)
                    activation_target = target;

                event->append_to_path(*parent, target, related_target, touch_targets, slot_in_closed_tree);
            }

            if (parent) {
                parent = parent->get_parent(event);
            }

            slot_in_closed_tree = false;
        }

        auto clear_targets_struct = event->path().last_matching([](auto& entry) {
            return !entry.shadow_adjusted_target.is_null();
        });

        VERIFY(clear_targets_struct.has_value());

        if (is<Node>(clear_targets_struct.value().shadow_adjusted_target.ptr())) {
            auto& shadow_adjusted_target_node = verify_cast<Node>(*clear_targets_struct.value().shadow_adjusted_target);
            if (is<ShadowRoot>(shadow_adjusted_target_node.root()))
                clear_targets = true;
        }

        if (!clear_targets && is<Node>(clear_targets_struct.value().related_target.ptr())) {
            auto& related_target_node = verify_cast<Node>(*clear_targets_struct.value().related_target);
            if (is<ShadowRoot>(related_target_node.root()))
                clear_targets = true;
        }

        if (!clear_targets) {
            for (auto touch_target : clear_targets_struct.value().touch_target_list) {
                if (is<Node>(*touch_target.ptr())) {
                    auto& touch_target_node = verify_cast<Node>(*touch_target.ptr());
                    if (is<ShadowRoot>(touch_target_node.root())) {
                        clear_targets = true;
                        break;
                    }
                }
            }
        }

        if (activation_target)
            activation_target->legacy_pre_activation_behavior();

        for (ssize_t i = event->path().size() - 1; i >= 0; --i) {
            auto& entry = event->path().at(i);

            if (entry.shadow_adjusted_target)
                event->set_phase(Event::Phase::AtTarget);
            else
                event->set_phase(Event::Phase::CapturingPhase);

            invoke(entry, event, Event::Phase::CapturingPhase);
        }

        for (auto& entry : event->path()) {
            if (entry.shadow_adjusted_target) {
                event->set_phase(Event::Phase::AtTarget);
            } else {
                if (!event->bubbles())
                    continue;

                event->set_phase(Event::Phase::BubblingPhase);
            }

            invoke(entry, event, Event::Phase::BubblingPhase);
        }
    }

    event->set_phase(Event::Phase::None);
    event->set_current_target(nullptr);
    event->clear_path();
    event->set_dispatched(false);
    event->set_stop_propagation(false);
    event->set_stop_immediate_propagation(false);

    if (clear_targets) {
        event->set_target(nullptr);
        event->set_related_target(nullptr);
        event->clear_touch_target_list();
    }

    if (activation_target) {
        if (!event->cancelled()) {
            // NOTE: Since activation_target is set, it will have activation behavior.
            activation_target->activation_behavior(event);
            activation_target->legacy_cancelled_activation_behavior_was_not_called();
        } else {
            activation_target->legacy_cancelled_activation_behavior();
        }
    }

    return !event->cancelled();
}

}