fune/servo/components/script/dom/htmlcollection.rs
Simon Sapin 7cb2f9a2dc servo: Merge #14043 - Update to string-cache 0.3 (from servo:string-cache-up); r=nox
Previously, `string-cache` defined:
*  An string-like `Atom` type,
* An `atom!("foo")` macro that expands to a value of that type, for a set of strings known at compile-time,
* A `struct Namespace(Atom);` type
* A `ns!(html)` macro that maps known prefixed to `Namespace` values with the corresponding namespace URL.

Adding a string to the static set required making a change to the `string-cache` crate.

With 0.3, the `Atom` type is now generic, with a type parameter that provides a set of static strings. We can have multiple such sets, defined in different crates. The `string_cache_codegen` crate, to be used in build scripts, generates code that defines such a set, a new atom type (a type alias for `Atom<_>` with the type parameter set), and an `atom!`-like macro.

The html5ever repository has a new `html5ever_atoms` crate that defines three such types: `Prefix`, `Namespace`, and `LocalName` (with respective `namespace_prefix!`, `namespace_url!`, and `local_name!` macros). It also defines the `ns!` macro like before.

This repository has a new `servo_atoms` crate in `components/atoms` that, for now, defines a single `Atom` type (and `atom!`) macro. (`servo_atoms::Atom` is defined as something like `type Atom = string_cache::Atom<ServoStaticStringSet>;`, so overall there’s now two types named `Atom`.)

In this PR, `servo_atoms::Atom` is used for everything else that was `string_cache::Atom` before. But more atom types can be defined as needed. Two reasons to do this are to auto-generate the set of static strings (I’m planning to do this for CSS property names, which is the motivation for this change), or to have the type system help us avoid mix up unrelated things (this is why we had a `Namespace` type ever before this change).

Introducing new types helped me find a bug: when creating a new attribute `dom::Element::set_style_attr`, would pass `Some(atom!("style"))` instead of `None` (now `Option<html5ever_atoms::Prefix>` instead of `Option<string_cache::Atom>`) to the `prefix` argument of `Attr::new`. I suppose the author of that code confused it with the `local_name` argument.

---

Note that Stylo is not affected by any of this. The `gecko_string_cache` module is unchanged, with a single `Atom` type. The `style` crate conditionally compiles `Prefix` and `LocalName` re-exports for that are both `gecko_string_cache::Atom` on stylo.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: 5b4cc9568dbd5c15e5d2fbc62719172f11566ffa
2016-11-03 11:19:44 -05:00

