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    #[allow(dead_code)]
48    pub(crate) fn new(
49        window: &Window,
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        Self::new_with_proto(
58            window, None, type_, bubbles, cancelable, source, command, can_gc,
59        )
60    }
61
62    #[allow(clippy::too_many_arguments)]
63    fn new_with_proto(
64        window: &Window,
65        proto: Option<HandleObject>,
66        type_: Atom,
67        bubbles: EventBubbles,
68        cancelable: EventCancelable,
69        source: Option<DomRoot<Element>>,
70        command: DOMString,
71        can_gc: CanGc,
72    ) -> DomRoot<CommandEvent> {
73        let event = Box::new(CommandEvent::new_inherited(source, command));
74        let event = reflect_dom_object_with_proto(event, window, proto, can_gc);
75        {
76            let event = event.upcast::<Event>();
77            event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
78        }
79        event
80    }
81}
82
83impl CommandEventMethods<crate::DomTypeHolder> for CommandEvent {
84    /// <https://html.spec.whatwg.org/multipage/#commandevent>
85    fn Constructor(
86        window: &Window,
87        proto: Option<HandleObject>,
88        can_gc: CanGc,
89        type_: DOMString,
90        init: &CommandEventBinding::CommandEventInit,
91    ) -> Fallible<DomRoot<CommandEvent>> {
92        let bubbles = EventBubbles::from(init.parent.bubbles);
93        let cancelable = EventCancelable::from(init.parent.cancelable);
94        Ok(CommandEvent::new_with_proto(
95            window,
96            proto,
97            Atom::from(type_),
98            bubbles,
99            cancelable,
100            init.source.as_ref().map(|s| DomRoot::from_ref(&**s)),
101            init.command.clone(),
102            can_gc,
103        ))
104    }
105
106    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
107    fn IsTrusted(&self) -> bool {
108        self.event.IsTrusted()
109    }
110
111    /// <https://html.spec.whatwg.org/multipage/#dom-commandevent-command>
112    fn Command(&self) -> DOMString {
113        // The command attribute must return the value it was initialized to.
114        self.command.clone()
115    }
116
117    /// <https://html.spec.whatwg.org/multipage/#dom-commandevent-source>
118    fn GetSource(&self) -> Option<DomRoot<Element>> {
119        // The source getter steps are to return the result of retargeting source against this's currentTarget.
120        let source = self.source.as_ref()?;
121
122        if let Some(current_target) = self.event.GetCurrentTarget() {
123            let retargeted = source.upcast::<EventTarget>().retarget(&current_target);
124            return retargeted.downcast::<Element>().map(DomRoot::from_ref);
125        }
126
127        let document = source.upcast::<Node>().GetOwnerDocument().unwrap();
128        let retargeted = source
129            .upcast::<EventTarget>()
130            .retarget(document.upcast::<EventTarget>());
131        retargeted.downcast::<Element>().map(DomRoot::from_ref)
132    }
133}