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