script/dom/
formdataevent.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::FormDataEventBinding;
11use crate::dom::bindings::codegen::Bindings::FormDataEventBinding::FormDataEventMethods;
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::{Dom, DomRoot};
16use crate::dom::bindings::str::DOMString;
17use crate::dom::event::{Event, EventBubbles, EventCancelable};
18use crate::dom::formdata::FormData;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct FormDataEvent {
24    event: Event,
25    form_data: Dom<FormData>,
26}
27
28impl FormDataEvent {
29    pub(crate) fn new(
30        window: &Window,
31        type_: Atom,
32        can_bubble: EventBubbles,
33        cancelable: EventCancelable,
34        form_data: &FormData,
35        can_gc: CanGc,
36    ) -> DomRoot<FormDataEvent> {
37        Self::new_with_proto(
38            window, None, type_, can_bubble, cancelable, form_data, can_gc,
39        )
40    }
41
42    fn new_with_proto(
43        window: &Window,
44        proto: Option<HandleObject>,
45        type_: Atom,
46        can_bubble: EventBubbles,
47        cancelable: EventCancelable,
48        form_data: &FormData,
49        can_gc: CanGc,
50    ) -> DomRoot<FormDataEvent> {
51        let ev = reflect_dom_object_with_proto(
52            Box::new(FormDataEvent {
53                event: Event::new_inherited(),
54                form_data: Dom::from_ref(form_data),
55            }),
56            window,
57            proto,
58            can_gc,
59        );
60
61        {
62            let event = ev.upcast::<Event>();
63            event.init_event(type_, bool::from(can_bubble), bool::from(cancelable));
64        }
65        ev
66    }
67}
68
69impl FormDataEventMethods<crate::DomTypeHolder> for FormDataEvent {
70    // https://html.spec.whatwg.org/multipage/#formdataevent
71    fn Constructor(
72        window: &Window,
73        proto: Option<HandleObject>,
74        can_gc: CanGc,
75        type_: DOMString,
76        init: &FormDataEventBinding::FormDataEventInit,
77    ) -> Fallible<DomRoot<FormDataEvent>> {
78        let bubbles = EventBubbles::from(init.parent.bubbles);
79        let cancelable = EventCancelable::from(init.parent.cancelable);
80
81        let event = FormDataEvent::new_with_proto(
82            window,
83            proto,
84            Atom::from(type_),
85            bubbles,
86            cancelable,
87            &init.formData.clone(),
88            can_gc,
89        );
90
91        Ok(event)
92    }
93
94    // https://html.spec.whatwg.org/multipage/#dom-formdataevent-formdata
95    fn FormData(&self) -> DomRoot<FormData> {
96        DomRoot::from_ref(&*self.form_data)
97    }
98
99    // https://dom.spec.whatwg.org/#dom-event-istrusted
100    fn IsTrusted(&self) -> bool {
101        self.event.IsTrusted()
102    }
103}