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]
22#[allow(non_snake_case)]
23pub(crate) struct SubmitEvent {
24    event: Event,
25    submitter: Option<DomRoot<HTMLElement>>,
26}
27
28impl SubmitEvent {
29    fn new_inherited(submitter: Option<DomRoot<HTMLElement>>) -> SubmitEvent {
30        SubmitEvent {
31            event: Event::new_inherited(),
32            submitter,
33        }
34    }
35
36    pub(crate) fn new(
37        window: &Window,
38        type_: Atom,
39        bubbles: bool,
40        cancelable: bool,
41        submitter: Option<DomRoot<HTMLElement>>,
42        can_gc: CanGc,
43    ) -> DomRoot<SubmitEvent> {
44        Self::new_with_proto(window, None, type_, bubbles, cancelable, submitter, can_gc)
45    }
46
47    fn new_with_proto(
48        window: &Window,
49        proto: Option<HandleObject>,
50        type_: Atom,
51        bubbles: bool,
52        cancelable: bool,
53        submitter: Option<DomRoot<HTMLElement>>,
54        can_gc: CanGc,
55    ) -> DomRoot<SubmitEvent> {
56        let ev = reflect_dom_object_with_proto(
57            Box::new(SubmitEvent::new_inherited(submitter)),
58            window,
59            proto,
60            can_gc,
61        );
62        {
63            let event = ev.upcast::<Event>();
64            event.init_event(type_, bubbles, cancelable);
65        }
66        ev
67    }
68}
69
70impl SubmitEventMethods<crate::DomTypeHolder> for SubmitEvent {
71    /// <https://html.spec.whatwg.org/multipage/#submitevent>
72    fn Constructor(
73        window: &Window,
74        proto: Option<HandleObject>,
75        can_gc: CanGc,
76        type_: DOMString,
77        init: &SubmitEventBinding::SubmitEventInit,
78    ) -> DomRoot<SubmitEvent> {
79        SubmitEvent::new_with_proto(
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            can_gc,
87        )
88    }
89
90    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
91    fn IsTrusted(&self) -> bool {
92        self.event.IsTrusted()
93    }
94
95    /// <https://html.spec.whatwg.org/multipage/#dom-submitevent-submitter>
96    fn GetSubmitter(&self) -> Option<DomRoot<HTMLElement>> {
97        self.submitter.as_ref().map(|s| DomRoot::from_ref(&**s))
98    }
99}