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_and_cx;
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_and_cx(
47            Box::new(PopStateEvent::new_inherited()),
48            window,
49            proto,
50            cx,
51        )
52    }
53
54    fn new(
55        cx: &mut JSContext,
56        window: &Window,
57        proto: Option<HandleObject>,
58        type_: Atom,
59        bubbles: bool,
60        cancelable: bool,
61        state: HandleValue,
62    ) -> DomRoot<PopStateEvent> {
63        let ev = PopStateEvent::new_uninitialized(cx, window, proto);
64        ev.state.set(state.get());
65        {
66            let event = ev.upcast::<Event>();
67            event.init_event(type_, bubbles, cancelable);
68        }
69        ev
70    }
71
72    pub(crate) fn dispatch_jsval(
73        cx: &mut js::context::JSContext,
74        target: &EventTarget,
75        window: &Window,
76        state: HandleValue,
77    ) {
78        let event = PopStateEvent::new(cx, window, None, atom!("popstate"), false, false, state);
79        event.upcast::<Event>().fire(cx, target);
80    }
81}
82
83impl PopStateEventMethods<crate::DomTypeHolder> for PopStateEvent {
84    /// <https://html.spec.whatwg.org/multipage/#popstateevent>
85    fn Constructor(
86        cx: &mut JSContext,
87        window: &Window,
88        proto: Option<HandleObject>,
89        type_: DOMString,
90        init: RootedTraceableBox<PopStateEventBinding::PopStateEventInit>,
91    ) -> Fallible<DomRoot<PopStateEvent>> {
92        Ok(PopStateEvent::new(
93            cx,
94            window,
95            proto,
96            Atom::from(type_),
97            init.parent.bubbles,
98            init.parent.cancelable,
99            init.state.handle(),
100        ))
101    }
102
103    /// <https://html.spec.whatwg.org/multipage/#dom-popstateevent-state>
104    fn State(&self, mut retval: MutableHandleValue) {
105        retval.set(self.state.get())
106    }
107
108    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
109    fn IsTrusted(&self) -> bool {
110        self.event.IsTrusted()
111    }
112}