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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::cell::Cell;
use std::default::Default;

use base::cross_process_instant::CrossProcessInstant;
use devtools_traits::{TimelineMarker, TimelineMarkerType};
use dom_struct::dom_struct;
use js::rust::HandleObject;
use servo_atoms::Atom;

use crate::dom::bindings::callback::ExceptionHandling;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::EventBinding;
use crate::dom::bindings::codegen::Bindings::EventBinding::{EventConstants, EventMethods};
use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, DomObject, Reflector};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::document::Document;
use crate::dom::element::Element;
use crate::dom::eventtarget::{CompiledEventListener, EventTarget, ListenerPhase};
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlinputelement::InputActivationState;
use crate::dom::mouseevent::MouseEvent;
use crate::dom::node::{Node, ShadowIncluding};
use crate::dom::virtualmethods::vtable_for;
use crate::dom::window::Window;
use crate::script_runtime::CanGc;
use crate::task::TaskOnce;

#[dom_struct]
pub struct Event {
    reflector_: Reflector,
    current_target: MutNullableDom<EventTarget>,
    target: MutNullableDom<EventTarget>,
    #[no_trace]
    type_: DomRefCell<Atom>,
    phase: Cell<EventPhase>,
    canceled: Cell<EventDefault>,
    stop_propagation: Cell<bool>,
    stop_immediate: Cell<bool>,
    cancelable: Cell<bool>,
    bubbles: Cell<bool>,
    trusted: Cell<bool>,
    dispatching: Cell<bool>,
    initialized: Cell<bool>,
    #[no_trace]
    time_stamp: CrossProcessInstant,
}

impl Event {
    pub fn new_inherited() -> Event {
        Event {
            reflector_: Reflector::new(),
            current_target: Default::default(),
            target: Default::default(),
            type_: DomRefCell::new(atom!("")),
            phase: Cell::new(EventPhase::None),
            canceled: Cell::new(EventDefault::Allowed),
            stop_propagation: Cell::new(false),
            stop_immediate: Cell::new(false),
            cancelable: Cell::new(false),
            bubbles: Cell::new(false),
            trusted: Cell::new(false),
            dispatching: Cell::new(false),
            initialized: Cell::new(false),
            time_stamp: CrossProcessInstant::now(),
        }
    }

    pub fn new_uninitialized(global: &GlobalScope, can_gc: CanGc) -> DomRoot<Event> {
        Self::new_uninitialized_with_proto(global, None, can_gc)
    }

    pub fn new_uninitialized_with_proto(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
    ) -> DomRoot<Event> {
        reflect_dom_object_with_proto(Box::new(Event::new_inherited()), global, proto, can_gc)
    }

    pub fn new(
        global: &GlobalScope,
        type_: Atom,
        bubbles: EventBubbles,
        cancelable: EventCancelable,
        can_gc: CanGc,
    ) -> DomRoot<Event> {
        Self::new_with_proto(global, None, type_, bubbles, cancelable, can_gc)
    }

    fn new_with_proto(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        type_: Atom,
        bubbles: EventBubbles,
        cancelable: EventCancelable,
        can_gc: CanGc,
    ) -> DomRoot<Event> {
        let event = Event::new_uninitialized_with_proto(global, proto, can_gc);
        event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
        event
    }

    pub fn init_event(&self, type_: Atom, bubbles: bool, cancelable: bool) {
        if self.dispatching.get() {
            return;
        }

        self.initialized.set(true);
        self.stop_propagation.set(false);
        self.stop_immediate.set(false);
        self.canceled.set(EventDefault::Allowed);
        self.trusted.set(false);
        self.target.set(None);
        *self.type_.borrow_mut() = type_;
        self.bubbles.set(bubbles);
        self.cancelable.set(cancelable);
    }

