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