Skip to content

Commit

Permalink
Inline functions HTMLCollection::get_length and get_item
Browse files Browse the repository at this point in the history
  • Loading branch information
saurvs committed Feb 24, 2016
1 parent 2fcf1a2 commit 5205974
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions components/script/dom/htmlcollection.rs
Expand Up @@ -103,19 +103,6 @@ impl HTMLCollection {
}
}

fn get_length(&self) -> u32 {
// Call validate_cache before calling this method!
if let Some(cached_length) = self.cached_length.get().to_option() {
// Cache hit
cached_length
} else {
// Cache miss, calculate the length
let length = self.elements_iter().count() as u32;
self.cached_length.set(OptionU32::some(length));
length
}
}

fn set_cached_cursor(&self, index: u32, element: Option<Root<Element>>) -> Option<Root<Element>> {
if let Some(element) = element {
self.cached_cursor_index.set(OptionU32::some(index));
Expand All @@ -126,39 +113,6 @@ impl HTMLCollection {
}
}

fn get_item(&self, index: u32) -> Option<Root<Element>> {
// Call validate_cache before calling this method!
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: Root<Node> = Root::upcast(element);
self.set_cached_cursor(index, self.elements_iter_after(node.r()).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: Root<Node> = Root::upcast(element);
self.set_cached_cursor(index, self.elements_iter_before(node.r()).nth(offset as usize))
}
} else {
// Cache miss
// Iterate forwards through all the nodes
self.set_cached_cursor(index, self.elements_iter().nth(index as usize))
}
} else {
// Cache miss
// Iterate forwards through all the nodes
self.set_cached_cursor(index, self.elements_iter().nth(index as usize))
}
}

pub fn by_tag_name(window: &Window, root: &Node, mut tag: DOMString)
-> Root<HTMLCollection> {
let tag_atom = Atom::from(&*tag); // FIXME(ajeffrey): Convert directly from DOMString to Atom
Expand Down Expand Up @@ -319,13 +273,51 @@ impl HTMLCollectionMethods for HTMLCollection {
// https://dom.spec.whatwg.org/#dom-htmlcollection-length
fn Length(&self) -> u32 {
self.validate_cache();
self.get_length()

if let Some(cached_length) = self.cached_length.get().to_option() {
// Cache hit
cached_length
} else {
// Cache miss, calculate the length
let length = self.elements_iter().count() as u32;
self.cached_length.set(OptionU32::some(length));
length
}
}

// https://dom.spec.whatwg.org/#dom-htmlcollection-item
fn Item(&self, index: u32) -> Option<Root<Element>> {
self.validate_cache();
self.get_item(index)

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: Root<Node> = Root::upcast(element);
self.set_cached_cursor(index, self.elements_iter_after(node.r()).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: Root<Node> = Root::upcast(element);
self.set_cached_cursor(index, self.elements_iter_before(node.r()).nth(offset as usize))
}
} else {
// Cache miss
// Iterate forwards through all the nodes
self.set_cached_cursor(index, self.elements_iter().nth(index as usize))
}
} else {
// Cache miss
// Iterate forwards through all the nodes
self.set_cached_cursor(index, self.elements_iter().nth(index as usize))
}
}

// https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem
Expand Down

0 comments on commit 5205974

Please sign in to comment.