    /// <https://dom.spec.whatwg.org/#event-path>
    // TODO: shadow roots put special flags in the path,
    // and it will stop just being a list of bare EventTargets
    fn construct_event_path(&self, target: &EventTarget) -> Vec<DomRoot<EventTarget>> {
        let mut event_path = vec![];
        if let Some(target_node) = target.downcast::<Node>() {
            // ShadowIncluding::Yes might be closer to right than ::No,
            // but still wrong since things about the path change when crossing
            // shadow attachments; getting it right needs to change
            // more than just that.
            for ancestor in target_node.inclusive_ancestors(ShadowIncluding::No) {
                event_path.push(DomRoot::from_ref(ancestor.upcast::<EventTarget>()));
            }
            // Most event-target-to-parent relationships are node parent
            // relationships, but the document-to-global one is not,
            // so that's handled separately here.
            // (an EventTarget.get_parent_event_target could save
            // some redundancy, especially when shadow DOM relationships
            // also need to be respected)
            let top_most_ancestor_or_target = event_path
                .last()
                .cloned()
                .unwrap_or(DomRoot::from_ref(target));
            if let Some(document) = DomRoot::downcast::<Document>(top_most_ancestor_or_target) {
                if self.type_() != atom!("load") && document.browsing_context().is_some() {
                    event_path.push(DomRoot::from_ref(document.window().upcast()));
                }
            }
        } else {
            // a non-node EventTarget, likely a global.
            // No parent to propagate up to, but we still
            // need it on the path.
            event_path.push(DomRoot::from_ref(target));
        }
        event_path
    }

