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

@ -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>