Skip to main content

script/dom/event/
popstateevent.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::jsapi::Heap;
8use js::jsval::JSVal;
9use js::rust::{HandleObject, HandleValue, MutableHandleValue};
10use script_bindings::reflector::reflect_dom_object_with_proto;
11use stylo_atoms::Atom;
12
13use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
14use crate::dom::bindings::codegen::Bindings::PopStateEventBinding;
15use crate::dom::bindings::codegen::Bindings::PopStateEventBinding::PopStateEventMethods;
16use crate::dom::bindings::error::Fallible;
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::dom::bindings::trace::RootedTraceableBox;
21use crate::dom::event::Event;
22use crate::dom::eventtarget::EventTarget;
23use crate::dom::window::Window;
24
25// https://html.spec.whatwg.org/multipage/#the-popstateevent-interface
26#[dom_struct]
27pub(crate) struct PopStateEvent {
28    event: Event,
29    #[ignore_malloc_size_of = "Defined in rust-mozjs"]
30    state: Heap<JSVal>,
31}
32
33impl PopStateEvent {
34    fn new_inherited() -> PopStateEvent {
35        PopStateEvent {
36            event: Event::new_inherited(),
37            state: Heap::default(),
38        }
39    }
40
41    fn new_uninitialized(
42        cx: &mut JSContext,
43        window: &Window,
44        proto: Option<HandleObject>,
45    ) -> DomRoot<PopStateEvent> {
46        reflect_dom_object_with_proto(cx, Box::new(PopStateEvent::new_inherited()), window, proto)
47    }
48
49    fn new(
50        cx: &mut JSContext,
51        window: &Window,
52        proto: Option<HandleObject>,
53        type_: Atom,
54        bubbles: bool,
55        cancelable: bool,
56        state: HandleValue,
57    ) -> DomRoot<PopStateEvent> {
58        let ev = PopStateEvent::new_uninitialized(cx, window, proto);
59        ev.state.set(state.get());
60        {
61            let event = ev.upcast::<Event>();
62            event.init_event(type_, bubbles, cancelable);
63        }
64        ev
65    }
66
67    pub(crate) fn dispatch_jsval(
68        cx: &mut js::context::JSContext,
69        target: &EventTarget,
70        window: &Window,
71        state: HandleValue,
72    ) {
73        let event = PopStateEvent::new(cx, window, None, atom!("popstate"), false, false, state);
74        event.upcast::<Event>().fire(cx, target);
75    }
76}
77
78impl PopStateEventMethods<crate::DomTypeHolder> for PopStateEvent {
79    /// <https://html.spec.whatwg.org/multipage/#popstateevent>
80    fn Constructor(
81        cx: &mut JSContext,
82        window: &Window,
83        proto: Option<HandleObject>,
84        type_: DOMString,
85        init: RootedTraceableBox<PopStateEventBinding::PopStateEventInit>,
86    ) -> Fallible<DomRoot<PopStateEvent>> {
87        Ok(PopStateEvent::new(
88            cx,
89            window,
90            proto,
91            Atom::from(type_),
92            init.parent.bubbles,
93            init.parent.cancelable,
94            init.state.handle(),
95        ))
96    }
97
98    /// <https://html.spec.whatwg.org/multipage/#dom-popstateevent-state>
99    fn State(&self, mut retval: MutableHandleValue) {
100        retval.set(self.state.get())
101    }
102
103    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
104    fn IsTrusted(&self) -> bool {
105        self.event.IsTrusted()
106    }
107}