mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Implemented {Document,Element}.getElementsByClassName
This commit is contained in:
parent
e1499b610e
commit
d010861b75
5 changed files with 24 additions and 4 deletions
|
@ -32,6 +32,7 @@ DOMInterfaces = {
|
|||
'createElement',
|
||||
'createProcessingInstruction',
|
||||
'createTextNode',
|
||||
'getElementsByClassName',
|
||||
'getElementsByTagName',
|
||||
'getElementsByTagNameNS',
|
||||
'title',
|
||||
|
@ -45,6 +46,7 @@ DOMInterfaces = {
|
|||
'attributes',
|
||||
'getBoundingClientRect',
|
||||
'getClientRects',
|
||||
'getElementsByClassName',
|
||||
'getElementsByTagName',
|
||||
'getElementsByTagNameNS',
|
||||
'id',
|
||||
|
|
|
@ -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<Document>, classes: DOMString) -> JS<HTMLCollection> {
|
||||
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<JS<Element>> {
|
||||
// TODO: "in tree order, within the context object's tree"
|
||||
|
|
|
@ -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<HTMLCollection> {
|
||||
// FIXME: stub - https://github.com/mozilla/servo/issues/1660
|
||||
pub fn GetElementsByClassName(&self, abstract_self: &JS<Element>, classes: DOMString) -> JS<HTMLCollection> {
|
||||
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
|
||||
|
|
|
@ -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> {
|
||||
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 {
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue