script/dom/
clipboardevent.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::ClipboardEventBinding::{
9    ClipboardEventInit, ClipboardEventMethods,
10};
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
14use crate::dom::bindings::root::{DomRoot, MutNullableDom};
15use crate::dom::bindings::str::DOMString;
16use crate::dom::datatransfer::DataTransfer;
17use crate::dom::event::{Event, EventBubbles, EventCancelable};
18use crate::dom::window::Window;
19use crate::script_runtime::CanGc;
20
21/// The types of clipboard events in the Clipboard APIs specification:
22/// <https://www.w3.org/TR/clipboard-apis/#clipboard-actions>.
23#[derive(Clone, Debug)]
24pub(crate) enum ClipboardEventType {
25    Change,
26    Copy,
27    Cut,
28    Paste,
29}
30
31impl ClipboardEventType {
32    /// Convert this [`ClipboardEventType`] to a `&str` for use in creating DOM events.
33    pub(crate) fn as_str(&self) -> &str {
34        match *self {
35            ClipboardEventType::Change => "clipboardchange",
36            ClipboardEventType::Copy => "copy",
37            ClipboardEventType::Cut => "cut",
38            ClipboardEventType::Paste => "paste",
39        }
40    }
41}
42
43#[dom_struct]
44pub(crate) struct ClipboardEvent {
45    event: Event,
46    clipboard_data: MutNullableDom<DataTransfer>,
47}
48
49impl ClipboardEvent {
50    fn new_inherited() -> ClipboardEvent {
51        ClipboardEvent {
52            event: Event::new_inherited(),
53            clipboard_data: MutNullableDom::new(None),
54        }
55    }
56
57    pub(crate) fn new(
58        window: &Window,
59        proto: Option<HandleObject>,
60        type_: DOMString,
61        can_bubble: EventBubbles,
62        cancelable: EventCancelable,
63        clipboard_data: Option<&DataTransfer>,
64        can_gc: CanGc,
65    ) -> DomRoot<ClipboardEvent> {
66        let ev = reflect_dom_object_with_proto(
67            Box::new(ClipboardEvent::new_inherited()),
68            window,
69            proto,
70            can_gc,
71        );
72        ev.upcast::<Event>()
73            .InitEvent(type_, bool::from(can_bubble), bool::from(cancelable));
74        ev.clipboard_data.set(clipboard_data);
75        ev
76    }
77
78    pub(crate) fn set_clipboard_data(&self, clipboard_data: Option<&DataTransfer>) {
79        self.clipboard_data.set(clipboard_data);
80    }
81
82    pub(crate) fn get_clipboard_data(&self) -> Option<DomRoot<DataTransfer>> {
83        self.clipboard_data.get()
84    }
85}
86
87impl ClipboardEventMethods<crate::DomTypeHolder> for ClipboardEvent {
88    /// <https://www.w3.org/TR/clipboard-apis/#dom-clipboardevent-clipboardevent>
89    fn Constructor(
90        window: &Window,
91        proto: Option<HandleObject>,
92        can_gc: CanGc,
93        type_: DOMString,
94        init: &ClipboardEventInit,
95    ) -> DomRoot<ClipboardEvent> {
96        let bubbles = EventBubbles::from(init.parent.bubbles);
97        let cancelable = EventCancelable::from(init.parent.cancelable);
98        let event = ClipboardEvent::new(
99            window,
100            proto,
101            type_,
102            bubbles,
103            cancelable,
104            init.clipboardData.as_deref(),
105            can_gc,
106        );
107        event.upcast::<Event>().set_composed(init.parent.composed);
108        event
109    }
110
111    /// <https://www.w3.org/TR/clipboard-apis/#dom-clipboardevent-clipboarddata>
112    fn GetClipboardData(&self) -> Option<DomRoot<DataTransfer>> {
113        self.clipboard_data.get()
114    }
115
116    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
117    fn IsTrusted(&self) -> bool {
118        self.event.IsTrusted()
119    }
120}