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/node.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/node.rs')
-rw-r--r-- | components/script/dom/node.rs | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 767a3132eb3..4ec6c878ad5 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -57,6 +57,7 @@ use euclid::rect::Rect; use euclid::size::Size2D; use heapsize::{HeapSizeOf, heap_size_of}; use html5ever::tree_builder::QuirksMode; +use html5ever_atoms::{Prefix, LocalName, Namespace, QualName}; use js::jsapi::{JSContext, JSObject, JSRuntime}; use libc::{self, c_void, uintptr_t}; use msg::constellation_msg::PipelineId; @@ -75,7 +76,6 @@ use std::default::Default; use std::iter; use std::mem; use std::ops::Range; -use string_cache::{Atom, Namespace, QualName}; use style::dom::OpaqueNode; use style::selector_impl::ServoSelectorImpl; use style::thread_state; @@ -1752,7 +1752,7 @@ impl Node { local: element.local_name().clone() }; let element = Element::create(name, - element.prefix().as_ref().map(|p| Atom::from(&**p)), + element.prefix().as_ref().map(|p| Prefix::from(&**p)), &document, ElementCreator::ScriptCreated); Root::upcast::<Node>(element) }, @@ -1818,21 +1818,21 @@ impl Node { pub fn namespace_to_string(namespace: Namespace) -> Option<DOMString> { match namespace { ns!() => None, - // FIXME(ajeffrey): convert directly from &Atom to DOMString - Namespace(ref ns) => Some(DOMString::from(&**ns)) + // FIXME(ajeffrey): convert directly from Namespace to DOMString + _ => Some(DOMString::from(&*namespace)) } } // https://dom.spec.whatwg.org/#locate-a-namespace pub fn locate_namespace(node: &Node, prefix: Option<DOMString>) -> Namespace { fn attr_defines_namespace(attr: &Attr, - prefix: &Option<Atom>) -> bool { + defined_prefix: &Option<LocalName>) -> bool { *attr.namespace() == ns!(xmlns) && - match (attr.prefix(), prefix) { - (&Some(ref attr_prefix), &Some(ref prefix)) => - attr_prefix == &atom!("xmlns") && - attr.local_name() == prefix, - (&None, &None) => *attr.local_name() == atom!("xmlns"), + match (attr.prefix(), defined_prefix) { + (&Some(ref attr_prefix), &Some(ref defined_prefix)) => + attr_prefix == &namespace_prefix!("xmlns") && + attr.local_name() == defined_prefix, + (&None, &None) => *attr.local_name() == local_name!("xmlns"), _ => false } } @@ -1845,8 +1845,11 @@ impl Node { return element.namespace().clone() } - // FIXME(ajeffrey): directly convert DOMString to Atom - let prefix_atom = prefix.as_ref().map(|s| Atom::from(&**s)); + // Even though this is conceptually a namespace prefix, + // in the `xmlns:foo="https://example.net/namespace" declaration + // it is a local name. + // FIXME(ajeffrey): directly convert DOMString to LocalName + let prefix_atom = prefix.as_ref().map(|s| LocalName::from(&**s)); // Step 2. let attrs = element.attrs(); |