    /// <https://dom.spec.whatwg.org/#concept-event-dispatch>
    pub fn dispatch(
        &self,
        target: &EventTarget,
        legacy_target_override: bool,
        can_gc: CanGc,
        // TODO legacy_did_output_listeners_throw_flag for indexeddb
    ) -> EventStatus {
        // Step 1.
        self.dispatching.set(true);

        // Step 2.
        let target_override_document; // upcasted EventTarget's lifetime depends on this
        let target_override = if legacy_target_override {
            target_override_document = target
                .downcast::<Window>()
                .expect("legacy_target_override must be true only when target is a Window")
                .Document();
            target_override_document.upcast::<EventTarget>()
        } else {
            target
        };

        // Step 3 - since step 5 always happens, we can wait until 5.5

        // Step 4 TODO: "retargeting" concept depends on shadow DOM

        // Step 5, outer if-statement, is always true until step 4 is implemented
        // Steps 5.1-5.2 TODO: touch target lists don't exist yet

        // Steps 5.3 and most of 5.9
        // A change in whatwg/dom#240 specifies that
        // the event path belongs to the event itself, rather than being
        // a local variable of the dispatch algorithm, but this is mostly
        // related to shadow DOM requirements that aren't otherwise
        // implemented right now. The path also needs to contain
        // various flags instead of just bare event targets.
        let path = self.construct_event_path(target);
        rooted_vec!(let event_path <- path.into_iter());

        // Step 5.4
        let is_activation_event = self.is::<MouseEvent>() && self.type_() == atom!("click");

        // Step 5.5
        let mut activation_target = if is_activation_event {
            target
                .downcast::<Element>()
                .and_then(|e| e.as_maybe_activatable())
        } else {
            // Step 3
            None
        };

        // Steps 5-6 - 5.7 are shadow DOM slot things

        // Step 5.9.8.1, not covered in construct_event_path
        // This what makes sure that clicking on e.g. an <img> inside
        // an <a> will cause activation of the activatable ancestor.
        if is_activation_event && activation_target.is_none() && self.bubbles.get() {
            for object in event_path.iter() {
                if let Some(activatable_ancestor) = object
                    .downcast::<Element>()
                    .and_then(|e| e.as_maybe_activatable())
                {
                    activation_target = Some(activatable_ancestor);
                    // once activation_target isn't null, we stop
                    // looking at ancestors for it.
                    break;
                }
            }
        }

        // Steps 5.10-5.11 are shadow DOM

        // Not specified in dispatch spec overtly; this is because
        // the legacy canceled activation behavior of a checkbox
        // or radio button needs to know what happened in the
        // corresponding pre-activation behavior.
        let mut pre_activation_result: Option<InputActivationState> = None;

        // Step 5.12
        if is_activation_event {
            if let Some(maybe_checkbox) = activation_target {
                pre_activation_result = maybe_checkbox.legacy_pre_activation_behavior();
            }
        }

        let timeline_window = DomRoot::downcast::<Window>(target.global())
            .filter(|window| window.need_emit_timeline_marker(TimelineMarkerType::DOMEvent));

        // Step 5.13
        for object in event_path.iter().rev() {
            if &**object == target {
                self.phase.set(EventPhase::AtTarget);
            } else {
                self.phase.set(EventPhase::Capturing);
            }

            // setting self.target is step 1 of invoke,
            // but done here because our event_path isn't a member of self
            // (without shadow DOM, target_override is always the
            // target to set to)
            self.target.set(Some(target_override));
            invoke(
                timeline_window.as_deref(),
                object,
                self,
                Some(ListenerPhase::Capturing),
                can_gc,
            );
        }

        // Step 5.14
        for object in event_path.iter() {
            let at_target = &**object == target;
            if at_target || self.bubbles.get() {
                self.phase.set(if at_target {
                    EventPhase::AtTarget
                } else {
                    EventPhase::Bubbling
                });

                self.target.set(Some(target_override));
                invoke(
                    timeline_window.as_deref(),
                    object,
                    self,
                    Some(ListenerPhase::Bubbling),
                    can_gc,
                );
            }
        }

        // Step 6
        self.phase.set(EventPhase::None);

        // FIXME: The UIEvents spec still expects firing an event
        // to carry a "default action" semantic, but the HTML spec
        // has removed this concept. Nothing in either spec currently
        // (as of Jan 11 2020) says that, e.g., a keydown event on an
        // input element causes a character to be typed; the UIEvents
        // spec assumes the HTML spec is covering it, and the HTML spec
        // no longer specifies any UI event other than mouse click as
        // causing an element to perform an action.
        // Compare:
        // https://w3c.github.io/uievents/#default-action
        // https://dom.spec.whatwg.org/#action-versus-occurance
        if !self.DefaultPrevented() {
            if let Some(target) = self.GetTarget() {
                if let Some(node) = target.downcast::<Node>() {
                    let vtable = vtable_for(node);
                    vtable.handle_event(self);
                }
            }
        }

        // Step 7
        self.current_target.set(None);

        // Step 8 TODO: if path were in the event struct, we'd clear it now

        // Step 9
        self.dispatching.set(false);
        self.stop_propagation.set(false);
        self.stop_immediate.set(false);

        // Step 10 TODO: condition is always false until there's shadow DOM

        // Step 11
        if let Some(activation_target) = activation_target {
            if self.DefaultPrevented() {
                activation_target.legacy_canceled_activation_behavior(pre_activation_result);
            } else {
                activation_target.activation_behavior(self, target, can_gc);
            }
        }

        self.status()
    }

    pub fn status(&self) -> EventStatus {
        if self.DefaultPrevented() {
            EventStatus::Canceled
        } else {
            EventStatus::NotCanceled
        }
    }

    #[inline]
    pub fn dispatching(&self) -> bool {
        self.dispatching.get()
    }

    #[inline]
    pub fn initialized(&self) -> bool {
        self.initialized.get()
    }

    #[inline]
    pub fn type_(&self) -> Atom {
        self.type_.borrow().clone()
    }

    #[inline]
    pub fn mark_as_handled(&self) {
        self.canceled.set(EventDefault::Handled);
    }

    #[inline]
    pub fn get_cancel_state(&self) -> EventDefault {
        self.canceled.get()
    }

    pub fn set_trusted(&self, trusted: bool) {
        self.trusted.set(trusted);
    }

