mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
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:
parent
0e9bebce0f
commit
966888615f
7 changed files with 198 additions and 125 deletions
|
@ -4,6 +4,7 @@
|
|||
|
||||
use std::sync::Arc;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use pixels::Image;
|
||||
use script_traits::serializable::BlobImpl;
|
||||
|
||||
|
@ -15,53 +16,30 @@ use crate::dom::globalscope::GlobalScope;
|
|||
use crate::script_runtime::CanGc;
|
||||
|
||||
/// <https://html.spec.whatwg.org/multipage/#the-drag-data-item-kind>
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum Kind {
|
||||
Text(PlainString),
|
||||
File(Binary),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct PlainString {
|
||||
data: DOMString,
|
||||
type_: DOMString,
|
||||
}
|
||||
|
||||
impl PlainString {
|
||||
pub(crate) fn new(data: DOMString, type_: DOMString) -> Self {
|
||||
Self { data, type_ }
|
||||
}
|
||||
|
||||
pub fn data(&self) -> String {
|
||||
self.data.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Binary {
|
||||
bytes: Vec<u8>,
|
||||
name: DOMString,
|
||||
type_: String,
|
||||
}
|
||||
|
||||
impl Binary {
|
||||
pub(crate) fn new(bytes: Vec<u8>, name: DOMString, type_: String) -> Self {
|
||||
Self { bytes, name, type_ }
|
||||
}
|
||||
Text {
|
||||
data: DOMString,
|
||||
type_: DOMString,
|
||||
},
|
||||
File {
|
||||
bytes: Vec<u8>,
|
||||
name: DOMString,
|
||||
type_: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
pub(crate) fn type_(&self) -> DOMString {
|
||||
match self {
|
||||
Kind::Text(string) => string.type_.clone(),
|
||||
Kind::File(binary) => DOMString::from(binary.type_.clone()),
|
||||
Kind::Text { type_, .. } => type_.clone(),
|
||||
Kind::File { type_, .. } => DOMString::from(type_.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn as_string(&self) -> Option<DOMString> {
|
||||
pub(crate) fn as_string(&self) -> Option<String> {
|
||||
match self {
|
||||
Kind::Text(string) => Some(string.data.clone()),
|
||||
Kind::File(_) => None,
|
||||
Kind::Text { data, .. } => Some(data.to_string()),
|
||||
Kind::File { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,23 +47,23 @@ impl Kind {
|
|||
// since File constructor requires moving it.
|
||||
pub(crate) fn as_file(&self, global: &GlobalScope, can_gc: CanGc) -> Option<DomRoot<File>> {
|
||||
match self {
|
||||
Kind::Text(_) => None,
|
||||
Kind::File(binary) => Some(File::new(
|
||||
Kind::Text { .. } => None,
|
||||
Kind::File { bytes, name, type_ } => Some(File::new(
|
||||
global,
|
||||
BlobImpl::new_from_bytes(binary.bytes.clone(), binary.type_.clone()),
|
||||
binary.name.clone(),
|
||||
BlobImpl::new_from_bytes(bytes.clone(), type_.clone()),
|
||||
name.clone(),
|
||||
None,
|
||||
can_gc,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn text_type_matches(&self, type_: &str) -> bool {
|
||||
matches!(self, Kind::Text(string) if string.type_.eq(type_))
|
||||
fn text_type_matches(&self, text_type: &str) -> bool {
|
||||
matches!(self, Kind::Text { type_, .. } if type_.eq(text_type))
|
||||
}
|
||||
|
||||
fn is_file(&self) -> bool {
|
||||
matches!(self, Kind::File(_))
|
||||
matches!(self, Kind::File { .. })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -111,7 +89,8 @@ pub(crate) enum Mode {
|
|||
#[allow(dead_code)] // TODO some fields are used by DragEvent.
|
||||
pub(crate) struct DragDataStore {
|
||||
/// <https://html.spec.whatwg.org/multipage/#drag-data-store-item-list>
|
||||
item_list: Vec<Kind>,
|
||||
item_list: IndexMap<u16, Kind>,
|
||||
next_item_id: u16,
|
||||
/// <https://html.spec.whatwg.org/multipage/#drag-data-store-default-feedback>
|
||||
default_feedback: Option<String>,
|
||||
bitmap: Option<Bitmap>,
|
||||
|
@ -127,7 +106,8 @@ impl DragDataStore {
|
|||
#[allow(clippy::new_without_default)]
|
||||
pub(crate) fn new() -> DragDataStore {
|
||||
DragDataStore {
|
||||
item_list: Vec::new(),
|
||||
item_list: IndexMap::new(),
|
||||
next_item_id: 0,
|
||||
default_feedback: None,
|
||||
bitmap: None,
|
||||
mode: Mode::Protected,
|
||||
|
@ -154,12 +134,12 @@ impl DragDataStore {
|
|||
pub(crate) fn types(&self) -> Vec<DOMString> {
|
||||
let mut types = Vec::new();
|
||||
|
||||
let has_files = self.item_list.iter().fold(false, |has_files, item| {
|
||||
let has_files = self.item_list.values().fold(false, |has_files, item| {
|
||||
// Step 2.1 For each item in the item list whose kind is text,
|
||||
// add an entry to L consisting of the item's type string.
|
||||
match item {
|
||||
Kind::Text(string) => types.push(string.type_.clone()),
|
||||
Kind::File(_) => return true,
|
||||
Kind::Text { type_, .. } => types.push(type_.clone()),
|
||||
Kind::File { .. } => return true,
|
||||
}
|
||||
|
||||
has_files
|
||||
|
@ -175,28 +155,32 @@ impl DragDataStore {
|
|||
|
||||
pub(crate) fn find_matching_text(&self, type_: &str) -> Option<DOMString> {
|
||||
self.item_list
|
||||
.iter()
|
||||
.values()
|
||||
.find(|item| item.text_type_matches(type_))
|
||||
.and_then(|item| item.as_string())
|
||||
.map(DOMString::from)
|
||||
}
|
||||
|
||||
pub(crate) fn add(&mut self, kind: Kind) -> Fallible<()> {
|
||||
if let Kind::Text(ref string) = kind {
|
||||
pub(crate) fn add(&mut self, kind: Kind) -> Fallible<u16> {
|
||||
if let Kind::Text { ref type_, .. } = kind {
|
||||
// Step 2.1 If there is already an item in the item list whose kind is text
|
||||
// and whose type string is equal to the method's second argument, throw "NotSupportedError".
|
||||
if self
|
||||
.item_list
|
||||
.iter()
|
||||
.any(|item| item.text_type_matches(&string.type_))
|
||||
.values()
|
||||
.any(|item| item.text_type_matches(type_))
|
||||
{
|
||||
return Err(Error::NotSupported);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2.2
|
||||
self.item_list.push(kind);
|
||||
let item_id = self.next_item_id;
|
||||
|
||||
Ok(())
|
||||
// Step 2.2
|
||||
self.item_list.insert(item_id, kind);
|
||||
|
||||
self.next_item_id += 1;
|
||||
Ok(item_id)
|
||||
}
|
||||
|
||||
pub(crate) fn set_data(&mut self, format: DOMString, data: DOMString) {
|
||||
|
@ -206,10 +190,12 @@ impl DragDataStore {
|
|||
// Step 5 Remove the item in the drag data store item list whose kind is text
|
||||
// and whose type string is equal to format, if there is one.
|
||||
self.item_list
|
||||
.retain(|item| !item.text_type_matches(&type_));
|
||||
.retain(|_, item| !item.text_type_matches(&type_));
|
||||
|
||||
// Step 6 Add an item whose kind is text, whose type is format, and whose data is the method's second argument.
|
||||
self.item_list.push(Kind::Text(PlainString { data, type_ }));
|
||||
self.item_list
|
||||
.insert(self.next_item_id, Kind::Text { data, type_ });
|
||||
self.next_item_id += 1;
|
||||
}
|
||||
|
||||
pub(crate) fn clear_data(&mut self, format: Option<DOMString>) -> bool {
|
||||
|
@ -220,7 +206,7 @@ impl DragDataStore {
|
|||
let type_ = normalize_mime(format);
|
||||
|
||||
// Step 6 Remove the item in the item list whose kind is text and whose type is format.
|
||||
self.item_list.retain(|item| {
|
||||
self.item_list.retain(|_, item| {
|
||||
let matches = item.text_type_matches(&type_);
|
||||
|
||||
if matches {
|
||||
|
@ -230,7 +216,7 @@ impl DragDataStore {
|
|||
});
|
||||
} else {
|
||||
// Step 3 Remove each item in the item list whose kind is text.
|
||||
self.item_list.retain(|item| {
|
||||
self.item_list.retain(|_, item| {
|
||||
let matches = item.is_file();
|
||||
|
||||
if !matches {
|
||||
|
@ -256,7 +242,7 @@ impl DragDataStore {
|
|||
|
||||
// Step 4 For each item in the drag data store item list whose kind is File, add the item's data to the list L.
|
||||
self.item_list
|
||||
.iter()
|
||||
.values()
|
||||
.filter_map(|item| item.as_file(global, can_gc))
|
||||
.for_each(|file| file_list.push(file));
|
||||
}
|
||||
|
@ -265,16 +251,20 @@ impl DragDataStore {
|
|||
self.item_list.len()
|
||||
}
|
||||
|
||||
pub(crate) fn iter_item_list(&self) -> std::slice::Iter<'_, Kind> {
|
||||
self.item_list.iter()
|
||||
pub(crate) fn iter_item_list(&self) -> indexmap::map::Values<'_, u16, Kind> {
|
||||
self.item_list.values()
|
||||
}
|
||||
|
||||
pub(crate) fn get_item(&self, index: usize) -> Option<Kind> {
|
||||
self.item_list.get(index).cloned()
|
||||
pub(crate) fn get_by_index(&self, index: usize) -> Option<(&u16, &Kind)> {
|
||||
self.item_list.get_index(index)
|
||||
}
|
||||
|
||||
pub(crate) fn get_by_id(&self, id: &u16) -> Option<&Kind> {
|
||||
self.item_list.get(id)
|
||||
}
|
||||
|
||||
pub(crate) fn remove(&mut self, index: usize) {
|
||||
self.item_list.remove(index);
|
||||
self.item_list.shift_remove_index(index);
|
||||
}
|
||||
|
||||
pub(crate) fn clear_list(&mut self) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue