1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::RefCell;
use std::rc::Rc;
use dom_struct::dom_struct;
use js::rust::{HandleObject, MutableHandleValue};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::DataTransferBinding::DataTransferMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::datatransferitemlist::DataTransferItemList;
use crate::dom::element::Element;
use crate::dom::filelist::FileList;
use crate::dom::htmlimageelement::HTMLImageElement;
use crate::dom::window::Window;
use crate::drag_data_store::{DragDataStore, Mode};
use crate::script_runtime::{CanGc, JSContext};
const VALID_DROP_EFFECTS: [&str; 4] = ["none", "copy", "link", "move"];
const VALID_EFFECTS_ALLOWED: [&str; 9] = [
"none",
"copy",
"copyLink",
"copyMove",
"link",
"linkMove",
"move",
"all",
"uninitialized",
];
#[dom_struct]
pub struct DataTransfer {
reflector_: Reflector,
drop_effect: DomRefCell<DOMString>,
effect_allowed: DomRefCell<DOMString>,
items: Dom<DataTransferItemList>,
#[ignore_malloc_size_of = "Rc"]
#[no_trace]
data_store: Rc<RefCell<Option<DragDataStore>>>,
}
impl DataTransfer {
fn new_inherited(
data_store: Rc<RefCell<Option<DragDataStore>>>,
item_list: &DataTransferItemList,
) -> DataTransfer {
DataTransfer {
reflector_: Reflector::new(),
drop_effect: DomRefCell::new(DOMString::from("none")),
effect_allowed: DomRefCell::new(DOMString::from("none")),
items: Dom::from_ref(item_list),
data_store,
}
}
pub fn new_with_proto(
window: &Window,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<DataTransfer> {
let mut drag_data_store = DragDataStore::new();
drag_data_store.set_mode(Mode::ReadWrite);
let data_store = Rc::new(RefCell::new(Some(drag_data_store)));
let item_list = DataTransferItemList::new(window, Rc::clone(&data_store));
reflect_dom_object_with_proto(
Box::new(DataTransfer::new_inherited(data_store, &item_list)),
window,
proto,
can_gc,
)
}
}
impl DataTransferMethods<crate::DomTypeHolder> for DataTransfer {
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer>
fn Constructor(
window: &Window,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<DataTransfer> {
DataTransfer::new_with_proto(window, proto, can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-dropeffect>
fn DropEffect(&self) -> DOMString {
self.drop_effect.borrow().clone()
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-dropeffect>
fn SetDropEffect(&self, value: DOMString) {
if VALID_DROP_EFFECTS.contains(&value.as_ref()) {
*self.drop_effect.borrow_mut() = value;
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-effectallowed>
fn EffectAllowed(&self) -> DOMString {
self.effect_allowed.borrow().clone()
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-effectallowed>
fn SetEffectAllowed(&self, value: DOMString) {
if self
.data_store
.borrow()
.as_ref()
.is_some_and(|data_store| data_store.mode() == Mode::ReadWrite) &&
VALID_EFFECTS_ALLOWED.contains(&value.as_ref())
{
*self.drop_effect.borrow_mut() = value;
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-items>
fn Items(&self) -> DomRoot<DataTransferItemList> {
DomRoot::from_ref(&self.items)
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-setdragimage>
fn SetDragImage(&self, image: &Element, x: i32, y: i32) {
// Step 1 If the DataTransfer is no longer associated with a data store, return.
let mut option = self.data_store.borrow_mut();
let data_store = match option.as_mut() {
Some(value) => value,
None => return,
};
// Step 2 If the data store's mode is not the read/write mode, return.
if data_store.mode() != Mode::ReadWrite {
return;
}
// Step 3
if let Some(image) = image.downcast::<HTMLImageElement>() {
data_store.set_bitmap(image.image_data(), x, y);
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-types>
fn Types(&self, cx: JSContext, retval: MutableHandleValue) {
self.items.frozen_types(cx, retval);
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-getdata>
fn GetData(&self, mut format: DOMString) -> DOMString {
// Step 1 If the DataTransfer object is not associated with a drag data store, then return the empty string.
let option = self.data_store.borrow();
let data_store = match option.as_ref() {
Some(value) => value,
None => return DOMString::new(),
};
// Step 2 If the drag data store's mode is the protected mode, then return the empty string.
if data_store.mode() == Mode::Protected {
return DOMString::new();
}
// Step 3 Let format be the first argument, converted to ASCII lowercase.
format.make_ascii_lowercase();
// Step 4 Let convert-to-URL be false.
let mut convert_to_url = false;
let type_ = match format.as_ref() {
// Step 5 If format equals "text", change it to "text/plain".
"text" => DOMString::from("text/plain"),
// Step 6 If format equals "url", change it to "text/uri-list" and set convert-to-URL to true.
"url" => {
convert_to_url = true;
DOMString::from("text/uri-list")
},
_ => format,
};
let data = data_store.find_matching_text(&type_);
// Step 8
if let Some(result) = data {
// Step 9 If convert-to-URL is true, then parse result as appropriate for text/uri-list data,
// and then set result to the first URL from the list, if any, or the empty string otherwise.
if convert_to_url {
//TODO parse uri-list as [RFC2483]
}
// Step 10 Return result.
result
} else {
// Step 7 If there is no item in the drag data store item list
// whose kind is text and whose type string is equal to format, return the empty string.
DOMString::new()
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-setdata>
fn SetData(&self, format: DOMString, data: DOMString) {
// Step 1 If the DataTransfer object is no longer associated with a drag data store, return. Nothing happens.
let mut option = self.data_store.borrow_mut();
let data_store = match option.as_mut() {
Some(value) => value,
None => return,
};
// Step 2 If the drag data store's mode is not the read/write mode, return. Nothing happens.
if data_store.mode() != Mode::ReadWrite {
return;
}
data_store.set_data(format, data);
self.items.invalidate_frozen_types();
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-cleardata>
fn ClearData(&self, format: Option<DOMString>) {
// Step 1 If the DataTransfer is not associated with a data store, return.
let mut option = self.data_store.borrow_mut();
let data_store = match option.as_mut() {
Some(value) => value,
None => return,
};
// Step 2 If the data store is not in the read/write mode, return.
if data_store.mode() != Mode::ReadWrite {
return;
}
if data_store.clear_data(format) {
self.items.invalidate_frozen_types();
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-files>
fn Files(&self) -> DomRoot<FileList> {
// Step 1 Start with an empty list.
let mut files = Vec::new();
// Step 2 If the DataTransfer is not associated with a data store return the empty list.
if let Some(data_store) = self.data_store.borrow().as_ref() {
data_store.files(&self.global(), &mut files);
}
// Step 5
FileList::new(self.global().as_window(), files)
}
}