diff --git a/components/script/dom/domtokenlist.rs b/components/script/dom/domtokenlist.rs index fecacbcdb73..e3bf5157ee9 100644 --- a/components/script/dom/domtokenlist.rs +++ b/components/script/dom/domtokenlist.rs @@ -68,7 +68,7 @@ impl DOMTokenList { } } - // https://dom.spec.whatwg.org/#concept-dtl-update + /// fn perform_update_steps(&self, atoms: Vec) { // Step 1 if !self.element.has_attribute(&self.local_name) && atoms.is_empty() { @@ -79,7 +79,7 @@ impl DOMTokenList { .set_atomic_tokenlist_attribute(&self.local_name, atoms) } - // https://dom.spec.whatwg.org/#concept-domtokenlist-validation + /// fn validation_steps(&self, token: &str) -> Fallible { match &self.supported_tokens { None => Err(Error::Type( @@ -99,15 +99,15 @@ impl DOMTokenList { } } -// https://dom.spec.whatwg.org/#domtokenlist +/// impl DOMTokenListMethods for DOMTokenList { - // https://dom.spec.whatwg.org/#dom-domtokenlist-length + /// fn Length(&self) -> u32 { self.attribute() .map_or(0, |attr| attr.value().as_tokens().len()) as u32 } - // https://dom.spec.whatwg.org/#dom-domtokenlist-item + /// fn Item(&self, index: u32) -> Option { self.attribute().and_then(|attr| { // FIXME(ajeffrey): Convert directly from Atom to DOMString @@ -118,7 +118,7 @@ impl DOMTokenListMethods for DOMTokenList { }) } - // https://dom.spec.whatwg.org/#dom-domtokenlist-contains + /// fn Contains(&self, token: DOMString) -> bool { let token = Atom::from(token); self.attribute().map_or(false, |attr| { @@ -129,7 +129,7 @@ impl DOMTokenListMethods for DOMTokenList { }) } - // https://dom.spec.whatwg.org/#dom-domtokenlist-add + /// fn Add(&self, tokens: Vec) -> ErrorResult { let mut atoms = self.element.get_tokenlist_attribute(&self.local_name); for token in &tokens { @@ -142,7 +142,7 @@ impl DOMTokenListMethods for DOMTokenList { Ok(()) } - // https://dom.spec.whatwg.org/#dom-domtokenlist-remove + /// fn Remove(&self, tokens: Vec) -> ErrorResult { let mut atoms = self.element.get_tokenlist_attribute(&self.local_name); for token in &tokens { @@ -156,7 +156,7 @@ impl DOMTokenListMethods for DOMTokenList { Ok(()) } - // https://dom.spec.whatwg.org/#dom-domtokenlist-toggle + /// fn Toggle(&self, token: DOMString, force: Option) -> Fallible { let mut atoms = self.element.get_tokenlist_attribute(&self.local_name); let token = self.check_token_exceptions(&token)?; @@ -180,18 +180,18 @@ impl DOMTokenListMethods for DOMTokenList { } } - // https://dom.spec.whatwg.org/#dom-domtokenlist-value + /// fn Value(&self) -> DOMString { self.element.get_string_attribute(&self.local_name) } - // https://dom.spec.whatwg.org/#dom-domtokenlist-value + /// fn SetValue(&self, value: DOMString) { self.element .set_tokenlist_attribute(&self.local_name, value); } - // https://dom.spec.whatwg.org/#dom-domtokenlist-replace + /// fn Replace(&self, token: DOMString, new_token: DOMString) -> Fallible { if token.is_empty() || new_token.is_empty() { // Step 1. @@ -207,23 +207,27 @@ impl DOMTokenListMethods for DOMTokenList { let mut atoms = self.element.get_tokenlist_attribute(&self.local_name); let mut result = false; if let Some(pos) = atoms.iter().position(|atom| *atom == token) { - if let Some(redundant_pos) = atoms.iter().position(|atom| *atom == new_token) { - if redundant_pos > pos { + match atoms.iter().position(|atom| *atom == new_token) { + Some(redundant_pos) if redundant_pos > pos => { // The replacement is already in the list, later, // so we perform the replacement and remove the // later copy. atoms[pos] = new_token; atoms.remove(redundant_pos); - } else if redundant_pos < pos { + }, + Some(redundant_pos) if redundant_pos < pos => { // The replacement is already in the list, earlier, // so we remove the index where we'd be putting the // later copy. atoms.remove(pos); - } - // else we are replacing the token with itself, nothing to change - } else { - // The replacement is not in the list already - atoms[pos] = new_token; + }, + Some(_) => { + // Else we are replacing the token with itself, nothing to change + }, + None => { + // The replacement is not in the list already + atoms[pos] = new_token; + }, } // Step 5. @@ -233,7 +237,7 @@ impl DOMTokenListMethods for DOMTokenList { Ok(result) } - // https://dom.spec.whatwg.org/#dom-domtokenlist-supports + /// fn Supports(&self, token: DOMString) -> Fallible { self.validation_steps(&token) } diff --git a/components/script/dom/history.rs b/components/script/dom/history.rs index 465d4153c3c..15a02fa64c1 100644 --- a/components/script/dom/history.rs +++ b/components/script/dom/history.rs @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cell::Cell; +use std::cmp::Ordering; use dom_struct::dom_struct; use js::jsapi::Heap; @@ -37,7 +38,7 @@ enum PushOrReplace { Replace, } -// https://html.spec.whatwg.org/multipage/#the-history-interface +/// #[dom_struct] pub struct History { reflector_: Reflector, @@ -79,8 +80,8 @@ impl History { Ok(()) } - // https://html.spec.whatwg.org/multipage/#history-traversal - // Steps 5-16 + /// + /// Steps 5-16 #[allow(unsafe_code)] pub fn activate_state(&self, state_id: Option, url: ServoUrl) { // Steps 5 @@ -165,8 +166,8 @@ impl History { .send(CoreResourceMsg::RemoveHistoryStates(states)); } - // https://html.spec.whatwg.org/multipage/#dom-history-pushstate - // https://html.spec.whatwg.org/multipage/#dom-history-replacestate + /// + /// fn push_or_replace_state( &self, cx: JSContext, @@ -285,7 +286,7 @@ impl History { } impl HistoryMethods for History { - // https://html.spec.whatwg.org/multipage/#dom-history-state + /// fn GetState(&self, _cx: JSContext) -> Fallible { if !self.window.Document().is_fully_active() { return Err(Error::Security); @@ -293,7 +294,7 @@ impl HistoryMethods for History { Ok(self.state.get()) } - // https://html.spec.whatwg.org/multipage/#dom-history-length + /// fn GetLength(&self) -> Fallible { if !self.window.Document().is_fully_active() { return Err(Error::Security); @@ -309,30 +310,28 @@ impl HistoryMethods for History { Ok(recv.recv().unwrap()) } - // https://html.spec.whatwg.org/multipage/#dom-history-go + /// fn Go(&self, delta: i32) -> ErrorResult { - let direction = if delta > 0 { - TraversalDirection::Forward(delta as usize) - } else if delta < 0 { - TraversalDirection::Back(-delta as usize) - } else { - return self.window.Location().Reload(); + let direction = match delta.cmp(&0) { + Ordering::Greater => TraversalDirection::Forward(delta as usize), + Ordering::Less => TraversalDirection::Back(-delta as usize), + Ordering::Equal => return self.window.Location().Reload(), }; self.traverse_history(direction) } - // https://html.spec.whatwg.org/multipage/#dom-history-back + /// fn Back(&self) -> ErrorResult { self.traverse_history(TraversalDirection::Back(1)) } - // https://html.spec.whatwg.org/multipage/#dom-history-forward + /// fn Forward(&self) -> ErrorResult { self.traverse_history(TraversalDirection::Forward(1)) } - // https://html.spec.whatwg.org/multipage/#dom-history-pushstate + /// fn PushState( &self, cx: JSContext, @@ -343,7 +342,7 @@ impl HistoryMethods for History { self.push_or_replace_state(cx, data, title, url, PushOrReplace::Push) } - // https://html.spec.whatwg.org/multipage/#dom-history-replacestate + /// fn ReplaceState( &self, cx: JSContext, diff --git a/components/script/dom/htmlcollection.rs b/components/script/dom/htmlcollection.rs index cfcc8b5f2ef..c83c4125f2d 100644 --- a/components/script/dom/htmlcollection.rs +++ b/components/script/dom/htmlcollection.rs @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cell::Cell; +use std::cmp::Ordering; use dom_struct::dom_struct; use html5ever::{local_name, namespace_url, ns, LocalName, QualName}; @@ -24,9 +25,9 @@ pub trait CollectionFilter: JSTraceable { fn filter<'a>(&self, elem: &'a Element, root: &'a Node) -> bool; } -// An optional u32, using maxint to represent None. -// It would be nicer just to use Option for this, but that would produce word -// alignment issues since Option uses 33 bits. +/// An optional u32, using maxint to represent None. +/// It would be nicer just to use Option for this, but that would produce word +/// alignment issues since Option uses 33 bits. #[derive(Clone, Copy, JSTraceable, MallocSizeOf)] struct OptionU32 { bits: u32, @@ -146,7 +147,7 @@ impl HTMLCollection { } } - // https://dom.spec.whatwg.org/#concept-getelementsbytagname + /// pub fn by_qualified_name( window: &Window, root: &Node, @@ -317,7 +318,7 @@ impl HTMLCollection { } impl HTMLCollectionMethods for HTMLCollection { - // https://dom.spec.whatwg.org/#dom-htmlcollection-length + /// fn Length(&self) -> u32 { self.validate_cache(); @@ -332,30 +333,34 @@ impl HTMLCollectionMethods for HTMLCollection { } } - // https://dom.spec.whatwg.org/#dom-htmlcollection-item + /// fn Item(&self, index: u32) -> Option> { self.validate_cache(); if let Some(element) = self.cached_cursor_element.get() { // Cache hit, the cursor element is set if let Some(cached_index) = self.cached_cursor_index.get().to_option() { - if cached_index == index { - // The cursor is the element we're looking for - Some(element) - } else if cached_index < index { - // The cursor is before the element we're looking for - // Iterate forwards, starting at the cursor. - let offset = index - (cached_index + 1); - let node: DomRoot = DomRoot::upcast(element); - let mut iter = self.elements_iter_after(&node); - self.set_cached_cursor(index, iter.nth(offset as usize)) - } else { - // The cursor is after the element we're looking for - // Iterate backwards, starting at the cursor. - let offset = cached_index - (index + 1); - let node: DomRoot = DomRoot::upcast(element); - let mut iter = self.elements_iter_before(&node); - self.set_cached_cursor(index, iter.nth(offset as usize)) + match cached_index.cmp(&index) { + Ordering::Equal => { + // The cursor is the element we're looking for + Some(element) + }, + Ordering::Less => { + // The cursor is before the element we're looking for + // Iterate forwards, starting at the cursor. + let offset = index - (cached_index + 1); + let node: DomRoot = DomRoot::upcast(element); + let mut iter = self.elements_iter_after(&node); + self.set_cached_cursor(index, iter.nth(offset as usize)) + }, + Ordering::Greater => { + // The cursor is after the element we're looking for + // Iterate backwards, starting at the cursor. + let offset = cached_index - (index + 1); + let node: DomRoot = DomRoot::upcast(element); + let mut iter = self.elements_iter_before(&node); + self.set_cached_cursor(index, iter.nth(offset as usize)) + }, } } else { // Cache miss @@ -369,7 +374,7 @@ impl HTMLCollectionMethods for HTMLCollection { } } - // https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem + /// fn NamedItem(&self, key: DOMString) -> Option> { // Step 1. if key.is_empty() { diff --git a/components/script/dom/htmlmetaelement.rs b/components/script/dom/htmlmetaelement.rs index 87d6c782c93..1f1d4997b8a 100644 --- a/components/script/dom/htmlmetaelement.rs +++ b/components/script/dom/htmlmetaelement.rs @@ -85,7 +85,7 @@ impl HTMLMetaElement { if name == "referrer" { self.apply_referrer(); } - } else if &*self.HttpEquiv() != "" { + } else if !self.HttpEquiv().is_empty() { self.declarative_refresh(); } } diff --git a/components/script/dom/htmloptionscollection.rs b/components/script/dom/htmloptionscollection.rs index de5845dd30f..58155ab3183 100644 --- a/components/script/dom/htmloptionscollection.rs +++ b/components/script/dom/htmloptionscollection.rs @@ -2,6 +2,8 @@ * 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::cmp::Ordering; + use dom_struct::dom_struct; use html5ever::local_name; @@ -120,27 +122,31 @@ impl HTMLOptionsCollectionMethods for HTMLOptionsCollection { } } - // https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-length + /// fn Length(&self) -> u32 { self.upcast().Length() } - // https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-length + /// fn SetLength(&self, length: u32) { let current_length = self.upcast().Length(); let delta = length as i32 - current_length as i32; - if delta < 0 { - // new length is lower - deleting last option elements - for index in (length..current_length).rev() { - self.Remove(index as i32) - } - } else if delta > 0 { - // new length is higher - adding new option elements - self.add_new_elements(delta as u32).unwrap(); + match delta.cmp(&0) { + Ordering::Less => { + // new length is lower - deleting last option elements + for index in (length..current_length).rev() { + self.Remove(index as i32) + } + }, + Ordering::Greater => { + // new length is higher - adding new option elements + self.add_new_elements(delta as u32).unwrap(); + }, + _ => {}, } } - // https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-add + /// fn Add( &self, element: HTMLOptionElementOrHTMLOptGroupElement, @@ -195,14 +201,14 @@ impl HTMLOptionsCollectionMethods for HTMLOptionsCollection { Node::pre_insert(node, &parent, reference_node.as_deref()).map(|_| ()) } - // https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-remove + /// fn Remove(&self, index: i32) { if let Some(element) = self.upcast().IndexedGetter(index as u32) { element.Remove(); } } - // https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-selectedindex + /// fn SelectedIndex(&self) -> i32 { self.upcast() .root_node() @@ -211,7 +217,7 @@ impl HTMLOptionsCollectionMethods for HTMLOptionsCollection { .SelectedIndex() } - // https://html.spec.whatwg.org/multipage/#dom-htmloptionscollection-selectedindex + /// fn SetSelectedIndex(&self, index: i32) { self.upcast() .root_node()