    /// <https://html.spec.whatwg.org/multipage/#fire-a-simple-event>
    pub fn fire(&self, target: &EventTarget, can_gc: CanGc) -> EventStatus {
        self.set_trusted(true);
        target.dispatch_event(self, can_gc)
    }
}

impl EventMethods for Event {
    /// <https://dom.spec.whatwg.org/#dom-event-event>
    fn Constructor(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
        type_: DOMString,
        init: &EventBinding::EventInit,
    ) -> Fallible<DomRoot<Event>> {
        let bubbles = EventBubbles::from(init.bubbles);
        let cancelable = EventCancelable::from(init.cancelable);
        Ok(Event::new_with_proto(
            global,
            proto,
            Atom::from(type_),
            bubbles,
            cancelable,
            can_gc,
        ))
    }

    /// <https://dom.spec.whatwg.org/#dom-event-eventphase>
    fn EventPhase(&self) -> u16 {
        self.phase.get() as u16
    }

    /// <https://dom.spec.whatwg.org/#dom-event-type>
    fn Type(&self) -> DOMString {
        DOMString::from(&*self.type_()) // FIXME(ajeffrey): Directly convert from Atom to DOMString
    }

    /// <https://dom.spec.whatwg.org/#dom-event-target>
    fn GetTarget(&self) -> Option<DomRoot<EventTarget>> {
        self.target.get()
    }

    /// <https://dom.spec.whatwg.org/#dom-event-srcelement>
    fn GetSrcElement(&self) -> Option<DomRoot<EventTarget>> {
        self.target.get()
    }

    /// <https://dom.spec.whatwg.org/#dom-event-currenttarget>
    fn GetCurrentTarget(&self) -> Option<DomRoot<EventTarget>> {
        self.current_target.get()
    }

    /// <https://dom.spec.whatwg.org/#dom-event-composedpath>
    fn ComposedPath(&self) -> Vec<DomRoot<EventTarget>> {
        if let Some(target) = self.target.get() {
            self.construct_event_path(&target)
        } else {
            vec![]
        }
    }

    /// <https://dom.spec.whatwg.org/#dom-event-defaultprevented>
    fn DefaultPrevented(&self) -> bool {
        self.canceled.get() == EventDefault::Prevented
    }

    /// <https://dom.spec.whatwg.org/#dom-event-preventdefault>
    fn PreventDefault(&self) {
        if self.cancelable.get() {
            self.canceled.set(EventDefault::Prevented)
        }
    }

    /// <https://dom.spec.whatwg.org/#dom-event-stoppropagation>
    fn StopPropagation(&self) {
        self.stop_propagation.set(true);
    }

    /// <https://dom.spec.whatwg.org/#dom-event-stopimmediatepropagation>
    fn StopImmediatePropagation(&self) {
        self.stop_immediate.set(true);
        self.stop_propagation.set(true);
    }

    /// <https://dom.spec.whatwg.org/#dom-event-bubbles>
    fn Bubbles(&self) -> bool {
        self.bubbles.get()
    }

    /// <https://dom.spec.whatwg.org/#dom-event-cancelable>
    fn Cancelable(&self) -> bool {
        self.cancelable.get()
    }

    /// <https://dom.spec.whatwg.org/#dom-event-returnvalue>
    fn ReturnValue(&self) -> bool {
        self.canceled.get() == EventDefault::Allowed
    }

    /// <https://dom.spec.whatwg.org/#dom-event-returnvalue>
    fn SetReturnValue(&self, val: bool) {
        if !val {
            self.PreventDefault();
        }
    }

    /// <https://dom.spec.whatwg.org/#dom-event-cancelbubble>
    fn CancelBubble(&self) -> bool {
        self.stop_propagation.get()
    }

    /// <https://dom.spec.whatwg.org/#dom-event-cancelbubble>
    fn SetCancelBubble(&self, value: bool) {
        if value {
            self.stop_propagation.set(true)
        }
    }

