Skip to content

Commit

Permalink
cleanup some code
Browse files Browse the repository at this point in the history
Signed-off-by: Gae24 <[email protected]>
  • Loading branch information
Gae24 committed Sep 13, 2024
1 parent e52728d commit 49d4647
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 49 deletions.
2 changes: 1 addition & 1 deletion components/script/dom/datatransfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl DataTransfer {
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<DataTransfer> {
let item_list = DataTransferItemList::new(window, proto);
let item_list = DataTransferItemList::new(window, proto, can_gc);
let data_transfer = reflect_dom_object_with_proto(
Box::new(DataTransfer::new_inherited(&item_list)),
window,
Expand Down
12 changes: 5 additions & 7 deletions components/script/dom/datatransferitem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DataTransferItem {
)
}

pub fn type_already_present(&self, type_: &DOMString) -> bool {
pub fn type_matches(&self, type_: &DOMString) -> bool {
matches!(self.item, Kind::Text(_) if self.type_.borrow().eq(type_))
}

Expand All @@ -67,12 +67,10 @@ impl DataTransferItem {
}

pub fn get_string(&self, type_: &DOMString) -> Option<DOMString> {
if let Kind::Text(data) = &self.item {
if self.type_.borrow().eq(type_) {
return Some(data.clone());
}
};
None
match self.item {
Kind::Text(data) if self.type_.borrow().eq(type_) => Some(data.clone()),
_ => None,
}
}

pub fn set_data_transfer(&self, data_transfer: Option<&DataTransfer>) {
Expand Down
73 changes: 32 additions & 41 deletions components/script/dom/datatransferitemlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ impl DataTransferItemList {
}
}

pub fn new(window: &Window, proto: Option<HandleObject>) -> DomRoot<DataTransferItemList> {
pub fn new(
window: &Window,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<DataTransferItemList> {
reflect_dom_object_with_proto(
Box::new(DataTransferItemList::new_inherited()),
window,
proto,
CanGc::note(),
can_gc,
)
}

Expand All @@ -50,13 +54,19 @@ impl DataTransferItemList {
}
}

fn has_write_permission(&self) -> bool {
self.data_transfer
.root()
.is_some_and(|data_transfer| data_transfer.can_write())
}

pub fn get_data(&self, mut format: DOMString) -> DOMString {
if self
.data_transfer
.root()
.is_some_and(|data_transfer| data_transfer.can_read())
{
let mut convert_to_URL = false;
let mut convert_to_url = false;
format.make_ascii_lowercase();
let type_ = match format.as_ref() {
"text" => DOMString::from("text/plain"),
Expand All @@ -69,7 +79,7 @@ impl DataTransferItemList {

for item in self.items.borrow().iter() {
if let Some(mut result) = item.get_string(&type_) {
if convert_to_URL {
if convert_to_url {
//TODO parse uri-list as [RFC2483]
}
return result;
Expand All @@ -80,11 +90,7 @@ impl DataTransferItemList {
}

pub fn set_data(&self, mut format: DOMString, data: DOMString) {
if self
.data_transfer
.root()
.is_some_and(|data_transfer| data_transfer.can_write())
{
if self.has_write_permission() {
format.make_ascii_lowercase();
let type_ = match format.as_ref() {
"text" => DOMString::from("text/plain"),
Expand All @@ -94,7 +100,7 @@ impl DataTransferItemList {

self.items
.borrow_mut()
.retain(|item| !item.type_already_present(&type_));
.retain(|item| !item.type_matches(&type_));
let item = DataTransferItem::new(
&self.global(),
type_,
Expand All @@ -106,21 +112,18 @@ impl DataTransferItemList {
}

pub fn clear_data(&self, format: Option<DOMString>) {
if self
.data_transfer
.root()
.is_some_and(|data_transfer| data_transfer.can_write())
{
if self.has_write_permission() {
if let Some(mut format) = format {
format.make_ascii_lowercase();
let type_ = match format.as_ref() {
"text" => DOMString::from("text/plain"),
"url" => DOMString::from("text/uri-list"),
_ => return,
};

self.items
.borrow_mut()
.retain(|item| !item.type_already_present(&type_));
.retain(|item| !item.type_matches(&type_));
} else {
self.items.borrow_mut().retain(|item| item.is_file_kind());
}
Expand All @@ -142,14 +145,10 @@ impl DataTransferItemListMethods for DataTransferItemList {
data: DOMString,
mut type_: DOMString,
) -> Fallible<Option<DomRoot<DataTransferItem>>> {
if self
.data_transfer
.root()
.is_some_and(|data_transfer| data_transfer.can_write())
{
if self.has_write_permission() {
type_.make_ascii_lowercase();
for item in self.items.borrow().iter() {
if item.type_already_present(&type_) {
if item.type_matches(&type_) {
return Err(Error::NotSupported);
}
}
Expand All @@ -170,47 +169,39 @@ impl DataTransferItemListMethods for DataTransferItemList {

// https://html.spec.whatwg.org/multipage/#dom-datatransferitemlist-add
fn Add_(&self, data: &File) -> Fallible<Option<DomRoot<DataTransferItem>>> {
if self
.data_transfer
.root()
.is_some_and(|data_transfer| data_transfer.can_write())
{
if self.has_write_permission() {
let mut type_ = data.file_type();
type_.make_ascii_lowercase();

let item = DataTransferItem::new(
&self.global(),
DOMString::from(type_),
Kind::File(DomRoot::from_ref(data)),
self.data_transfer.root().as_deref(),
);
self.items.borrow_mut().push(item.clone());
return Ok(Some(item));

Ok(Some(item))
} else {
Ok(None)
}
Ok(None)
}

// https://html.spec.whatwg.org/multipage/#dom-datatransferitemlist-remove
fn Remove(&self, index: u32) -> Fallible<()> {
if self
.data_transfer
.root()
.is_some_and(|data_transfer| data_transfer.can_write())
{
if self.has_write_permission() {
if (index as usize) < self.items.borrow().len() {
self.items.borrow_mut().remove(index as usize);
}
return Ok(());
Ok(())
} else {
Err(Error::InvalidState)
}
Err(Error::InvalidState)
}

// https://html.spec.whatwg.org/multipage/#dom-datatransferitemlist-clear
fn Clear(&self) {
if self
.data_transfer
.root()
.is_some_and(|data_transfer| data_transfer.can_write())
{
if self.has_write_permission() {
self.items.borrow_mut().clear();
}
}
Expand Down

0 comments on commit 49d4647

Please sign in to comment.