Skip to main content

script/dom/event/
hashchangeevent.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::HashChangeEventBinding;
13use crate::dom::bindings::codegen::Bindings::HashChangeEventBinding::HashChangeEventMethods;
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::{DOMString, USVString};
18use crate::dom::event::Event;
19use crate::dom::window::Window;
20
21// https://html.spec.whatwg.org/multipage/#hashchangeevent
22#[dom_struct]
23pub(crate) struct HashChangeEvent {
24    event: Event,
25    old_url: String,
26    new_url: String,
27}
28
29impl HashChangeEvent {
30    fn new_inherited(old_url: String, new_url: String) -> HashChangeEvent {
31        HashChangeEvent {
32            event: Event::new_inherited(),
33            old_url,
34            new_url,
35        }
36    }
37
38    pub(crate) fn new_uninitialized(
39        cx: &mut JSContext,
40        window: &Window,
41    ) -> DomRoot<HashChangeEvent> {
42        Self::new_uninitialized_with_proto(cx, window, None)
43    }
44
45    fn new_uninitialized_with_proto(
46        cx: &mut JSContext,
47        window: &Window,
48        proto: Option<HandleObject>,
49    ) -> DomRoot<HashChangeEvent> {
50        reflect_dom_object_with_proto_and_cx(
51            Box::new(HashChangeEvent::new_inherited(String::new(), String::new())),
52            window,
53            proto,
54            cx,
55        )
56    }
57
58    pub(crate) fn new(
59        cx: &mut JSContext,
60        window: &Window,
61        type_: Atom,
62        bubbles: bool,
63        cancelable: bool,
64        old_url: String,
65        new_url: String,
66    ) -> DomRoot<HashChangeEvent> {
67        Self::new_with_proto(
68            cx, window, None, type_, bubbles, cancelable, old_url, new_url,
69        )
70    }
71
72    #[expect(clippy::too_many_arguments)]
73    fn new_with_proto(
74        cx: &mut JSContext,
75        window: &Window,
76        proto: Option<HandleObject>,
77        type_: Atom,
78        bubbles: bool,
79        cancelable: bool,
80        old_url: String,
81        new_url: String,
82    ) -> DomRoot<HashChangeEvent> {
83        let ev = reflect_dom_object_with_proto_and_cx(
84            Box::new(HashChangeEvent::new_inherited(old_url, new_url)),
85            window,
86            proto,
87            cx,
88        );
89        {
90            let event = ev.upcast::<Event>();
91            event.init_event(type_, bubbles, cancelable);
92        }
93        ev
94    }
95}
96
97impl HashChangeEventMethods<crate::DomTypeHolder> for HashChangeEvent {
98    /// <https://html.spec.whatwg.org/multipage/#hashchangeevent>
99    fn Constructor(
100        cx: &mut JSContext,
101        window: &Window,
102        proto: Option<HandleObject>,
103        type_: DOMString,
104        init: &HashChangeEventBinding::HashChangeEventInit,
105    ) -> Fallible<DomRoot<HashChangeEvent>> {
106        Ok(HashChangeEvent::new_with_proto(
107            cx,
108            window,
109            proto,
110            Atom::from(type_),
111            init.parent.bubbles,
112            init.parent.cancelable,
113            init.oldURL.0.clone(),
114            init.newURL.0.clone(),
115        ))
116    }
117
118    /// <https://html.spec.whatwg.org/multipage/#dom-hashchangeevent-oldurl>
119    fn OldURL(&self) -> USVString {
120        USVString(self.old_url.clone())
121    }
122
123    /// <https://html.spec.whatwg.org/multipage/#dom-hashchangeevent-newurl>
124    fn NewURL(&self) -> USVString {
125        USVString(self.new_url.clone())
126    }
127
128    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
129    fn IsTrusted(&self) -> bool {
130        self.event.IsTrusted()
131    }
132}