diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-11-03 11:19:44 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-03 11:19:44 -0500 |
commit | 5b4cc9568dbd5c15e5d2fbc62719172f11566ffa (patch) | |
tree | 596f3845740221c6748588ae1a79de79640e9922 /components/script/dom/document.rs | |
parent | dafc57e8abb3723e62e32e82ef04eb770eee2ea2 (diff) | |
parent | 53b638c0e29ba78448d07695343b7ddfa36c5141 (diff) | |
download | servo-5b4cc9568dbd5c15e5d2fbc62719172f11566ffa.tar.gz servo-5b4cc9568dbd5c15e5d2fbc62719172f11566ffa.zip |
Auto merge of #14043 - servo:string-cache-up, r=nox
Update to string-cache 0.3
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. -->
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14043)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script/dom/document.rs')
-rw-r--r-- | components/script/dom/document.rs | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 1f48f8fea01..2728bc757fb 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -91,6 +91,7 @@ use encoding::EncodingRef; use encoding::all::UTF_8; use euclid::point::Point2D; use html5ever::tree_builder::{LimitedQuirks, NoQuirks, Quirks, QuirksMode}; +use html5ever_atoms::{LocalName, QualName}; use ipc_channel::ipc::{self, IpcSender}; use js::jsapi::{JSContext, JSObject, JSRuntime}; use js::jsapi::JS_GetRuntime; @@ -110,6 +111,7 @@ use script_traits::{AnimationState, CompositorEvent, MouseButton, MouseEventType use script_traits::{ScriptMsg as ConstellationMsg, TouchpadPressurePhase}; use script_traits::{TouchEventType, TouchId}; use script_traits::UntrustedNodeAddress; +use servo_atoms::Atom; use std::ascii::AsciiExt; use std::borrow::ToOwned; use std::boxed::FnBox; @@ -122,7 +124,6 @@ use std::mem; use std::rc::Rc; use std::sync::Arc; use std::time::{Duration, Instant}; -use string_cache::{Atom, QualName}; use style::attr::AttrValue; use style::context::ReflowGoal; use style::selector_impl::ElementSnapshot; @@ -175,7 +176,7 @@ pub struct Document { quirks_mode: Cell<QuirksMode>, /// Caches for the getElement methods id_map: DOMRefCell<HashMap<Atom, Vec<JS<Element>>>>, - tag_map: DOMRefCell<HashMap<Atom, JS<HTMLCollection>>>, + tag_map: DOMRefCell<HashMap<LocalName, JS<HTMLCollection>>>, tagns_map: DOMRefCell<HashMap<QualName, JS<HTMLCollection>>>, classes_map: DOMRefCell<HashMap<Vec<Atom>, JS<HTMLCollection>>>, images: MutNullableHeap<JS<HTMLCollection>>, @@ -288,7 +289,7 @@ struct LinksFilter; impl CollectionFilter for LinksFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { (elem.is::<HTMLAnchorElement>() || elem.is::<HTMLAreaElement>()) && - elem.has_attribute(&atom!("href")) + elem.has_attribute(&local_name!("href")) } } @@ -312,7 +313,7 @@ impl CollectionFilter for ScriptsFilter { struct AnchorsFilter; impl CollectionFilter for AnchorsFilter { fn filter(&self, elem: &Element, _root: &Node) -> bool { - elem.is::<HTMLAnchorElement>() && elem.has_attribute(&atom!("href")) + elem.is::<HTMLAnchorElement>() && elem.has_attribute(&local_name!("href")) } } @@ -428,7 +429,7 @@ impl Document { let base = self.upcast::<Node>() .traverse_preorder() .filter_map(Root::downcast::<HTMLBaseElement>) - .find(|element| element.upcast::<Element>().has_attribute(&atom!("href"))); + .find(|element| element.upcast::<Element>().has_attribute(&local_name!("href"))); self.base_element.set(base.r()); } @@ -577,7 +578,7 @@ impl Document { fn get_anchor_by_name(&self, name: &str) -> Option<Root<Element>> { let check_anchor = |node: &HTMLAnchorElement| { let elem = node.upcast::<Element>(); - elem.get_attribute(&ns!(), &atom!("name")) + elem.get_attribute(&ns!(), &local_name!("name")) .map_or(false, |attr| &**attr.value() == name) }; let doc_node = self.upcast::<Node>(); @@ -1322,7 +1323,7 @@ impl Document { } } - pub fn get_body_attribute(&self, local_name: &Atom) -> DOMString { + pub fn get_body_attribute(&self, local_name: &LocalName) -> DOMString { match self.GetBody().and_then(Root::downcast::<HTMLBodyElement>) { Some(ref body) => { body.upcast::<Element>().get_string_attribute(local_name) @@ -1331,7 +1332,7 @@ impl Document { } } - pub fn set_body_attribute(&self, local_name: &Atom, value: DOMString) { + pub fn set_body_attribute(&self, local_name: &LocalName, value: DOMString) { if let Some(ref body) = self.GetBody().and_then(Root::downcast::<HTMLBodyElement>) { let body = body.upcast::<Element>(); let value = body.parse_attribute(&ns!(), &local_name, value); @@ -2175,13 +2176,13 @@ impl DocumentMethods for Document { // https://dom.spec.whatwg.org/#dom-document-getelementsbytagname fn GetElementsByTagName(&self, tag_name: DOMString) -> Root<HTMLCollection> { - let tag_atom = Atom::from(&*tag_name); + let tag_atom = LocalName::from(&*tag_name); match self.tag_map.borrow_mut().entry(tag_atom.clone()) { Occupied(entry) => Root::from_ref(entry.get()), Vacant(entry) => { let mut tag_copy = tag_name; tag_copy.make_ascii_lowercase(); - let ascii_lower_tag = Atom::from(tag_copy); + let ascii_lower_tag = LocalName::from(tag_copy); let result = HTMLCollection::by_atomic_tag_name(&self.window, self.upcast(), tag_atom, @@ -2198,7 +2199,7 @@ impl DocumentMethods for Document { tag_name: DOMString) -> Root<HTMLCollection> { let ns = namespace_from_domstring(maybe_ns); - let local = Atom::from(tag_name); + let local = LocalName::from(tag_name); let qname = QualName::new(ns, local); match self.tagns_map.borrow_mut().entry(qname.clone()) { Occupied(entry) => Root::from_ref(entry.get()), @@ -2241,7 +2242,7 @@ impl DocumentMethods for Document { if self.is_html_document { local_name.make_ascii_lowercase(); } - let name = QualName::new(ns!(html), Atom::from(local_name)); + let name = QualName::new(ns!(html), LocalName::from(local_name)); Ok(Element::create(name, None, self, ElementCreator::ScriptCreated)) } @@ -2265,7 +2266,7 @@ impl DocumentMethods for Document { if self.is_html_document { local_name.make_ascii_lowercase(); } - let name = Atom::from(local_name); + let name = LocalName::from(local_name); let value = AttrValue::String("".to_owned()); Ok(Attr::new(&self.window, name.clone(), value, name, ns!(), None, None)) @@ -2279,7 +2280,7 @@ impl DocumentMethods for Document { let (namespace, prefix, local_name) = try!(validate_and_extract(namespace, &qualified_name)); let value = AttrValue::String("".to_owned()); - let qualified_name = Atom::from(qualified_name); + let qualified_name = LocalName::from(qualified_name); Ok(Attr::new(&self.window, local_name, value, @@ -2465,12 +2466,12 @@ impl DocumentMethods for Document { // https://html.spec.whatwg.org/multipage/#document.title fn Title(&self) -> DOMString { let title = self.GetDocumentElement().and_then(|root| { - if root.namespace() == &ns!(svg) && root.local_name() == &atom!("svg") { + if root.namespace() == &ns!(svg) && root.local_name() == &local_name!("svg") { // Step 1. root.upcast::<Node>() .child_elements() .find(|node| { - node.namespace() == &ns!(svg) && node.local_name() == &atom!("title") + node.namespace() == &ns!(svg) && node.local_name() == &local_name!("title") }) .map(Root::upcast::<Node>) } else { @@ -2498,14 +2499,14 @@ impl DocumentMethods for Document { None => return, }; - let elem = if root.namespace() == &ns!(svg) && root.local_name() == &atom!("svg") { + let elem = if root.namespace() == &ns!(svg) && root.local_name() == &local_name!("svg") { let elem = root.upcast::<Node>().child_elements().find(|node| { - node.namespace() == &ns!(svg) && node.local_name() == &atom!("title") + node.namespace() == &ns!(svg) && node.local_name() == &local_name!("title") }); match elem { Some(elem) => Root::upcast::<Node>(elem), None => { - let name = QualName::new(ns!(svg), atom!("title")); + let name = QualName::new(ns!(svg), local_name!("title")); let elem = Element::create(name, None, self, ElementCreator::ScriptCreated); let parent = root.upcast::<Node>(); let child = elem.upcast::<Node>(); @@ -2522,7 +2523,7 @@ impl DocumentMethods for Document { None => { match self.GetHead() { Some(head) => { - let name = QualName::new(ns!(html), atom!("title")); + let name = QualName::new(ns!(html), local_name!("title")); let elem = Element::create(name, None, self, @@ -2617,7 +2618,7 @@ impl DocumentMethods for Document { if element.namespace() != &ns!(html) { return false; } - element.get_attribute(&ns!(), &atom!("name")) + element.get_attribute(&ns!(), &local_name!("name")) .map_or(false, |attr| &**attr.value() == &*name) }) } @@ -2785,22 +2786,22 @@ impl DocumentMethods for Document { // https://html.spec.whatwg.org/multipage/#dom-document-bgcolor fn BgColor(&self) -> DOMString { - self.get_body_attribute(&atom!("bgcolor")) + self.get_body_attribute(&local_name!("bgcolor")) } // https://html.spec.whatwg.org/multipage/#dom-document-bgcolor fn SetBgColor(&self, value: DOMString) { - self.set_body_attribute(&atom!("bgcolor"), value) + self.set_body_attribute(&local_name!("bgcolor"), value) } // https://html.spec.whatwg.org/multipage/#dom-document-fgcolor fn FgColor(&self) -> DOMString { - self.get_body_attribute(&atom!("text")) + self.get_body_attribute(&local_name!("text")) } // https://html.spec.whatwg.org/multipage/#dom-document-fgcolor fn SetFgColor(&self, value: DOMString) { - self.set_body_attribute(&atom!("text"), value) + self.set_body_attribute(&local_name!("text"), value) } #[allow(unsafe_code)] @@ -2827,10 +2828,10 @@ impl DocumentMethods for Document { }; match html_elem_type { HTMLElementTypeId::HTMLAppletElement => { - match elem.get_attribute(&ns!(), &atom!("name")) { + match elem.get_attribute(&ns!(), &local_name!("name")) { Some(ref attr) if attr.value().as_atom() == name => true, _ => { - match elem.get_attribute(&ns!(), &atom!("id")) { + match elem.get_attribute(&ns!(), &local_name!("id")) { Some(ref attr) => attr.value().as_atom() == name, None => false, } @@ -2838,18 +2839,18 @@ impl DocumentMethods for Document { } }, HTMLElementTypeId::HTMLFormElement => { - match elem.get_attribute(&ns!(), &atom!("name")) { + match elem.get_attribute(&ns!(), &local_name!("name")) { Some(ref attr) => attr.value().as_atom() == name, None => false, } }, HTMLElementTypeId::HTMLImageElement => { - match elem.get_attribute(&ns!(), &atom!("name")) { + match elem.get_attribute(&ns!(), &local_name!("name")) { Some(ref attr) => { if attr.value().as_atom() == name { true } else { - match elem.get_attribute(&ns!(), &atom!("id")) { + match elem.get_attribute(&ns!(), &local_name!("id")) { Some(ref attr) => attr.value().as_atom() == name, None => false, } |