diff --git a/src/components/script/dom/htmlcollection.rs b/src/components/script/dom/htmlcollection.rs index 71f4f90a568..d54ec0c0aff 100644 --- a/src/components/script/dom/htmlcollection.rs +++ b/src/components/script/dom/htmlcollection.rs @@ -51,6 +51,10 @@ impl HTMLCollection { } impl HTMLCollection { + pub fn create_live(window: &JS, root: &JS, filter: ~CollectionFilter) -> JS { + HTMLCollection::new(window, Live(root.clone(), filter)) + } + pub fn create(window: &JS, root: &JS, predicate: |elem: &JS| -> bool) -> JS { let mut elements = ~[]; for child in root.traverse_preorder() { @@ -64,18 +68,54 @@ impl HTMLCollection { HTMLCollection::new(window, Static(elements)) } - pub fn by_tag_name(window: &JS, root: &JS, tag_name: DOMString) -> JS { - HTMLCollection::create(window, root, |elem| elem.get().tag_name == tag_name) + pub fn by_tag_name(window: &JS, root: &JS, tag: DOMString) + -> JS { + struct TagNameFilter { + tag: DOMString + } + impl CollectionFilter for TagNameFilter { + fn filter(&self, elem: &JS, _root: &JS) -> bool { + elem.get().tag_name == self.tag + } + } + let filter = TagNameFilter { + tag: tag + }; + HTMLCollection::create_live(window, root, ~filter) } - pub fn by_tag_name_ns(window: &JS, root: &JS, tag_name: DOMString, namespace: Namespace) -> JS { - HTMLCollection::create(window, root, |elem| elem.get().namespace == namespace && elem.get().tag_name == tag_name) + pub fn by_tag_name_ns(window: &JS, root: &JS, tag: DOMString, + namespace: Namespace) -> JS { + struct TagNameNSFilter { + tag: DOMString, + namespace: Namespace + } + impl CollectionFilter for TagNameNSFilter { + fn filter(&self, elem: &JS, _root: &JS) -> bool { + elem.get().namespace == self.namespace && elem.get().tag_name == self.tag + } + } + let filter = TagNameNSFilter { + tag: tag, + namespace: namespace + }; + HTMLCollection::create_live(window, root, ~filter) } - pub fn by_class_name(window: &JS, root: &JS, classes: DOMString) -> JS { - // FIXME: https://github.com/mozilla/servo/issues/1840 - let classes: ~[&str] = classes.split(' ').collect(); - HTMLCollection::create(window, root, |elem| classes.iter().all(|class| elem.has_class(*class))) + pub fn by_class_name(window: &JS, root: &JS, classes: DOMString) + -> JS { + struct ClassNameFilter { + classes: ~[DOMString] + } + impl CollectionFilter for ClassNameFilter { + fn filter(&self, elem: &JS, _root: &JS) -> bool { + self.classes.iter().all(|class| elem.has_class(*class)) + } + } + let filter = ClassNameFilter { + classes: classes.split(' ').map(|class| class.into_owned()).to_owned_vec() + }; + HTMLCollection::create_live(window, root, ~filter) } }