DataTransferItem: improve spec compliance (#35418)

* DataTransfer: remove PlainString and Binary structs

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>

* add DataTransferItemList remove() test case

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>

* bring datatransferitem closer to the spec

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>

* queue a task to invoke the callback

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>

---------

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>
This commit is contained in:
Gae24 2025-02-16 19:53:35 +01:00 committed by GitHub
parent 0e9bebce0f
commit 966888615f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 198 additions and 125 deletions

View file

@ -2,74 +2,146 @@
* 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::{Cell, Ref, RefCell};
use std::rc::Rc;
use dom_struct::dom_struct;
use crate::dom::bindings::callback::ExceptionHandling;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::DataTransferItemBinding::{
DataTransferItemMethods, FunctionStringCallback,
};
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{reflect_dom_object, DomGlobal, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::file::File;
use crate::dom::globalscope::GlobalScope;
use crate::drag_data_store::Kind;
use crate::drag_data_store::{DragDataStore, Kind, Mode};
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct DataTransferItem {
reflector_: Reflector,
#[ignore_malloc_size_of = "TODO"]
#[ignore_malloc_size_of = "Rc"]
#[no_trace]
item: Kind,
data_store: Rc<RefCell<Option<DragDataStore>>>,
id: u16,
pending_callbacks: DomRefCell<Vec<PendingStringCallback>>,
next_callback: Cell<usize>,
}
#[derive(JSTraceable, MallocSizeOf)]
struct PendingStringCallback {
id: usize,
#[ignore_malloc_size_of = "Rc"]
callback: Rc<FunctionStringCallback>,
}
impl DataTransferItem {
fn new_inherited(item: Kind) -> DataTransferItem {
fn new_inherited(data_store: Rc<RefCell<Option<DragDataStore>>>, id: u16) -> DataTransferItem {
DataTransferItem {
reflector_: Reflector::new(),
item,
data_store,
id,
pending_callbacks: Default::default(),
next_callback: Cell::new(0),
}
}
pub(crate) fn new(
global: &GlobalScope,
data_store: Rc<RefCell<Option<DragDataStore>>>,
id: u16,
can_gc: CanGc,
item: Kind,
) -> DomRoot<DataTransferItem> {
reflect_dom_object(
Box::new(DataTransferItem::new_inherited(item)),
Box::new(DataTransferItem::new_inherited(data_store, id)),
global,
can_gc,
)
}
fn item_kind(&self) -> Option<Ref<Kind>> {
Ref::filter_map(self.data_store.borrow(), |data_store| {
data_store
.as_ref()
.and_then(|data_store| data_store.get_by_id(&self.id))
})
.ok()
}
fn can_read(&self) -> bool {
self.data_store
.borrow()
.as_ref()
.is_some_and(|data_store| data_store.mode() != Mode::Protected)
}
}
impl DataTransferItemMethods<crate::DomTypeHolder> for DataTransferItem {
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitem-kind>
fn Kind(&self) -> DOMString {
match self.item {
Kind::Text(_) => DOMString::from("string"),
Kind::File(_) => DOMString::from("file"),
}
self.item_kind()
.map_or(DOMString::new(), |item| match *item {
Kind::Text { .. } => DOMString::from("string"),
Kind::File { .. } => DOMString::from("file"),
})
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitem-type>
fn Type(&self) -> DOMString {
self.item.type_()
self.item_kind()
.map_or(DOMString::new(), |item| item.type_())
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitem-getasstring>
fn GetAsString(&self, callback: Option<Rc<FunctionStringCallback>>) {
if let (Some(callback), Some(data)) = (callback, self.item.as_string()) {
let _ = callback.Call__(data, ExceptionHandling::Report);
// Step 1 If the callback is null, return.
let Some(callback) = callback else {
return;
};
// Step 2 If the DataTransferItem object is not in the read/write mode or the read-only mode, return.
if !self.can_read() {
return;
}
// Step 3 If the drag data item kind is not text, then return.
if let Some(string) = self.item_kind().and_then(|item| item.as_string()) {
let id = self.next_callback.get();
let pending_callback = PendingStringCallback { id, callback };
self.pending_callbacks.borrow_mut().push(pending_callback);
self.next_callback.set(id + 1);
let this = Trusted::new(self);
// Step 4 Otherwise, queue a task to invoke callback,
// passing the actual data of the item represented by the DataTransferItem object as the argument.
self.global()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(invoke_callback: move || {
if let Some(index) = this.root().pending_callbacks.borrow().iter().position(|val| val.id == id) {
let callback = this.root().pending_callbacks.borrow_mut().swap_remove(index).callback;
let _ = callback.Call__(DOMString::from(string), ExceptionHandling::Report);
}
}));
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitem-getasfile>
fn GetAsFile(&self, can_gc: CanGc) -> Option<DomRoot<File>> {
self.item.as_file(&self.global(), can_gc)
// Step 1 If the DataTransferItem object is not in the read/write mode or the read-only mode, then return null.
if !self.can_read() {
return None;
}
// Step 2 If the drag data item kind is not File, then return null.
// Step 3 Return a new File object representing the actual data
// of the item represented by the DataTransferItem object.
self.item_kind()
.and_then(|item| item.as_file(&self.global(), can_gc))
}
}

View file

@ -17,7 +17,7 @@ use crate::dom::bindings::str::DOMString;
use crate::dom::datatransferitem::DataTransferItem;
use crate::dom::file::File;
use crate::dom::window::Window;
use crate::drag_data_store::{Binary, DragDataStore, Kind, Mode, PlainString};
use crate::drag_data_store::{DragDataStore, Kind, Mode};
use crate::script_runtime::{CanGc, JSContext};
#[dom_struct]
@ -90,9 +90,9 @@ impl DataTransferItemListMethods<crate::DomTypeHolder> for DataTransferItemList
};
// Step 2
data_store
.get_item(index as usize)
.map(|item| DataTransferItem::new(&self.global(), can_gc, item))
data_store.get_by_index(index as usize).map(|(id, _)| {
DataTransferItem::new(&self.global(), Rc::clone(&self.data_store), *id, can_gc)
})
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitemlist-add>
@ -113,18 +113,18 @@ impl DataTransferItemListMethods<crate::DomTypeHolder> for DataTransferItemList
// whose type string is equal to the value of the method's second argument, converted to ASCII lowercase,
// and whose data is the string given by the method's first argument.
type_.make_ascii_lowercase();
data_store.add(Kind::Text(PlainString::new(data, type_)))?;
data_store.add(Kind::Text { data, type_ }).map(|id| {
self.frozen_types.clear();
self.frozen_types.clear();
// Step 3 Determine the value of the indexed property corresponding to the newly added item,
// and return that value (a newly created DataTransferItem object).
let index = data_store.list_len() - 1;
let item = data_store
.get_item(index)
.map(|item| DataTransferItem::new(&self.global(), can_gc, item));
Ok(item)
// Step 3 Determine the value of the indexed property corresponding to the newly added item,
// and return that value (a newly created DataTransferItem object).
Some(DataTransferItem::new(
&self.global(),
Rc::clone(&self.data_store),
id,
can_gc,
))
})
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitemlist-add>
@ -141,24 +141,21 @@ impl DataTransferItemListMethods<crate::DomTypeHolder> for DataTransferItemList
// and whose data is the same as the File's data.
let mut type_ = data.file_type();
type_.make_ascii_lowercase();
let binary = Binary::new(
data.file_bytes().unwrap_or_default(),
data.name().clone(),
type_,
);
let bytes = data.file_bytes().unwrap_or_default();
let name = data.name().clone();
data_store.add(Kind::File(binary))?;
data_store.add(Kind::File { bytes, name, type_ }).map(|id| {
self.frozen_types.clear();
self.frozen_types.clear();
// Step 3 Determine the value of the indexed property corresponding to the newly added item,
// and return that value (a newly created DataTransferItem object).
let index = data_store.list_len() - 1;
let item = data_store
.get_item(index)
.map(|item| DataTransferItem::new(&self.global(), can_gc, item));
Ok(item)
// Step 3 Determine the value of the indexed property corresponding to the newly added item,
// and return that value (a newly created DataTransferItem object).
Some(DataTransferItem::new(
&self.global(),
Rc::clone(&self.data_store),
id,
can_gc,
))
})
}
/// <https://html.spec.whatwg.org/multipage/#dom-datatransferitemlist-remove>

View file

@ -194,7 +194,7 @@ use crate::dom::wheelevent::WheelEvent as DomWheelEvent;
use crate::dom::window::Window;
use crate::dom::windowproxy::WindowProxy;
use crate::dom::xpathevaluator::XPathEvaluator;
use crate::drag_data_store::{DragDataStore, Kind, Mode, PlainString};
use crate::drag_data_store::{DragDataStore, Kind, Mode};
use crate::fetch::FetchCanceller;
use crate::iframe_collection::IFrameCollection;
use crate::messaging::{CommonScriptMsg, MainThreadScriptMsg};
@ -1688,11 +1688,9 @@ impl Document {
// Step 7.1.2.1 For each clipboard-part on the OS clipboard:
// Step 7.1.2.1.1 If clipboard-part contains plain text, then
let plain_string = PlainString::new(
DOMString::from_string(text_contents.to_string()),
DOMString::from("text/plain"),
);
let _ = drag_data_store.add(Kind::Text(plain_string));
let data = DOMString::from(text_contents.to_string());
let type_ = DOMString::from("text/plain");
let _ = drag_data_store.add(Kind::Text { data, type_ });
// Step 7.1.2.1.2 TODO If clipboard-part represents file references, then for each file reference
// Step 7.1.2.1.3 TODO If clipboard-part contains HTML- or XHTML-formatted text then
@ -1744,16 +1742,16 @@ impl Document {
// Step 1.2
for item in drag_data_store.iter_item_list() {
match item {
Kind::Text(string) => {
Kind::Text { data, .. } => {
// Step 1.2.1.1 Ensure encoding is correct per OS and locale conventions
// Step 1.2.1.2 Normalize line endings according to platform conventions
// Step 1.2.1.3
self.send_to_embedder(EmbedderMsg::SetClipboardText(
self.webview_id(),
string.data(),
data.to_string(),
));
},
Kind::File(_) => {
Kind::File { .. } => {
// Step 1.2.2 If data is of a type listed in the mandatory data types list, then
// Step 1.2.2.1 Place part on clipboard with the appropriate OS clipboard format description
// Step 1.2.3 Else this is left to the implementation