Replaced Document::createHTMLDocument in favor of HTMLCollection helpers

This commit is contained in:
Bruno de Oliveira Abinader 2014-02-27 13:43:55 -04:00
parent d010861b75
commit 38ba71ceb1
2 changed files with 32 additions and 33 deletions

View file

@ -26,15 +26,23 @@ DOMInterfaces = {
'Console': {}, 'Console': {},
'Document': { 'Document': {
'needsAbstract': [ 'needsAbstract': [
'anchors',
'applets',
'body', 'body',
'createComment', 'createComment',
'createDocumentFragment', 'createDocumentFragment',
'createElement', 'createElement',
'createProcessingInstruction', 'createProcessingInstruction',
'createTextNode', 'createTextNode',
'embeds',
'forms',
'getElementsByClassName', 'getElementsByClassName',
'getElementsByTagName', 'getElementsByTagName',
'getElementsByTagNameNS', 'getElementsByTagNameNS',
'images',
'links',
'plugins',
'scripts',
'title', 'title',
], ],
}, },

View file

@ -431,43 +431,49 @@ impl Document {
}) })
} }
pub fn Images(&self) -> JS<HTMLCollection> { pub fn Images(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| "img" == elem.get().tag_name) // FIXME: https://github.com/mozilla/servo/issues/1847
HTMLCollection::by_tag_name(&self.window, &NodeCast::from(abstract_self), ~"img")
} }
pub fn Embeds(&self) -> JS<HTMLCollection> { pub fn Embeds(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| "embed" == elem.get().tag_name) // FIXME: https://github.com/mozilla/servo/issues/1847
HTMLCollection::by_tag_name(&self.window, &NodeCast::from(abstract_self), ~"embed")
} }
pub fn Plugins(&self) -> JS<HTMLCollection> { pub fn Plugins(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
self.Embeds() // FIXME: https://github.com/mozilla/servo/issues/1847
self.Embeds(abstract_self)
} }
pub fn Links(&self) -> JS<HTMLCollection> { pub fn Links(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| { // FIXME: https://github.com/mozilla/servo/issues/1847
HTMLCollection::create(&self.window, &NodeCast::from(abstract_self), |elem| {
("a" == elem.get().tag_name || "area" == elem.get().tag_name) && ("a" == elem.get().tag_name || "area" == elem.get().tag_name) &&
elem.get().get_attribute(Null, "href").is_some() elem.get().get_attribute(Null, "href").is_some()
}) })
} }
pub fn Forms(&self) -> JS<HTMLCollection> { pub fn Forms(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| "form" == elem.get().tag_name) // FIXME: https://github.com/mozilla/servo/issues/1847
HTMLCollection::by_tag_name(&self.window, &NodeCast::from(abstract_self), ~"form")
} }
pub fn Scripts(&self) -> JS<HTMLCollection> { pub fn Scripts(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| "script" == elem.get().tag_name) // FIXME: https://github.com/mozilla/servo/issues/1847
HTMLCollection::by_tag_name(&self.window, &NodeCast::from(abstract_self), ~"script")
} }
pub fn Anchors(&self) -> JS<HTMLCollection> { pub fn Anchors(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| { // FIXME: https://github.com/mozilla/servo/issues/1847
"a" == elem.get().tag_name && HTMLCollection::create(&self.window, &NodeCast::from(abstract_self), |elem| {
elem.get().get_attribute(Null, "name").is_some() "a" == elem.get().tag_name && elem.get().get_attribute(Null, "name").is_some()
}) })
} }
pub fn Applets(&self) -> JS<HTMLCollection> { pub fn Applets(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
// FIXME: This should be return OBJECT elements containing applets. // FIXME: This should be return OBJECT elements containing applets.
self.createHTMLCollection(|elem| "applet" == elem.get().tag_name) HTMLCollection::by_tag_name(&self.window, &NodeCast::from(abstract_self), ~"applet")
} }
pub fn create_collection<T>(&self, callback: |elem: &JS<Node>| -> Option<JS<T>>) -> ~[JS<T>] { pub fn create_collection<T>(&self, callback: |elem: &JS<Node>| -> Option<JS<T>>) -> ~[JS<T>] {
@ -487,21 +493,6 @@ impl Document {
nodes nodes
} }
pub fn createHTMLCollection(&self, callback: |elem: &JS<Element>| -> bool) -> JS<HTMLCollection> {
HTMLCollection::new(&self.window, self.create_collection(|node| {
if !node.is_element() {
return None;
}
let element: JS<Element> = ElementCast::to(node);
if !callback(&element) {
return None;
}
Some(element)
}))
}
pub fn createNodeList(&self, callback: |node: &JS<Node>| -> bool) -> JS<NodeList> { pub fn createNodeList(&self, callback: |node: &JS<Node>| -> bool) -> JS<NodeList> {
NodeList::new_simple_list(&self.window, self.create_collection(|node| { NodeList::new_simple_list(&self.window, self.create_collection(|node| {
if !callback(node) { if !callback(node) {