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::domstring::TracedDOMString;
9use script_bindings::reflector::reflect_dom_object_with_proto;
10use stylo_atoms::Atom;
11
12use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
13use crate::dom::bindings::codegen::Bindings::TransitionEventBinding::{
14    TransitionEventInit, TransitionEventMethods,
15};
16use crate::dom::bindings::error::Fallible;
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::num::Finite;
19use crate::dom::bindings::root::DomRoot;
20use crate::dom::bindings::str::DOMString;
21use crate::dom::event::Event;
22use crate::dom::window::Window;
23
24#[dom_struct]
25pub(crate) struct TransitionEvent {
26    event: Event,
27    #[no_trace]
28    property_name: Atom,
29    elapsed_time: Finite<f32>,
30    pseudo_element: TracedDOMString,
31}
32
33impl TransitionEvent {
34    fn new_inherited(init: &TransitionEventInit) -> TransitionEvent {
35        TransitionEvent {
36            event: Event::new_inherited(),
37            property_name: Atom::from(init.propertyName.clone()),
38            elapsed_time: init.elapsedTime,
39            pseudo_element: init.pseudoElement.clone().traced(),
40        }
41    }
42
43    pub(crate) fn new(
44        cx: &mut JSContext,
45        window: &Window,
46        type_: Atom,
47        init: &TransitionEventInit,
48    ) -> DomRoot<TransitionEvent> {
49        Self::new_with_proto(cx, window, None, type_, init)
50    }
51
52    fn new_with_proto(
53        cx: &mut JSContext,
54        window: &Window,
55        proto: Option<HandleObject>,
56        type_: Atom,
57        init: &TransitionEventInit,
58    ) -> DomRoot<TransitionEvent> {
59        let ev = reflect_dom_object_with_proto(
60            cx,
61            Box::new(TransitionEvent::new_inherited(init)),
62            window,
63            proto,
64        );
65        {
66            let event = ev.upcast::<Event>();
67            event.init_event(type_, init.parent.bubbles, init.parent.cancelable);
68        }
69        ev
70    }
71}
72
73impl TransitionEventMethods<crate::DomTypeHolder> for TransitionEvent {
74    /// <https://drafts.csswg.org/css-transitions/#dom-transitionevent-transitionevent>
75    fn Constructor(
76        cx: &mut JSContext,
77        window: &Window,
78        proto: Option<HandleObject>,
79        type_: DOMString,
80        init: &TransitionEventInit,
81    ) -> Fallible<DomRoot<TransitionEvent>> {
82        Ok(TransitionEvent::new_with_proto(
83            cx,
84            window,
85            proto,
86            Atom::from(type_),
87            init,
88        ))
89    }
90
91    /// <https://drafts.csswg.org/css-transitions/#Events-TransitionEvent-propertyName>
92    fn PropertyName(&self) -> DOMString {
93        TracedDOMString::from(self.property_name.clone()).root()
94    }
95
96    /// <https://drafts.csswg.org/css-transitions/#Events-TransitionEvent-elapsedTime>
97    fn ElapsedTime(&self) -> Finite<f32> {
98        self.elapsed_time
99    }
100
101    /// <https://drafts.csswg.org/css-transitions/#Events-TransitionEvent-pseudoElement>
102    fn PseudoElement(&self) -> DOMString {
103        self.pseudo_element.clone().root()
104    }
105
106    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
107    fn IsTrusted(&self) -> bool {
108        self.upcast::<Event>().IsTrusted()
109    }
110}