script/dom/
compositionevent.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 style::Atom;
8
9use crate::dom::bindings::codegen::Bindings::CompositionEventBinding::{
10    self, CompositionEventMethods,
11};
12use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEvent_Binding::UIEventMethods;
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::reflector::{reflect_dom_object, reflect_dom_object_with_proto};
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::uievent::UIEvent;
18use crate::dom::window::Window;
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct CompositionEvent {
23    uievent: UIEvent,
24    data: DOMString,
25}
26
27impl CompositionEvent {
28    pub(crate) fn new_inherited() -> CompositionEvent {
29        CompositionEvent {
30            uievent: UIEvent::new_inherited(),
31            data: DOMString::new(),
32        }
33    }
34
35    pub(crate) fn new_uninitialized(window: &Window, can_gc: CanGc) -> DomRoot<CompositionEvent> {
36        reflect_dom_object(Box::new(CompositionEvent::new_inherited()), window, can_gc)
37    }
38
39    #[expect(clippy::too_many_arguments)]
40    pub(crate) fn new(
41        window: &Window,
42        event_type: Atom,
43        can_bubble: bool,
44        cancelable: bool,
45        view: Option<&Window>,
46        detail: i32,
47        data: DOMString,
48        can_gc: CanGc,
49    ) -> DomRoot<CompositionEvent> {
50        Self::new_with_proto(
51            window, None, event_type, can_bubble, cancelable, view, detail, data, can_gc,
52        )
53    }
54
55    #[expect(clippy::too_many_arguments)]
56    fn new_with_proto(
57        window: &Window,
58        proto: Option<HandleObject>,
59        event_type: Atom,
60        can_bubble: bool,
61        cancelable: bool,
62        view: Option<&Window>,
63        detail: i32,
64        data: DOMString,
65        can_gc: CanGc,
66    ) -> DomRoot<CompositionEvent> {
67        let ev = reflect_dom_object_with_proto(
68            Box::new(CompositionEvent {
69                uievent: UIEvent::new_inherited(),
70                data,
71            }),
72            window,
73            proto,
74            can_gc,
75        );
76        ev.uievent
77            .init_event(event_type, can_bubble, cancelable, view, detail);
78        ev
79    }
80
81    pub(crate) fn data(&self) -> &DOMString {
82        &self.data
83    }
84}
85
86impl CompositionEventMethods<crate::DomTypeHolder> for CompositionEvent {
87    /// <https://w3c.github.io/uievents/#dom-compositionevent-compositionevent>
88    fn Constructor(
89        window: &Window,
90        proto: Option<HandleObject>,
91        can_gc: CanGc,
92        event_type: DOMString,
93        init: &CompositionEventBinding::CompositionEventInit,
94    ) -> Fallible<DomRoot<CompositionEvent>> {
95        let event = CompositionEvent::new_with_proto(
96            window,
97            proto,
98            event_type.into(),
99            init.parent.parent.bubbles,
100            init.parent.parent.cancelable,
101            init.parent.view.as_deref(),
102            init.parent.detail,
103            init.data.clone(),
104            can_gc,
105        );
106        Ok(event)
107    }
108
109    /// <https://w3c.github.io/uievents/#dom-compositionevent-data>
110    fn Data(&self) -> DOMString {
111        self.data.clone()
112    }
113
114    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
115    fn IsTrusted(&self) -> bool {
116        self.uievent.IsTrusted()
117    }
118}