Implemented {Document,Element}.getElementsByClassName

This commit is contained in:
Bruno de Oliveira Abinader 2014-02-27 13:18:29 -04:00
parent e1499b610e
commit d010861b75
5 changed files with 24 additions and 4 deletions

View file

@ -32,6 +32,7 @@ DOMInterfaces = {
'createElement', 'createElement',
'createProcessingInstruction', 'createProcessingInstruction',
'createTextNode', 'createTextNode',
'getElementsByClassName',
'getElementsByTagName', 'getElementsByTagName',
'getElementsByTagNameNS', 'getElementsByTagNameNS',
'title', 'title',
@ -45,6 +46,7 @@ DOMInterfaces = {
'attributes', 'attributes',
'getBoundingClientRect', 'getBoundingClientRect',
'getClientRects', 'getClientRects',
'getElementsByClassName',
'getElementsByTagName', 'getElementsByTagName',
'getElementsByTagNameNS', 'getElementsByTagNameNS',
'id', 'id',

View file

@ -225,6 +225,11 @@ impl Document {
HTMLCollection::by_tag_name_ns(&self.window, &NodeCast::from(abstract_self), tag_name, namespace) 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<Document>, classes: DOMString) -> JS<HTMLCollection> {
HTMLCollection::by_class_name(&self.window, &NodeCast::from(abstract_self), classes)
}
// http://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid // http://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid
pub fn GetElementById(&self, id: DOMString) -> Option<JS<Element>> { pub fn GetElementById(&self, id: DOMString) -> Option<JS<Element>> {
// TODO: "in tree order, within the context object's tree" // TODO: "in tree order, within the context object's tree"

View file

@ -370,6 +370,13 @@ impl Element {
_ => false _ => 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 // 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) 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, abstract_self: &JS<Element>, classes: DOMString) -> JS<HTMLCollection> {
pub fn GetElementsByClassName(&self, _names: DOMString) -> JS<HTMLCollection> {
// FIXME: stub - https://github.com/mozilla/servo/issues/1660
let doc = self.node.owner_doc(); 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 // http://dev.w3.org/csswg/cssom-view/#dom-element-getclientrects

View file

@ -55,6 +55,12 @@ impl HTMLCollection {
pub fn by_tag_name_ns(window: &JS<Window>, root: &JS<Node>, tag_name: DOMString, namespace: Namespace) -> JS<HTMLCollection> { pub fn by_tag_name_ns(window: &JS<Window>, root: &JS<Node>, tag_name: DOMString, namespace: Namespace) -> JS<HTMLCollection> {
HTMLCollection::create(window, root, |elem| elem.get().namespace == namespace && elem.get().tag_name == tag_name) HTMLCollection::create(window, root, |elem| elem.get().namespace == namespace && elem.get().tag_name == tag_name)
} }
pub fn by_class_name(window: &JS<Window>, root: &JS<Node>, classes: DOMString) -> JS<HTMLCollection> {
// 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 { impl HTMLCollection {

View file

@ -22,6 +22,7 @@ interface Document : Node {
readonly attribute Element? documentElement; readonly attribute Element? documentElement;
HTMLCollection getElementsByTagName(DOMString localName); HTMLCollection getElementsByTagName(DOMString localName);
HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName); HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
HTMLCollection getElementsByClassName(DOMString classNames);
Element? getElementById(DOMString elementId); Element? getElementById(DOMString elementId);
[Creator, Throws] [Creator, Throws]