script/dom/
commandevent.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 script_bindings::codegen::GenericBindings::NodeBinding::NodeMethods;
8use script_bindings::inheritance::Castable;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::CommandEventBinding;
12use crate::dom::bindings::codegen::Bindings::CommandEventBinding::CommandEventMethods;
13use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::element::Element;
19use crate::dom::event::{Event, EventBubbles, EventCancelable};
20use crate::dom::eventtarget::EventTarget;
21use crate::dom::node::Node;
22use crate::dom::types::Window;
23use crate::script_runtime::CanGc;
24
25#[dom_struct]
26pub(crate) struct CommandEvent {
27    event: Event,
28    /// <https://html.spec.whatwg.org/multipage/#dom-commandevent-source>
29    source: Option<DomRoot<Element>>,
30    /// <https://html.spec.whatwg.org/multipage/#dom-commandevent-command>
31    command: DOMString,
32}
33
34impl CommandEvent {
35    pub(crate) fn new_inherited(
36        source: Option<DomRoot<Element>>,
37        command: DOMString,
38    ) -> CommandEvent {
39        CommandEvent {
40            event: Event::new_inherited(),
41            source,
42            command,
43        }
44    }
45
46    #[allow(clippy::too_many_arguments)]
47    fn new_with_proto(
48        window: &Window,
49        proto: Option<HandleObject>,
50        type_: Atom,
51        bubbles: EventBubbles,
52        cancelable: EventCancelable,
53        source: Option<DomRoot<Element>>,
54        command: DOMString,
55        can_gc: CanGc,
56    ) -> DomRoot<CommandEvent> {
57        let event = Box::new(CommandEvent::new_inherited(source, command));
58        let event = reflect_dom_object_with_proto(event, window, proto, can_gc);
59        {
60            let event = event.upcast::<Event>();
61            event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
62        }
63        event
64    }
65
66    #[allow(clippy::too_many_arguments)]
67    pub(crate) fn new(
68        window: &Window,
69        type_: Atom,
70        bubbles: EventBubbles,
71        cancelable: EventCancelable,
72        source: Option<DomRoot<Element>>,
73        command: DOMString,
74        can_gc: CanGc,
75    ) -> DomRoot<CommandEvent> {
76        Self::new_with_proto(
77            window, None, type_, bubbles, cancelable, source, command, can_gc,
78        )
79    }
80}
81
82impl CommandEventMethods<crate::DomTypeHolder> for CommandEvent {
83    /// <https://html.spec.whatwg.org/multipage/#commandevent>
84    fn Constructor(
85        window: &Window,
86        proto: Option<HandleObject>,
87        can_gc: CanGc,
88        type_: DOMString,
89        init: &CommandEventBinding::CommandEventInit,
90    ) -> Fallible<DomRoot<CommandEvent>> {
91        let bubbles = EventBubbles::from(init.parent.bubbles);
92        let cancelable = EventCancelable::from(init.parent.cancelable);
93        Ok(CommandEvent::new_with_proto(
94            window,
95            proto,
96            Atom::from(type_),
97            bubbles,
98            cancelable,
99            init.source.as_ref().map(|s| DomRoot::from_ref(&**s)),
100            init.command.clone(),
101            can_gc,
102        ))
103    }
104
105    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
106    fn IsTrusted(&self) -> bool {
107        self.event.IsTrusted()
108    }
109
110    /// <https://html.spec.whatwg.org/multipage/#dom-commandevent-command>
111    fn Command(&self) -> DOMString {
112        // The command attribute must return the value it was initialized to.
113        self.command.clone()
114    }
115
116    /// <https://html.spec.whatwg.org/multipage/#dom-commandevent-source>
117    fn GetSource(&self) -> Option<DomRoot<Element>> {
118        // The source getter steps are to return the result of retargeting source against this's currentTarget.
119        let source = self.source.as_ref()?;
120
121        if let Some(current_target) = self.event.GetCurrentTarget() {
122            let retargeted = source.upcast::<EventTarget>().retarget(&current_target);
123            return retargeted.downcast::<Element>().map(DomRoot::from_ref);
124        }
125
126        let document = source.upcast::<Node>().GetOwnerDocument().unwrap();
127        let retargeted = source
128            .upcast::<EventTarget>()
129            .retarget(document.upcast::<EventTarget>());
130        retargeted.downcast::<Element>().map(DomRoot::from_ref)
131    }
132}