Files
tubestation/servo/components/script/dom/htmlcollection.rs
Michael Wu ae3ff6be00 servo: Merge #6150 - Upgrade to Spidermonkey 39 (from servo:smupgrade3); r=mbrubeck
> Here it is.
>
> ~~There's two major things that are unfinished here:~~
> - ~~Dealing with the unroot_must_root lint. I'm not sure about the value of this lint with the new rooting API.~~ Done.
> - ~~Updating the Cargo.locks to point to the new SM and SM binding.~~ Done.
>
> I also included my fixes for the rust update, but these will disappear in a rebase. A rust update is necessary to support calling `Drop` on `Heap<T>` correctly when `Heap<T>` is inside a `Rc<T>`. Otherwise `&self` points to the wrong location.
>
> Incremental GC is disabled here. I'm not sure how to deal with the incremental barriers so that's left for later.
>
> Generational GC works. SM doesn't work without it.
>
> The biggest change here is to the rooting API. `Root` was made movable, and `Temporary` and `JSRef` was removed. Movable `Root`s means there's no need for `Temporary`, and `JSRef`s aren't needed generally since it can be assumed that being able to obtain a reference to a dom object means it's already rooted. References have their lifetime bound to the Roots that provided them. DOM objects that haven't passed through `reflect_dom_object` don't need to be rooted, and DOM objects that have passed through `reflect_dom_object` can't be obtained without being rooted through `native_from_reflector_jsmanaged` or `JS::<T>::root()`.
>
> Support for `Heap<T>` ended up messier than I expected. It's split into two commits, but only because it's a bit difficult to fold them together. Supporting `Heap<T>` properly requires that that `Heap::<T>::set()` be called on something that won't move. I removed the Copy and Clone trait from `Heap<T>` so `Cell` can't hold `Heap<T>` - only `UnsafeCell` can hold it.
>
> `CallbackObject` is a bit tricky - I moved all callbacks into `Rc<T>` in order to make sure that the pointer inside to a `*mut JSObject` doesn't move. This is necessary for supporting `Heap<T>`.
>
> `RootedCollectionSet` is very general purpose now. Anything with `JSTraceable` can be rooted by `RootedCollectionSet`/`RootedTraceable`. Right now, `RootedTraceable` is only used to hold down dom objects before they're fully attached to their reflector. I had to make a custom mechanism to dispatch the trace call - couldn't figure out how to get trait objects working for this case.
>
> This has been tested with the following zeal settings:
>
> GC after every allocation
> JS_GC_ZEAL=2,1
>
> GC after every 100 allocations (important for catching use-after-free bugs)
> JS_GC_ZEAL=2,100
>
> Verify pre barriers
> JS_GC_ZEAL=4,1
>
> Verify post barriers
> JS_GC_ZEAL=11,1

Source-Repo: https://github.com/servo/servo
Source-Revision: e7808c526c348fea5e3b48af70b7f1a066652097
2015-06-19 16:46:55 -06:00

