diff --git a/src/components/script/dom/bindings/codegen/Bindings.conf b/src/components/script/dom/bindings/codegen/Bindings.conf index ffa19b30198..7cff64d5ade 100644 --- a/src/components/script/dom/bindings/codegen/Bindings.conf +++ b/src/components/script/dom/bindings/codegen/Bindings.conf @@ -32,6 +32,7 @@ DOMInterfaces = { 'createElement', 'createProcessingInstruction', 'createTextNode', + 'getElementsByClassName', 'getElementsByTagName', 'getElementsByTagNameNS', 'title', @@ -45,6 +46,7 @@ DOMInterfaces = { 'attributes', 'getBoundingClientRect', 'getClientRects', + 'getElementsByClassName', 'getElementsByTagName', 'getElementsByTagNameNS', 'id', diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index fba0d4b506f..95de6ab2fac 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -225,6 +225,11 @@ impl Document { HTMLCollection::by_tag_name_ns(&self.window, &NodeCast::from(abstract_self), tag_name, namespace) } + // http://dom.spec.whatwg.org/#dom-document-getelementsbyclassname + pub fn GetElementsByClassName(&self, abstract_self: &JS, classes: DOMString) -> JS { + HTMLCollection::by_class_name(&self.window, &NodeCast::from(abstract_self), classes) + } + // http://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid pub fn GetElementById(&self, id: DOMString) -> Option> { // TODO: "in tree order, within the context object's tree" diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs index a004126cc6d..95e4e7f9af9 100644 --- a/src/components/script/dom/element.rs +++ b/src/components/script/dom/element.rs @@ -370,6 +370,13 @@ impl Element { _ => false } } + + pub fn has_class(&self, name: &str) -> bool { + // FIXME: https://github.com/mozilla/servo/issues/1840 + let class_names = self.get_string_attribute("class"); + let mut classes = class_names.split(' '); + classes.any(|class| name == class) + } } // http://www.whatwg.org/html/#reflecting-content-attributes-in-idl-attributes @@ -520,11 +527,10 @@ impl Element { HTMLCollection::by_tag_name_ns(&doc.window, &NodeCast::from(abstract_self), localname, namespace) } - // http://dom.spec.whatwg.org/#dom-element-getelementsbyclassname - pub fn GetElementsByClassName(&self, _names: DOMString) -> JS { - // FIXME: stub - https://github.com/mozilla/servo/issues/1660 + pub fn GetElementsByClassName(&self, abstract_self: &JS, classes: DOMString) -> JS { let doc = self.node.owner_doc(); - HTMLCollection::new(&doc.get().window, ~[]) + let doc = doc.get(); + HTMLCollection::by_class_name(&doc.window, &NodeCast::from(abstract_self), classes) } // http://dev.w3.org/csswg/cssom-view/#dom-element-getclientrects diff --git a/src/components/script/dom/htmlcollection.rs b/src/components/script/dom/htmlcollection.rs index 4d18e9734f4..96e04d46d3b 100644 --- a/src/components/script/dom/htmlcollection.rs +++ b/src/components/script/dom/htmlcollection.rs @@ -55,6 +55,12 @@ impl HTMLCollection { 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_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.get().has_class(*class))) + } } impl HTMLCollection { diff --git a/src/components/script/dom/webidls/Document.webidl b/src/components/script/dom/webidls/Document.webidl index 9dc92b1520f..98df0b2aef9 100644 --- a/src/components/script/dom/webidls/Document.webidl +++ b/src/components/script/dom/webidls/Document.webidl @@ -22,6 +22,7 @@ interface Document : Node { readonly attribute Element? documentElement; HTMLCollection getElementsByTagName(DOMString localName); HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName); + HTMLCollection getElementsByClassName(DOMString classNames); Element? getElementById(DOMString elementId); [Creator, Throws]