script/dom/
transitionevent.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::rust::HandleObject;
7use stylo_atoms::Atom;
8
9use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
10use crate::dom::bindings::codegen::Bindings::TransitionEventBinding::{
11    TransitionEventInit, TransitionEventMethods,
12};
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::num::Finite;
16use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::event::Event;
20use crate::dom::window::Window;
21use crate::script_runtime::CanGc;
22
23#[dom_struct]
24pub(crate) struct TransitionEvent {
25    event: Event,
26    #[no_trace]
27    property_name: Atom,
28    elapsed_time: Finite<f32>,
29    pseudo_element: DOMString,
30}
31
32impl TransitionEvent {
33    fn new_inherited(init: &TransitionEventInit) -> TransitionEvent {
34        TransitionEvent {
35            event: Event::new_inherited(),
36            property_name: Atom::from(init.propertyName.clone()),
37            elapsed_time: init.elapsedTime,
38            pseudo_element: init.pseudoElement.clone(),
39        }
40    }
41
42    pub(crate) fn new(
43        window: &Window,
44        type_: Atom,
45        init: &TransitionEventInit,
46        can_gc: CanGc,
47    ) -> DomRoot<TransitionEvent> {
48        Self::new_with_proto(window, None, type_, init, can_gc)
49    }
50
51    fn new_with_proto(
52        window: &Window,
53        proto: Option<HandleObject>,
54        type_: Atom,
55        init: &TransitionEventInit,
56        can_gc: CanGc,
57    ) -> DomRoot<TransitionEvent> {
58        let ev = reflect_dom_object_with_proto(
59            Box::new(TransitionEvent::new_inherited(init)),
60            window,
61            proto,
62            can_gc,
63        );
64        {
65            let event = ev.upcast::<Event>();
66            event.init_event(type_, init.parent.bubbles, init.parent.cancelable);
67        }
68        ev
69    }
70}
71
72impl TransitionEventMethods<crate::DomTypeHolder> for TransitionEvent {
73    // https://drafts.csswg.org/css-transitions/#dom-transitionevent-transitionevent
74    fn Constructor(
75        window: &Window,
76        proto: Option<HandleObject>,
77        can_gc: CanGc,
78        type_: DOMString,
79        init: &TransitionEventInit,
80    ) -> Fallible<DomRoot<TransitionEvent>> {
81        Ok(TransitionEvent::new_with_proto(
82            window,
83            proto,
84            Atom::from(type_),
85            init,
86            can_gc,
87        ))
88    }
89
90    // https://drafts.csswg.org/css-transitions/#Events-TransitionEvent-propertyName
91    fn PropertyName(&self) -> DOMString {
92        DOMString::from(&*self.property_name)
93    }
94
95    // https://drafts.csswg.org/css-transitions/#Events-TransitionEvent-elapsedTime
96    fn ElapsedTime(&self) -> Finite<f32> {
97        self.elapsed_time
98    }
99
100    // https://drafts.csswg.org/css-transitions/#Events-TransitionEvent-pseudoElement
101    fn PseudoElement(&self) -> DOMString {
102        self.pseudo_element.clone()
103    }
104
105    // https://dom.spec.whatwg.org/#dom-event-istrusted
106    fn IsTrusted(&self) -> bool {
107        self.upcast::<Event>().IsTrusted()
108    }
109}