script/dom/
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::rust::HandleObject;
7use stylo_atoms::Atom;
8
9use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
10use crate::dom::bindings::codegen::Bindings::SubmitEventBinding;
11use crate::dom::bindings::codegen::Bindings::SubmitEventBinding::SubmitEventMethods;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::event::Event;
17use crate::dom::html::htmlelement::HTMLElement;
18use crate::dom::window::Window;
19use crate::script_runtime::CanGc;
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        window: &Window,
37        type_: Atom,
38        bubbles: bool,
39        cancelable: bool,
40        submitter: Option<DomRoot<HTMLElement>>,
41        can_gc: CanGc,
42    ) -> DomRoot<SubmitEvent> {
43        Self::new_with_proto(window, None, type_, bubbles, cancelable, submitter, can_gc)
44    }
45
46    fn new_with_proto(
47        window: &Window,
48        proto: Option<HandleObject>,
49        type_: Atom,
50        bubbles: bool,
51        cancelable: bool,
52        submitter: Option<DomRoot<HTMLElement>>,
53        can_gc: CanGc,
54    ) -> DomRoot<SubmitEvent> {
55        let ev = reflect_dom_object_with_proto(
56            Box::new(SubmitEvent::new_inherited(submitter)),
57            window,
58            proto,
59            can_gc,
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        window: &Window,
73        proto: Option<HandleObject>,
74        can_gc: CanGc,
75        type_: DOMString,
76        init: &SubmitEventBinding::SubmitEventInit,
77    ) -> DomRoot<SubmitEvent> {
78        SubmitEvent::new_with_proto(
79            window,
80            proto,
81            Atom::from(type_),
82            init.parent.bubbles,
83            init.parent.cancelable,
84            init.submitter.as_ref().map(|s| DomRoot::from_ref(&**s)),
85            can_gc,
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}