script/dom/event/
commandevent.rs1use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::codegen::GenericBindings::NodeBinding::NodeMethods;
9use script_bindings::domstring::TracedDOMString;
10use script_bindings::inheritance::Castable;
11use script_bindings::reflector::reflect_dom_object_with_proto;
12use stylo_atoms::Atom;
13
14use crate::dom::bindings::codegen::Bindings::CommandEventBinding;
15use crate::dom::bindings::codegen::Bindings::CommandEventBinding::CommandEventMethods;
16use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
17use crate::dom::bindings::error::Fallible;
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::dom::bindings::str::DOMString;
20use crate::dom::element::Element;
21use crate::dom::event::{Event, EventBubbles, EventCancelable};
22use crate::dom::eventtarget::EventTarget;
23use crate::dom::node::Node;
24use crate::dom::types::Window;
25
26#[dom_struct]
27pub(crate) struct CommandEvent {
28 event: Event,
29 source: Option<Dom<Element>>,
31 command: TracedDOMString,
33}
34
35impl CommandEvent {
36 pub(crate) fn new_inherited(source: Option<&Element>, command: DOMString) -> CommandEvent {
37 CommandEvent {
38 event: Event::new_inherited(),
39 source: source.map(Dom::from_ref),
40 command: command.traced(),
41 }
42 }
43
44 #[expect(clippy::too_many_arguments)]
45 fn new_with_proto(
46 cx: &mut JSContext,
47 window: &Window,
48 proto: Option<HandleObject>,
49 type_: Atom,
50 bubbles: EventBubbles,
51 cancelable: EventCancelable,
52 source: Option<&Element>,
53 command: DOMString,
54 ) -> DomRoot<CommandEvent> {
55 let event = Box::new(CommandEvent::new_inherited(source, command));
56 let event = reflect_dom_object_with_proto(cx, event, window, proto);
57 {
58 let event = event.upcast::<Event>();
59 event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
60 }
61 event
62 }
63
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 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 fn IsTrusted(&self) -> bool {
104 self.event.IsTrusted()
105 }
106
107 fn Command(&self) -> DOMString {
109 self.command.clone().root()
111 }
112
113 fn GetSource(&self) -> Option<DomRoot<Element>> {
115 let source = self.source.as_ref()?;
117
118 if let Some(current_target) = self.event.GetCurrentTarget() {
119 let retargeted = source.upcast::<EventTarget>().retarget(¤t_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}