    /// <https://dom.spec.whatwg.org/#dom-event-timestamp>
    fn TimeStamp(&self) -> DOMHighResTimeStamp {
        self.global()
            .performance()
            .to_dom_high_res_time_stamp(self.time_stamp)
    }

    /// <https://dom.spec.whatwg.org/#dom-event-initevent>
    fn InitEvent(&self, type_: DOMString, bubbles: bool, cancelable: bool) {
        self.init_event(Atom::from(type_), bubbles, cancelable)
    }

    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
    fn IsTrusted(&self) -> bool {
        self.trusted.get()
    }
}

#[derive(Clone, Copy, MallocSizeOf, PartialEq)]
pub enum EventBubbles {
    Bubbles,
    DoesNotBubble,
}

impl From<bool> for EventBubbles {
    fn from(boolean: bool) -> Self {
        if boolean {
            EventBubbles::Bubbles
        } else {
            EventBubbles::DoesNotBubble
        }
    }
}

impl From<EventBubbles> for bool {
    fn from(bubbles: EventBubbles) -> Self {
        match bubbles {
            EventBubbles::Bubbles => true,
            EventBubbles::DoesNotBubble => false,
        }
    }
}

#[derive(Clone, Copy, MallocSizeOf, PartialEq)]
pub enum EventCancelable {
    Cancelable,
    NotCancelable,
}

impl From<bool> for EventCancelable {
    fn from(boolean: bool) -> Self {
        if boolean {
            EventCancelable::Cancelable
        } else {
            EventCancelable::NotCancelable
        }
    }
}

