Skip to main content

script/dom/event/
submitevent.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::SubmitEventBinding;
13use crate::dom::bindings::codegen::Bindings::SubmitEventBinding::SubmitEventMethods;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::event::Event;
18use crate::dom::html::htmlelement::HTMLElement;
19use crate::dom::window::Window;
20
21#[dom_struct]
22pub(crate) struct SubmitEvent {
23    event: Event,
24    submitter: Option<DomRoot<HTMLElement>>,
25}
26
27impl SubmitEvent {
28    fn new_inherited(submitter: Option<DomRoot<HTMLElement>>) -> SubmitEvent {
29        SubmitEvent {
30            event: Event::new_inherited(),
31            submitter,
32        }
33    }
34
35    pub(crate) fn new(
36        cx: &mut JSContext,
37        window: &Window,
38        type_: Atom,
39        bubbles: bool,
40        cancelable: bool,
41        submitter: Option<DomRoot<HTMLElement>>,
42    ) -> DomRoot<SubmitEvent> {
43        Self::new_with_proto(cx, window, None, type_, bubbles, cancelable, submitter)
44    }
45
46    fn new_with_proto(
47        cx: &mut JSContext,
48        window: &Window,
49        proto: Option<HandleObject>,
50        type_: Atom,
51        bubbles: bool,
52        cancelable: bool,
53        submitter: Option<DomRoot<HTMLElement>>,
54    ) -> DomRoot<SubmitEvent> {
55        let ev = reflect_dom_object_with_proto_and_cx(
56            Box::new(SubmitEvent::new_inherited(submitter)),
57            window,
58            proto,
59            cx,
60        );
61        {
62            let event = ev.upcast::<Event>();
63            event.init_event(type_, bubbles, cancelable);
64        }
65        ev
66    }
67}
68
69impl SubmitEventMethods<crate::DomTypeHolder> for SubmitEvent {
70    /// <https://html.spec.whatwg.org/multipage/#submitevent>
71    fn Constructor(
72        cx: &mut JSContext,
73        window: &Window,
74        proto: Option<HandleObject>,
75        type_: DOMString,
76        init: &SubmitEventBinding::SubmitEventInit,
77    ) -> DomRoot<SubmitEvent> {
78        SubmitEvent::new_with_proto(
79            cx,
80            window,
81            proto,
82            Atom::from(type_),
83            init.parent.bubbles,
84            init.parent.cancelable,
85            init.submitter.as_ref().map(|s| DomRoot::from_ref(&**s)),
86        )
87    }
88
89    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
90    fn IsTrusted(&self) -> bool {
91        self.event.IsTrusted()
92    }
93
94    /// <https://html.spec.whatwg.org/multipage/#dom-submitevent-submitter>
95    fn GetSubmitter(&self) -> Option<DomRoot<HTMLElement>> {
96        self.submitter.as_ref().map(|s| DomRoot::from_ref(&**s))
97    }
98}