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::inheritance::Castable;
10use script_bindings::reflector::reflect_dom_object_with_proto;
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 source: Option<Dom<Element>>,
30 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 #[expect(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(cx, event, window, proto);
56 {
57 let event = event.upcast::<Event>();
58 event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
59 }
60 event
61 }
62
63 pub(crate) fn new(
64 cx: &mut JSContext,
65 window: &Window,
66 type_: Atom,
67 bubbles: EventBubbles,
68 cancelable: EventCancelable,
69 source: Option<&Element>,
70 command: DOMString,
71 ) -> DomRoot<CommandEvent> {
72 Self::new_with_proto(
73 cx, window, None, type_, bubbles, cancelable, source, command,
74 )
75 }
76}
77
78impl CommandEventMethods<crate::DomTypeHolder> for CommandEvent {
79 fn Constructor(
81 cx: &mut JSContext,
82 window: &Window,
83 proto: Option<HandleObject>,
84 type_: DOMString,
85 init: &CommandEventBinding::CommandEventInit,
86 ) -> Fallible<DomRoot<CommandEvent>> {
87 let bubbles = EventBubbles::from(init.parent.bubbles);
88 let cancelable = EventCancelable::from(init.parent.cancelable);
89 Ok(CommandEvent::new_with_proto(
90 cx,
91 window,
92 proto,
93 Atom::from(type_),
94 bubbles,
95 cancelable,
96 init.source.as_deref(),
97 init.command.clone(),
98 ))
99 }
100
101 fn IsTrusted(&self) -> bool {
103 self.event.IsTrusted()
104 }
105
106 fn Command(&self) -> DOMString {
108 self.command.clone()
110 }
111
112 fn GetSource(&self) -> Option<DomRoot<Element>> {
114 let source = self.source.as_ref()?;
116
117 if let Some(current_target) = self.event.GetCurrentTarget() {
118 let retargeted = source.upcast::<EventTarget>().retarget(¤t_target);
119 return retargeted.downcast::<Element>().map(DomRoot::from_ref);
120 }
121
122 let document = source.upcast::<Node>().GetOwnerDocument().unwrap();
123 let retargeted = source
124 .upcast::<EventTarget>()
125 .retarget(document.upcast::<EventTarget>());
126 retargeted.downcast::<Element>().map(DomRoot::from_ref)
127 }
128}