impl From<EventCancelable> for bool {
    fn from(bubbles: EventCancelable) -> Self {
        match bubbles {
            EventCancelable::Cancelable => true,
            EventCancelable::NotCancelable => false,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, JSTraceable, PartialEq)]
#[repr(u16)]
#[derive(MallocSizeOf)]
pub enum EventPhase {
    None = EventConstants::NONE,
    Capturing = EventConstants::CAPTURING_PHASE,
    AtTarget = EventConstants::AT_TARGET,
    Bubbling = EventConstants::BUBBLING_PHASE,
}

/// An enum to indicate whether the default action of an event is allowed.
///
/// This should've been a bool. Instead, it's an enum, because, aside from the allowed/canceled
/// states, we also need something to stop the event from being handled again (without cancelling
/// the event entirely). For example, an Up/Down `KeyEvent` inside a `textarea` element will
/// trigger the cursor to go up/down if the text inside the element spans multiple lines. This enum
/// helps us to prevent such events from being [sent to the constellation][msg] where it will be
/// handled once again for page scrolling (which is definitely not what we'd want).
///
/// [msg]: https://doc.servo.org/compositing/enum.ConstellationMsg.html#variant.KeyEvent
///
#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq)]
pub enum EventDefault {
    /// The default action of the event is allowed (constructor's default)
    Allowed,
    /// The default action has been prevented by calling `PreventDefault`
    Prevented,
    /// The event has been handled somewhere in the DOM, and it should be prevented from being
    /// re-handled elsewhere. This doesn't affect the judgement of `DefaultPrevented`
    Handled,
}

#[derive(PartialEq)]
pub enum EventStatus {
    Canceled,
    NotCanceled,
}

/// <https://dom.spec.whatwg.org/#concept-event-fire>
pub struct EventTask {
    pub target: Trusted<EventTarget>,
    pub name: Atom,
    pub bubbles: EventBubbles,
    pub cancelable: EventCancelable,
}

impl TaskOnce for EventTask {
    fn run_once(self) {
        let target = self.target.root();
        let bubbles = self.bubbles;
        let cancelable = self.cancelable;
        target.fire_event_with_params(self.name, bubbles, cancelable, CanGc::note());
    }
}

/// <https://html.spec.whatwg.org/multipage/#fire-a-simple-event>
pub struct SimpleEventTask {
    pub target: Trusted<EventTarget>,
    pub name: Atom,
}

impl TaskOnce for SimpleEventTask {
    fn run_once(self) {
        let target = self.target.root();
        target.fire_event(self.name, CanGc::note());
    }
}

/// <https://dom.spec.whatwg.org/#concept-event-listener-invoke>
fn invoke(
    timeline_window: Option<&Window>,
    object: &EventTarget,
    event: &Event,
    phase: Option<ListenerPhase>,
    can_gc: CanGc,
    // TODO legacy_output_did_listeners_throw for indexeddb
) {
    // Step 1: Until shadow DOM puts the event path in the
    // event itself, this is easier to do in dispatch before
    // calling invoke.

    // Step 2 TODO: relatedTarget only matters for shadow DOM

    // Step 3 TODO: touch target lists not implemented

    // Step 4.
    if event.stop_propagation.get() {
        return;
    }
    // Step 5.
    event.current_target.set(Some(object));

    // Step 6
    let listeners = object.get_listeners_for(&event.type_(), phase, can_gc);

    // Step 7.
    let found = inner_invoke(timeline_window, object, event, &listeners);

    // Step 8
    if !found && event.trusted.get() {
        if let Some(legacy_type) = match event.type_() {
            atom!("animationend") => Some(atom!("webkitAnimationEnd")),
            atom!("animationiteration") => Some(atom!("webkitAnimationIteration")),
            atom!("animationstart") => Some(atom!("webkitAnimationStart")),
            atom!("transitionend") => Some(atom!("webkitTransitionEnd")),
            atom!("transitionrun") => Some(atom!("webkitTransitionRun")),
            _ => None,
        } {
            let original_type = event.type_();
            *event.type_.borrow_mut() = legacy_type;
            inner_invoke(timeline_window, object, event, &listeners);
            *event.type_.borrow_mut() = original_type;
        }
    }
}

/// <https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke>
fn inner_invoke(
    timeline_window: Option<&Window>,
    object: &EventTarget,
    event: &Event,
    listeners: &[CompiledEventListener],
) -> bool {
    // Step 1.
    let mut found = false;

    // Step 2.
    for listener in listeners {
        // FIXME(#25479): We need an "if !listener.removed()" here,
        // but there's a subtlety. Where Servo is currently using the
        // CompiledEventListener, we really need something that maps to
        // https://dom.spec.whatwg.org/#concept-event-listener
        // which is not the same thing as the EventListener interface.
        // script::dom::eventtarget::EventListenerEntry is the closest
        // match we have, and is already holding the "once" flag,
        // but it's not a drop-in replacement.

        // Steps 2.1 and 2.3-2.4 are not done because `listeners` contain only the
        // relevant ones for this invoke call during the dispatch algorithm.

        // Step 2.2.
        found = true;

        // Step 2.5.
        if let CompiledEventListener::Listener(event_listener) = listener {
            object.remove_listener_if_once(&event.type_(), event_listener);
        }

        // Step 2.6
        let global = listener.associated_global();

        // Step 2.7-2.8
        let current_event = if let Some(window) = global.downcast::<Window>() {
            window.set_current_event(Some(event))
        } else {
            None
        };

        // Step 2.9 TODO: EventListener passive option not implemented

        // Step 2.10
        let marker = TimelineMarker::start("DOMEvent".to_owned());

        // Step 2.10
        listener.call_or_handle_event(object, event, ExceptionHandling::Report);

        if let Some(window) = timeline_window {
            window.emit_timeline_marker(marker.end());
        }

        // Step 2.11 TODO: passive not implemented

        // Step 2.12
        if let Some(window) = global.downcast::<Window>() {
            window.set_current_event(current_event.as_deref());
        }

        // Step 2.13: short-circuit instead of going to next listener
        if event.stop_immediate.get() {
            return found;
        }
    }

    // Step 3.
    found
}

impl Default for EventBinding::EventInit {
    fn default() -> Self {
        Self::empty()
    }
}