From 49d46472e38e44509eae607a2ab53201414af6c5 Mon Sep 17 00:00:00 2001 From: Gae24 <96017547+Gae24@users.noreply.github.com> Date: Fri, 13 Sep 2024 20:04:29 +0200 Subject: [PATCH] cleanup some code Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com> --- components/script/dom/datatransfer.rs | 2 +- components/script/dom/datatransferitem.rs | 12 ++- components/script/dom/datatransferitemlist.rs | 73 ++++++++----------- 3 files changed, 38 insertions(+), 49 deletions(-) diff --git a/components/script/dom/datatransfer.rs b/components/script/dom/datatransfer.rs index c508b4450ebf3..d012aa630e16b 100644 --- a/components/script/dom/datatransfer.rs +++ b/components/script/dom/datatransfer.rs @@ -49,7 +49,7 @@ impl DataTransfer { proto: Option, can_gc: CanGc, ) -> DomRoot { - 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, diff --git a/components/script/dom/datatransferitem.rs b/components/script/dom/datatransferitem.rs index f65e3dd79e1ed..6ca0871a09521 100644 --- a/components/script/dom/datatransferitem.rs +++ b/components/script/dom/datatransferitem.rs @@ -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_)) } @@ -67,12 +67,10 @@ impl DataTransferItem { } pub fn get_string(&self, type_: &DOMString) -> Option { - 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>) { diff --git a/components/script/dom/datatransferitemlist.rs b/components/script/dom/datatransferitemlist.rs index 6b5cf1efcaffe..bc4683e3455c1 100644 --- a/components/script/dom/datatransferitemlist.rs +++ b/components/script/dom/datatransferitemlist.rs @@ -34,12 +34,16 @@ impl DataTransferItemList { } } - pub fn new(window: &Window, proto: Option) -> DomRoot { + pub fn new( + window: &Window, + proto: Option, + can_gc: CanGc, + ) -> DomRoot { reflect_dom_object_with_proto( Box::new(DataTransferItemList::new_inherited()), window, proto, - CanGc::note(), + can_gc, ) } @@ -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"), @@ -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; @@ -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"), @@ -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_, @@ -106,11 +112,7 @@ impl DataTransferItemList { } pub fn clear_data(&self, format: Option) { - 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() { @@ -118,9 +120,10 @@ impl DataTransferItemList { "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()); } @@ -142,14 +145,10 @@ impl DataTransferItemListMethods for DataTransferItemList { data: DOMString, mut type_: DOMString, ) -> Fallible>> { - 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); } } @@ -170,13 +169,10 @@ impl DataTransferItemListMethods for DataTransferItemList { // https://html.spec.whatwg.org/multipage/#dom-datatransferitemlist-add fn Add_(&self, data: &File) -> Fallible>> { - 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_), @@ -184,33 +180,28 @@ impl DataTransferItemListMethods for DataTransferItemList { 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(); } }