Skip to main content

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