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