servo: Merge #1838 - Updates for HTMLCollection (from brunoabinader:htmlcollection-live); r=jdm

Source-Repo: https://github.com/servo/servo
Source-Revision: 2238d81b530d40c164df59569fb4d11914ad5c99
This commit is contained in:
Bruno de Oliveira Abinader 2014-03-07 15:43:24 -05:00
Родитель 53c63cf25b
Коммит 6a7c48ee88
12 изменённых файлов: 277 добавлений и 64 удалений

Просмотреть файл

@ -26,12 +26,23 @@ DOMInterfaces = {
'Console': {},
'Document': {
'needsAbstract': [
'anchors',
'applets',
'body',
'createComment',
'createDocumentFragment',
'createElement',
'createProcessingInstruction',
'createTextNode',
'embeds',
'forms',
'getElementsByClassName',
'getElementsByTagName',
'getElementsByTagNameNS',
'images',
'links',
'plugins',
'scripts',
'title',
],
},
@ -43,6 +54,9 @@ DOMInterfaces = {
'attributes',
'getBoundingClientRect',
'getClientRects',
'getElementsByClassName',
'getElementsByTagName',
'getElementsByTagNameNS',
'id',
'innerHTML',
'outerHTML',

Просмотреть файл

@ -35,7 +35,7 @@ use dom::window::Window;
use html::hubbub_html_parser::build_element_from_tag;
use hubbub::hubbub::{QuirksMode, NoQuirks, LimitedQuirks, FullQuirks};
use layout_interface::{DocumentDamageLevel, ContentChangedDocumentDamage};
use servo_util::namespace::Null;
use servo_util::namespace::{Namespace, Null};
use servo_util::str::DOMString;
use extra::url::{Url, from_str};
@ -212,8 +212,22 @@ impl Document {
}
// http://dom.spec.whatwg.org/#dom-document-getelementsbytagname
pub fn GetElementsByTagName(&self, tag: DOMString) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| elem.get().tag_name == tag)
pub fn GetElementsByTagName(&self, abstract_self: &JS<Document>, tag_name: DOMString) -> JS<HTMLCollection> {
HTMLCollection::by_tag_name(&self.window, &NodeCast::from(abstract_self), tag_name)
}
// http://dom.spec.whatwg.org/#dom-document-getelementsbytagnamens
pub fn GetElementsByTagNameNS(&self, abstract_self: &JS<Document>, maybe_ns: Option<DOMString>, tag_name: DOMString) -> JS<HTMLCollection> {
let namespace = match maybe_ns {
Some(namespace) => Namespace::from_str(namespace),
None => Null
};
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
@ -417,43 +431,49 @@ impl Document {
})
}
pub fn Images(&self) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| "img" == elem.get().tag_name)
pub fn Images(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
// 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> {
self.createHTMLCollection(|elem| "embed" == elem.get().tag_name)
pub fn Embeds(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
// 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> {
self.Embeds()
pub fn Plugins(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
// FIXME: https://github.com/mozilla/servo/issues/1847
self.Embeds(abstract_self)
}
pub fn Links(&self) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| {
pub fn Links(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
// 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) &&
elem.get().get_attribute(Null, "href").is_some()
})
}
pub fn Forms(&self) -> JS<HTMLCollection> {
self.createHTMLCollection(|elem| "form" == elem.get().tag_name)
pub fn Forms(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
// 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> {
self.createHTMLCollection(|elem| "script" == elem.get().tag_name)
pub fn Scripts(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
// 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> {
self.createHTMLCollection(|elem| {
"a" == elem.get().tag_name &&
elem.get().get_attribute(Null, "name").is_some()
pub fn Anchors(&self, abstract_self: &JS<Document>) -> JS<HTMLCollection> {
// FIXME: https://github.com/mozilla/servo/issues/1847
HTMLCollection::create(&self.window, &NodeCast::from(abstract_self), |elem| {
"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.
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>] {
@ -473,21 +493,6 @@ impl Document {
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> {
NodeList::new_simple_list(&self.window, self.create_collection(|node| {
if !callback(node) {

Просмотреть файл

@ -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
@ -503,25 +510,27 @@ impl Element {
self.GetAttributeNS(namespace, local_name).is_some()
}
// http://dom.spec.whatwg.org/#dom-element-getelementsbytagname
pub fn GetElementsByTagName(&self, _localname: DOMString) -> JS<HTMLCollection> {
// FIXME: stub - https://github.com/mozilla/servo/issues/1660
pub fn GetElementsByTagName(&self, abstract_self: &JS<Element>, localname: DOMString) -> JS<HTMLCollection> {
let doc = self.node.owner_doc();
HTMLCollection::new(&doc.get().window, ~[])
let doc = doc.get();
HTMLCollection::by_tag_name(&doc.window, &NodeCast::from(abstract_self), localname)
}
// http://dom.spec.whatwg.org/#dom-element-getelementsbytagnamens
pub fn GetElementsByTagNameNS(&self, _namespace: Option<DOMString>, _localname: DOMString) -> Fallible<JS<HTMLCollection>> {
// FIXME: stub - https://github.com/mozilla/servo/issues/1660
pub fn GetElementsByTagNameNS(&self, abstract_self: &JS<Element>, maybe_ns: Option<DOMString>,
localname: DOMString) -> JS<HTMLCollection> {
let doc = self.node.owner_doc();
Ok(HTMLCollection::new(&doc.get().window, ~[]))
let doc = doc.get();
let namespace = match maybe_ns {
Some(namespace) => Namespace::from_str(namespace),
None => Null
};
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

Просмотреть файл

@ -2,18 +2,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::InheritTypes::{ElementCast};
use dom::bindings::codegen::HTMLCollectionBinding;
use dom::bindings::js::JS;
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::error::Fallible;
use dom::element::Element;
use dom::node::{Node, NodeHelpers};
use dom::window::Window;
use servo_util::namespace::Namespace;
use servo_util::str::DOMString;
use js::jsapi::{JSObject, JSContext};
use std::ptr;
#[deriving(Encodable)]
pub struct HTMLCollection {
elements: ~[JS<Element>],
@ -34,11 +32,44 @@ impl HTMLCollection {
reflect_dom_object(~HTMLCollection::new_inherited(window.clone(), elements),
window, HTMLCollectionBinding::Wrap)
}
}
impl HTMLCollection {
pub fn create(window: &JS<Window>, root: &JS<Node>, predicate: |elem: &JS<Element>| -> bool) -> JS<HTMLCollection> {
let mut elements = ~[];
for child in root.traverse_preorder() {
if child.is_element() {
let elem: JS<Element> = ElementCast::to(&child);
if predicate(&elem) {
elements.push(elem);
}
}
}
HTMLCollection::new(window, elements)
}
pub fn by_tag_name(window: &JS<Window>, root: &JS<Node>, tag_name: DOMString) -> JS<HTMLCollection> {
HTMLCollection::create(window, root, |elem| elem.get().tag_name == tag_name)
}
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 {
// http://dom.spec.whatwg.org/#dom-htmlcollection-length
pub fn Length(&self) -> u32 {
self.elements.len() as u32
}
// http://dom.spec.whatwg.org/#dom-htmlcollection-item
pub fn Item(&self, index: u32) -> Option<JS<Element>> {
if index < self.Length() {
Some(self.elements[index].clone())
@ -47,17 +78,40 @@ impl HTMLCollection {
}
}
pub fn NamedItem(&self, _cx: *JSContext, _name: DOMString) -> Fallible<*JSObject> {
Ok(ptr::null())
}
// http://dom.spec.whatwg.org/#dom-htmlcollection-nameditem
pub fn NamedItem(&self, key: DOMString) -> Option<JS<Element>> {
// Step 1.
if key.is_empty() {
return None;
}
// Step 2.
self.elements.iter().find(|elem| {
let elem = elem.get();
elem.get_string_attribute("name") == key || elem.get_string_attribute("id") == key
}).map(|maybe_elem| maybe_elem.clone())
}
}
impl HTMLCollection {
pub fn IndexedGetter(&self, index: u32, found: &mut bool) -> Option<JS<Element>> {
*found = true;
self.Item(index)
let maybe_elem = self.Item(index);
*found = maybe_elem.is_some();
maybe_elem
}
pub fn NamedGetter(&self, _cx: *JSContext, _name: Option<DOMString>, _found: &mut bool) -> Fallible<*JSObject> {
Ok(ptr::null())
pub fn NamedGetter(&self, maybe_name: Option<DOMString>, found: &mut bool) -> Option<JS<Element>> {
match maybe_name {
Some(name) => {
let maybe_elem = self.NamedItem(name);
*found = maybe_elem.is_some();
maybe_elem
},
None => {
*found = false;
None
}
}
}
}

Просмотреть файл

@ -42,6 +42,7 @@ impl HTMLDataListElement {
impl HTMLDataListElement {
pub fn Options(&self) -> JS<HTMLCollection> {
// FIXME: https://github.com/mozilla/servo/issues/1842
let doc = self.htmlelement.element.node.owner_doc();
let doc = doc.get();
HTMLCollection::new(&doc.window, ~[])

Просмотреть файл

@ -69,6 +69,7 @@ impl HTMLFieldSetElement {
}
pub fn Elements(&self) -> JS<HTMLCollection> {
// FIXME: https://github.com/mozilla/servo/issues/1843
let doc = self.htmlelement.element.node.owner_doc();
let doc = doc.get();
HTMLCollection::new(&doc.window, ~[])

Просмотреть файл

@ -115,6 +115,7 @@ impl HTMLFormElement {
}
pub fn Elements(&self) -> JS<HTMLCollection> {
// FIXME: https://github.com/mozilla/servo/issues/1844
let doc = self.htmlelement.element.node.owner_doc();
let doc = doc.get();
HTMLCollection::new(&doc.window, ~[])

Просмотреть файл

@ -51,6 +51,7 @@ impl HTMLMapElement {
}
pub fn Areas(&self) -> JS<HTMLCollection> {
// FIXME: https://github.com/mozilla/servo/issues/1845
let doc = self.htmlelement.element.node.owner_doc();
let doc = doc.get();
HTMLCollection::new(&doc.window, ~[])

Просмотреть файл

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

Просмотреть файл

@ -51,7 +51,6 @@ interface Element : Node {
boolean hasAttributeNS(DOMString? namespace, DOMString localName);
HTMLCollection getElementsByTagName(DOMString localName);
[Throws]
HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
HTMLCollection getElementsByClassName(DOMString classNames);
};

Просмотреть файл

@ -10,6 +10,5 @@
interface HTMLCollection {
readonly attribute unsigned long length;
getter Element? item(unsigned long index);
[Throws]
getter object? namedItem(DOMString name); // only returns Element
getter Element? namedItem(DOMString name);
};

Просмотреть файл

@ -0,0 +1,127 @@
<!DOCTYPE html>
<html>
<head>
<script src="harness.js"></script>
<script>
let foo1 = document.getElementById("foo-1");
let foo2 = document.getElementById("foo-2");
let bar = document.getElementById("bar");
let live = document.getElementById("live");
let child = document.createElement("p");
let p1 = document.getElementById("p1");
let p2 = document.getElementById("p2");
let p3 = document.getElementById("p3");
let htmlcollection = null;
// test1: HTMLCollection interface
{
htmlcollection = document.getElementsByClassName("foo");
is(htmlcollection.length, 2);
is(htmlcollection.item(0), foo1);
is(htmlcollection.item(1), foo2);
is(htmlcollection.item(2), null);
is(htmlcollection.namedItem("foo-1"), foo1);
is(htmlcollection.namedItem("bar"), null);
htmlcollection = document.getElementsByClassName("FOO");
is(htmlcollection.length, 0);
htmlcollection = document.getElementsByClassName("bar");
is(htmlcollection.length, 1);
is(htmlcollection.item(0), bar);
is(htmlcollection.item(1), null);
is(htmlcollection.namedItem("bar"), bar);
}
// test2: live HTMLCollection
{
htmlcollection = document.getElementsByClassName("live");
is(htmlcollection.length, 1);
is(htmlcollection.item(0), live);
is(live.childNodes.length, 0)
is(htmlcollection.item(0).childNodes.length, 0);
is(document.getElementById("live").childNodes.length, 0);
live.appendChild(child);
is(live.childNodes.length, 1);
is(htmlcollection.item(0).childNodes.length, 1);
is(document.getElementById("live").childNodes.length, 1);
}
// test3: getElementsByTagName
{
htmlcollection = document.getElementsByTagName("div");
is(htmlcollection.length, 5);
let from_element = document.documentElement.getElementsByTagName("div");
is(htmlcollection.length, from_element.length);
htmlcollection = document.getElementsByTagName("DIV");
is(htmlcollection.length, 0);
htmlcollection = document.getElementsByTagName("p");
is(htmlcollection.length, 4);
from_element = document.getElementById("class-example").getElementsByTagName("p");
is(from_element.length, 3);
}
// test4: getElementsByTagNameNS
{
htmlcollection = document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "div");
is(htmlcollection.length, 5);
let from_element = document.documentElement.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "div");
is(htmlcollection.length, from_element.length);
htmlcollection = document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "DIV");
is(htmlcollection.length, 0);
htmlcollection = document.getElementsByTagNameNS("", "div");
is(htmlcollection.length, 0);
htmlcollection = document.getElementsByTagNameNS("invalid", "div");
is(htmlcollection.length, 0);
from_element = document.getElementById("class-example").getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "p");
is(from_element.length, 3);
}
// test5: document.getElementsByClassName
{
htmlcollection = document.getElementsByClassName("aaa");
is(htmlcollection.length, 2);
is(htmlcollection.item(0), p1);
is(htmlcollection.item(1), p2);
htmlcollection = document.getElementsByClassName("ccc bbb");
is(htmlcollection.length, 1);
is(htmlcollection.item(0), p3);
htmlcollection = document.getElementsByClassName("aaa,bbb");
is(htmlcollection.length, 0);
let from_element = document.getElementById("class-example").getElementsByClassName("bbb");
is(from_element.length, 2);
is(from_element.item(0), p1);
is(from_element.item(1), p3);
}
finish();
</script>
</head>
<body>
<div id="foo-1" class="foo"></div>
<div id="foo-2" class="foo"></div>
<div id="bar" class="bar"></div>
<div id="live" class="live"></div>
<div id="class-example">
<p id="p1" class="aaa bbb"/>
<p id="p2" class="aaa ccc"/>
<p id="p3" class="bbb ccc"/>
</div>
</body>
</html>