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
67impl CommandEventMethods<crate::DomTypeHolder> for CommandEvent {
68    /// <https://html.spec.whatwg.org/multipage/#commandevent>
69    fn Constructor(
70        window: &Window,
71        proto: Option<HandleObject>,
72        can_gc: CanGc,
73        type_: DOMString,
74        init: &CommandEventBinding::CommandEventInit,
75    ) -> Fallible<DomRoot<CommandEvent>> {
76        let bubbles = EventBubbles::from(init.parent.bubbles);
77        let cancelable = EventCancelable::from(init.parent.cancelable);
78        Ok(CommandEvent::new_with_proto(
79            window,
80            proto,
81            Atom::from(type_),
82            bubbles,
83            cancelable,
84            init.source.as_ref().map(|s| DomRoot::from_ref(&**s)),
85            init.command.clone(),
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-commandevent-command>
96    fn Command(&self) -> DOMString {
97        // The command attribute must return the value it was initialized to.
98        self.command.clone()
99    }
100
101    /// <https://html.spec.whatwg.org/multipage/#dom-commandevent-source>
102    fn GetSource(&self) -> Option<DomRoot<Element>> {
103        // The source getter steps are to return the result of retargeting source against this's currentTarget.
104        let source = self.source.as_ref()?;
105
106        if let Some(current_target) = self.event.GetCurrentTarget() {
107            let retargeted = source.upcast::<EventTarget>().retarget(&current_target);
108            return retargeted.downcast::<Element>().map(DomRoot::from_ref);
109        }
110
111        let document = source.upcast::<Node>().GetOwnerDocument().unwrap();
112        let retargeted = source
113            .upcast::<EventTarget>()
114            .retarget(document.upcast::<EventTarget>());
115        retargeted.downcast::<Element>().map(DomRoot::from_ref)
116    }
117}