Skip to main content

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