246 lines
8.6 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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::Bindings::HTMLCollectionBinding;
use dom::bindings::codegen::Bindings::HTMLCollectionBinding::HTMLCollectionMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, NodeCast};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root};
use dom::bindings::trace::JSTraceable;
use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::element::{Element, AttributeHandlers, ElementHelpers};
use dom::node::{Node, NodeHelpers, TreeIterator};
use dom::window::Window;
use util::namespace;
use util::str::{DOMString, split_html_space_chars};
use std::ascii::AsciiExt;
use std::iter::{FilterMap, Skip};
use string_cache::{Atom, Namespace};
pub trait CollectionFilter : JSTraceable {
fn filter<'a>(&self, elem: &'a Element, root: &'a Node) -> bool;
}
#[jstraceable]
#[must_root]
pub enum CollectionTypeId {
Static(Vec<JS<Element>>),
Live(JS<Node>, Box<CollectionFilter+'static>)
}
#[dom_struct]
pub struct HTMLCollection {
reflector_: Reflector,
collection: CollectionTypeId,
}
impl HTMLCollection {
fn new_inherited(collection: CollectionTypeId) -> HTMLCollection {
HTMLCollection {
reflector_: Reflector::new(),
collection: collection,
}
}
pub fn new(window: &Window, collection: CollectionTypeId) -> Root<HTMLCollection> {
reflect_dom_object(box HTMLCollection::new_inherited(collection),
GlobalRef::Window(window), HTMLCollectionBinding::Wrap)
}
}
impl HTMLCollection {
pub fn create(window: &Window, root: &Node,
filter: Box<CollectionFilter+'static>) -> Root<HTMLCollection> {
HTMLCollection::new(window, CollectionTypeId::Live(JS::from_ref(root), filter))
}
fn all_elements(window: &Window, root: &Node,
namespace_filter: Option<Namespace>) -> Root<HTMLCollection> {
#[jstraceable]
struct AllElementFilter {
namespace_filter: Option<Namespace>
}
impl CollectionFilter for AllElementFilter {
fn filter(&self, elem: &Element, _root: &Node) -> bool {
match self.namespace_filter {
None => true,
Some(ref namespace) => *elem.namespace() == *namespace
}
}
}
let filter = AllElementFilter {namespace_filter: namespace_filter};
HTMLCollection::create(window, root, box filter)
}
pub fn by_tag_name(window: &Window, root: &Node, tag: DOMString)
-> Root<HTMLCollection> {
if tag == "*" {
return HTMLCollection::all_elements(window, root, None);
}
#[jstraceable]
struct TagNameFilter {
tag: Atom,
ascii_lower_tag: Atom,
}
impl CollectionFilter for TagNameFilter {
fn filter(&self, elem: &Element, _root: &Node) -> bool {
if elem.html_element_in_html_document() {
*elem.local_name() == self.ascii_lower_tag
} else {
*elem.local_name() == self.tag
}
}
}
let filter = TagNameFilter {
tag: Atom::from_slice(&tag),
ascii_lower_tag: Atom::from_slice(&tag.to_ascii_lowercase()),
};
HTMLCollection::create(window, root, box filter)
}
pub fn by_tag_name_ns(window: &Window, root: &Node, tag: DOMString,
maybe_ns: Option<DOMString>) -> Root<HTMLCollection> {
let namespace_filter = match maybe_ns {
Some(ref namespace) if namespace == &"*" => None,
ns => Some(namespace::from_domstring(ns)),
};
if tag == "*" {
return HTMLCollection::all_elements(window, root, namespace_filter);
}
#[jstraceable]
struct TagNameNSFilter {
tag: Atom,
namespace_filter: Option<Namespace>
}
impl CollectionFilter for TagNameNSFilter {
fn filter(&self, elem: &Element, _root: &Node) -> bool {
let ns_match = match self.namespace_filter {
Some(ref namespace) => {
*elem.namespace() == *namespace
},
None => true
};
ns_match && *elem.local_name() == self.tag
}
}
let filter = TagNameNSFilter {
tag: Atom::from_slice(&tag),
namespace_filter: namespace_filter
};
HTMLCollection::create(window, root, box filter)
}
pub fn by_class_name(window: &Window, root: &Node, classes: DOMString)
-> Root<HTMLCollection> {
#[jstraceable]
struct ClassNameFilter {
classes: Vec<Atom>
}
impl CollectionFilter for ClassNameFilter {
fn filter(&self, elem: &Element, _root: &Node) -> bool {
self.classes.iter().all(|class| elem.has_class(class))
}
}
let filter = ClassNameFilter {
classes: split_html_space_chars(&classes).map(|class| {
Atom::from_slice(class)
}).collect()
};
HTMLCollection::create(window, root, box filter)
}
pub fn children(window: &Window, root: &Node) -> Root<HTMLCollection> {
#[jstraceable]
struct ElementChildFilter;
impl CollectionFilter for ElementChildFilter {
fn filter(&self, elem: &Element, root: &Node) -> bool {
root.is_parent_of(NodeCast::from_ref(elem))
}
}
HTMLCollection::create(window, root, box ElementChildFilter)
}
fn traverse(root: &Node)
-> FilterMap<Skip<TreeIterator>,
fn(Root<Node>) -> Option<Root<Element>>> {
fn to_temporary(node: Root<Node>) -> Option<Root<Element>> {
ElementCast::to_root(node)
}
root.traverse_preorder()
.skip(1)
.filter_map(to_temporary)
}
}
impl<'a> HTMLCollectionMethods for &'a HTMLCollection {
// https://dom.spec.whatwg.org/#dom-htmlcollection-length
fn Length(self) -> u32 {
match self.collection {
CollectionTypeId::Static(ref elems) => elems.len() as u32,
CollectionTypeId::Live(ref root, ref filter) => {
let root = root.root();
HTMLCollection::traverse(root.r())
.filter(|element| filter.filter(element.r(), root.r()))
.count() as u32
}
}
}
// https://dom.spec.whatwg.org/#dom-htmlcollection-item
fn Item(self, index: u32) -> Option<Root<Element>> {
let index = index as usize;
match self.collection {
CollectionTypeId::Static(ref elems) => elems
.get(index).map(|t| t.root()),
CollectionTypeId::Live(ref root, ref filter) => {
let root = root.root();
HTMLCollection::traverse(root.r())
.filter(|element| filter.filter(element.r(), root.r()))
.nth(index)
}
}
}
// https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem
fn NamedItem(self, key: DOMString) -> Option<Root<Element>> {
// Step 1.
if key.is_empty() {
return None;
}
// Step 2.
match self.collection {
CollectionTypeId::Static(ref elems) => elems.iter()
.map(|elem| elem.root())
.find(|elem| {
elem.r().get_string_attribute(&atom!("name")) == key ||
elem.r().get_string_attribute(&atom!("id")) == key }),
CollectionTypeId::Live(ref root, ref filter) => {
let root = root.root();
HTMLCollection::traverse(root.r())
.filter(|element| filter.filter(element.r(), root.r()))
.find(|elem| {
elem.r().get_string_attribute(&atom!("name")) == key ||
elem.r().get_string_attribute(&atom!("id")) == key })
}
}
}
fn IndexedGetter(self, index: u32, found: &mut bool) -> Option<Root<Element>> {
let maybe_elem = self.Item(index);
*found = maybe_elem.is_some();
maybe_elem
}
fn NamedGetter(self, name: DOMString, found: &mut bool) -> Option<Root<Element>> {
let maybe_elem = self.NamedItem(name);
*found = maybe_elem.is_some();
maybe_elem
}
}