354 lines
13 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::inheritance::Castable;
use dom::bindings::js::{JS, Root, MutNullableHeap};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::str::DOMString;
use dom::bindings::trace::JSTraceable;
use dom::bindings::xmlname::namespace_from_domstring;
use dom::element::Element;
use dom::node::Node;
use dom::window::Window;
use html5ever_atoms::{LocalName, QualName};
use servo_atoms::Atom;
use std::ascii::AsciiExt;
use std::cell::Cell;
use style::str::split_html_space_chars;
pub trait CollectionFilter : JSTraceable {
fn filter<'a>(&self, elem: &'a Element, root: &'a Node) -> bool;
}
// An optional u32, using maxint to represent None.
// It would be nicer just to use Option<u32> for this, but that would produce word
// alignment issues since Option<u32> uses 33 bits.
#[derive(Clone, Copy, JSTraceable, HeapSizeOf)]
struct OptionU32 {
bits: u32,
}
impl OptionU32 {
fn to_option(self) -> Option<u32> {
if self.bits == u32::max_value() {
None
} else {
Some(self.bits)
}
}
fn some(bits: u32) -> OptionU32 {
assert!(bits != u32::max_value());
OptionU32 { bits: bits }
}
fn none() -> OptionU32 {
OptionU32 { bits: u32::max_value() }
}
}
#[dom_struct]
pub struct HTMLCollection {
reflector_: Reflector,
root: JS<Node>,
#[ignore_heap_size_of = "Contains a trait object; can't measure due to #6870"]
filter: Box<CollectionFilter + 'static>,
// We cache the version of the root node and all its decendents,
// the length of the collection, and a cursor into the collection.
// FIXME: make the cached cursor element a weak pointer
cached_version: Cell<u64>,
cached_cursor_element: MutNullableHeap<JS<Element>>,
cached_cursor_index: Cell<OptionU32>,
cached_length: Cell<OptionU32>,
}
impl HTMLCollection {
#[allow(unrooted_must_root)]
pub fn new_inherited(root: &Node, filter: Box<CollectionFilter + 'static>) -> HTMLCollection {
HTMLCollection {
reflector_: Reflector::new(),
root: JS::from_ref(root),
filter: filter,
// Default values for the cache
cached_version: Cell::new(root.inclusive_descendants_version()),
cached_cursor_element: MutNullableHeap::new(None),
cached_cursor_index: Cell::new(OptionU32::none()),
cached_length: Cell::new(OptionU32::none()),
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, root: &Node, filter: Box<CollectionFilter + 'static>) -> Root<HTMLCollection> {
reflect_dom_object(box HTMLCollection::new_inherited(root, filter),
window, HTMLCollectionBinding::Wrap)
}
pub fn create(window: &Window, root: &Node,
filter: Box<CollectionFilter + 'static>) -> Root<HTMLCollection> {
HTMLCollection::new(window, root, filter)
}
fn validate_cache(&self) {
// Clear the cache if the root version is different from our cached version
let cached_version = self.cached_version.get();
let curr_version = self.root.inclusive_descendants_version();
if curr_version != cached_version {
// Default values for the cache
self.cached_version.set(curr_version);
self.cached_cursor_element.set(None);
self.cached_length.set(OptionU32::none());
self.cached_cursor_index.set(OptionU32::none());
}
}
fn set_cached_cursor(&self, index: u32, element: Option<Root<Element>>) -> Option<Root<Element>> {
if let Some(element) = element {
self.cached_cursor_index.set(OptionU32::some(index));
self.cached_cursor_element.set(Some(&element));
Some(element)
} else {
None
}
}
pub fn by_tag_name(window: &Window, root: &Node, mut tag: DOMString)
-> Root<HTMLCollection> {
let tag_atom = LocalName::from(&*tag);
tag.make_ascii_lowercase();
let ascii_lower_tag = LocalName::from(tag); // FIXME(ajeffrey): don't clone atom if it was already lowercased.
HTMLCollection::by_atomic_tag_name(window, root, tag_atom, ascii_lower_tag)
}
pub fn by_atomic_tag_name(window: &Window, root: &Node, tag_atom: LocalName, ascii_lower_tag: LocalName)
-> Root<HTMLCollection> {
#[derive(JSTraceable, HeapSizeOf)]
struct TagNameFilter {
tag: LocalName,
ascii_lower_tag: LocalName,
}
impl CollectionFilter for TagNameFilter {
fn filter(&self, elem: &Element, _root: &Node) -> bool {
if self.tag == local_name!("*") {
true
} else if elem.html_element_in_html_document() {
*elem.local_name() == self.ascii_lower_tag
} else {
*elem.local_name() == self.tag
}
}
}
let filter = TagNameFilter {
tag: tag_atom,
ascii_lower_tag: ascii_lower_tag,
};
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 local = LocalName::from(tag);
let ns = namespace_from_domstring(maybe_ns);
let qname = QualName::new(ns, local);
HTMLCollection::by_qual_tag_name(window, root, qname)
}
pub fn by_qual_tag_name(window: &Window, root: &Node, qname: QualName) -> Root<HTMLCollection> {
#[derive(JSTraceable, HeapSizeOf)]
struct TagNameNSFilter {
qname: QualName
}
impl CollectionFilter for TagNameNSFilter {
fn filter(&self, elem: &Element, _root: &Node) -> bool {
((self.qname.ns == namespace_url!("*")) || (self.qname.ns == *elem.namespace())) &&
((self.qname.local == local_name!("*")) || (self.qname.local == *elem.local_name()))
}
}
let filter = TagNameNSFilter {
qname: qname
};
HTMLCollection::create(window, root, box filter)
}
pub fn by_class_name(window: &Window, root: &Node, classes: DOMString)
-> Root<HTMLCollection> {
let class_atoms = split_html_space_chars(&classes).map(Atom::from).collect();
HTMLCollection::by_atomic_class_name(window, root, class_atoms)
}
pub fn by_atomic_class_name(window: &Window, root: &Node, classes: Vec<Atom>)
-> Root<HTMLCollection> {
#[derive(JSTraceable, HeapSizeOf)]
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: classes
};
HTMLCollection::create(window, root, box filter)
}
pub fn children(window: &Window, root: &Node) -> Root<HTMLCollection> {
#[derive(JSTraceable, HeapSizeOf)]
struct ElementChildFilter;
impl CollectionFilter for ElementChildFilter {
fn filter(&self, elem: &Element, root: &Node) -> bool {
root.is_parent_of(elem.upcast())
}
}
HTMLCollection::create(window, root, box ElementChildFilter)
}
pub fn elements_iter_after(&self, after: &Node) -> HTMLCollectionElementsIter {
// Iterate forwards from a node.
HTMLCollectionElementsIter {
node_iter: box after.following_nodes(&self.root),
root: Root::from_ref(&self.root),
filter: &self.filter,
}
}
pub fn elements_iter(&self) -> HTMLCollectionElementsIter {
// Iterate forwards from the root.
self.elements_iter_after(&*self.root)
}
pub fn elements_iter_before(&self, before: &Node) -> HTMLCollectionElementsIter {
// Iterate backwards from a node.
HTMLCollectionElementsIter {
node_iter: box before.preceding_nodes(&self.root),
root: Root::from_ref(&self.root),
filter: &self.filter,
}
}
pub fn root_node(&self) -> Root<Node> {
Root::from_ref(&self.root)
}
}
// TODO: Make this generic, and avoid code duplication
pub struct HTMLCollectionElementsIter<'a> {
node_iter: Box<Iterator<Item = Root<Node>>>,
root: Root<Node>,
filter: &'a Box<CollectionFilter>,
}
impl<'a> Iterator for HTMLCollectionElementsIter<'a> {
type Item = Root<Element>;
fn next(&mut self) -> Option<Self::Item> {
let filter = &self.filter;
let root = &self.root;
self.node_iter.by_ref()
.filter_map(Root::downcast)
.filter(|element| filter.filter(&element, root))
.next()
}
}
impl HTMLCollectionMethods for HTMLCollection {
// https://dom.spec.whatwg.org/#dom-htmlcollection-length
fn Length(&self) -> u32 {
self.validate_cache();
if let Some(cached_length) = self.cached_length.get().to_option() {
// Cache hit
cached_length
} else {
// Cache miss, calculate the length
let length = self.elements_iter().count() as u32;
self.cached_length.set(OptionU32::some(length));
length
}
}
// https://dom.spec.whatwg.org/#dom-htmlcollection-item
fn Item(&self, index: u32) -> Option<Root<Element>> {
self.validate_cache();
if let Some(element) = self.cached_cursor_element.get() {
// Cache hit, the cursor element is set
if let Some(cached_index) = self.cached_cursor_index.get().to_option() {
if cached_index == index {
// The cursor is the element we're looking for
Some(element)
} else if cached_index < index {
// The cursor is before the element we're looking for
// Iterate forwards, starting at the cursor.
let offset = index - (cached_index + 1);
let node: Root<Node> = Root::upcast(element);
self.set_cached_cursor(index, self.elements_iter_after(&node).nth(offset as usize))
} else {
// The cursor is after the element we're looking for
// Iterate backwards, starting at the cursor.
let offset = cached_index - (index + 1);
let node: Root<Node> = Root::upcast(element);
self.set_cached_cursor(index, self.elements_iter_before(&node).nth(offset as usize))
}
} else {
// Cache miss
// Iterate forwards through all the nodes
self.set_cached_cursor(index, self.elements_iter().nth(index as usize))
}
} else {
// Cache miss
// Iterate forwards through all the nodes
self.set_cached_cursor(index, self.elements_iter().nth(index as usize))
}
}
// 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.
self.elements_iter().find(|elem| {
elem.get_string_attribute(&local_name!("id")) == key ||
(elem.namespace() == &ns!(html) && elem.get_string_attribute(&local_name!("name")) == key)
})
}
// https://dom.spec.whatwg.org/#dom-htmlcollection-item
fn IndexedGetter(&self, index: u32) -> Option<Root<Element>> {
self.Item(index)
}
// check-tidy: no specs after this line
fn NamedGetter(&self, name: DOMString) -> Option<Root<Element>> {
self.NamedItem(name)
}
// https://dom.spec.whatwg.org/#interface-htmlcollection
fn SupportedPropertyNames(&self) -> Vec<DOMString> {
// Step 1
let mut result = vec![];
// Step 2
for elem in self.elements_iter() {
// Step 2.1
let id_attr = elem.get_string_attribute(&local_name!("id"));
if !id_attr.is_empty() && !result.contains(&id_attr) {
result.push(id_attr)
}
// Step 2.2
let name_attr = elem.get_string_attribute(&local_name!("name"));
if !name_attr.is_empty() && !result.contains(&name_attr) && *elem.namespace() == ns!(html) {
result.push(name_attr)
}
}
// Step 3
result
}
}