Skip to main content

script/dom/event/
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::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::TransitionEventBinding::{
13    TransitionEventInit, TransitionEventMethods,
14};
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::num::Finite;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::dom::event::Event;
21use crate::dom::window::Window;
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        cx: &mut JSContext,
44        window: &Window,
45        type_: Atom,
46        init: &TransitionEventInit,
47    ) -> DomRoot<TransitionEvent> {
48        Self::new_with_proto(cx, window, None, type_, init)
49    }
50
51    fn new_with_proto(
52        cx: &mut JSContext,
53        window: &Window,
54        proto: Option<HandleObject>,
55        type_: Atom,
56        init: &TransitionEventInit,
57    ) -> DomRoot<TransitionEvent> {
58        let ev = reflect_dom_object_with_proto_and_cx(
59            Box::new(TransitionEvent::new_inherited(init)),
60            window,
61            proto,
62            cx,
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        cx: &mut JSContext,
76        window: &Window,
77        proto: Option<HandleObject>,
78        type_: DOMString,
79        init: &TransitionEventInit,
80    ) -> Fallible<DomRoot<TransitionEvent>> {
81        Ok(TransitionEvent::new_with_proto(
82            cx,
83            window,
84            proto,
85            Atom::from(type_),
86            init,
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}