diff options
author | Glenn Watson <gw@intuitionlibrary.com> | 2014-07-14 13:40:53 +1000 |
---|---|---|
committer | Glenn Watson <gw@intuitionlibrary.com> | 2014-07-16 07:33:29 +1000 |
commit | dddd3346a68d4a29b3751fe1d9b38d196350acbd (patch) | |
tree | eb0a1647997c8b37bb3a58b629dafd51f25eaeaf | |
parent | d97ec6995773ee79fbde053520bc580e7b33d15d (diff) | |
download | servo-dddd3346a68d4a29b3751fe1d9b38d196350acbd.tar.gz servo-dddd3346a68d4a29b3751fe1d9b38d196350acbd.zip |
Convert element name to be stored as atom instead of string.
-rw-r--r-- | src/components/script/dom/element.rs | 7 | ||||
-rw-r--r-- | src/components/script/dom/htmlcollection.rs | 9 | ||||
-rw-r--r-- | src/components/script/dom/htmlserializer.rs | 2 | ||||
-rw-r--r-- | src/components/script/dom/node.rs | 2 | ||||
-rw-r--r-- | src/components/util/atom.rs | 44 | ||||
-rw-r--r-- | src/components/util/util.rs | 2 |
6 files changed, 57 insertions, 9 deletions
diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs index a16c36f4708..05a13b3a8d9 100644 --- a/src/components/script/dom/element.rs +++ b/src/components/script/dom/element.rs @@ -32,6 +32,7 @@ use style; use servo_util::namespace; use servo_util::namespace::{Namespace, Null}; use servo_util::str::{DOMString, null_str_as_empty_ref, split_html_space_chars}; +use servo_util::atom::Atom; use std::ascii::StrAsciiExt; use std::cell::{Cell, RefCell}; @@ -40,7 +41,7 @@ use std::mem; #[deriving(Encodable)] pub struct Element { pub node: Node, - pub local_name: DOMString, // TODO: This should be an atom, not a DOMString. + pub local_name: Atom, pub namespace: Namespace, pub prefix: Option<DOMString>, pub attrs: RefCell<Vec<JS<Attr>>>, @@ -145,7 +146,7 @@ impl Element { pub fn new_inherited(type_id: ElementTypeId, local_name: DOMString, namespace: Namespace, prefix: Option<DOMString>, document: &JSRef<Document>) -> Element { Element { node: Node::new_inherited(ElementNodeTypeId(type_id), document), - local_name: local_name, + local_name: Atom::from_slice(local_name.as_slice()), namespace: namespace, prefix: prefix, attrs: RefCell::new(vec!()), @@ -460,7 +461,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> { } fn LocalName(&self) -> DOMString { - self.local_name.clone() + self.local_name.as_slice().to_string() } // http://dom.spec.whatwg.org/#dom-element-prefix diff --git a/src/components/script/dom/htmlcollection.rs b/src/components/script/dom/htmlcollection.rs index ed47802324b..a1d37700af6 100644 --- a/src/components/script/dom/htmlcollection.rs +++ b/src/components/script/dom/htmlcollection.rs @@ -10,6 +10,7 @@ use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; use dom::element::{Element, AttributeHandlers}; use dom::node::{Node, NodeHelpers}; use dom::window::Window; +use servo_util::atom::Atom; use servo_util::namespace::Namespace; use servo_util::str::{DOMString, split_html_space_chars}; @@ -60,7 +61,7 @@ impl HTMLCollection { pub fn by_tag_name(window: &JSRef<Window>, root: &JSRef<Node>, tag: DOMString) -> Temporary<HTMLCollection> { struct TagNameFilter { - tag: DOMString + tag: Atom } impl CollectionFilter for TagNameFilter { fn filter(&self, elem: &JSRef<Element>, _root: &JSRef<Node>) -> bool { @@ -68,7 +69,7 @@ impl HTMLCollection { } } let filter = TagNameFilter { - tag: tag + tag: Atom::from_slice(tag.as_slice()) }; HTMLCollection::create(window, root, box filter) } @@ -76,7 +77,7 @@ impl HTMLCollection { pub fn by_tag_name_ns(window: &JSRef<Window>, root: &JSRef<Node>, tag: DOMString, namespace: Namespace) -> Temporary<HTMLCollection> { struct TagNameNSFilter { - tag: DOMString, + tag: Atom, namespace: Namespace } impl CollectionFilter for TagNameNSFilter { @@ -85,7 +86,7 @@ impl HTMLCollection { } } let filter = TagNameNSFilter { - tag: tag, + tag: Atom::from_slice(tag.as_slice()), namespace: namespace }; HTMLCollection::create(window, root, box filter) diff --git a/src/components/script/dom/htmlserializer.rs b/src/components/script/dom/htmlserializer.rs index 3f2b753c55b..e2bfee571c3 100644 --- a/src/components/script/dom/htmlserializer.rs +++ b/src/components/script/dom/htmlserializer.rs @@ -129,7 +129,7 @@ fn serialize_elem(elem: &JSRef<Element>, open_elements: &mut Vec<String>, html: } if !elem.deref().is_void() { - open_elements.push(elem.deref().local_name.clone()); + open_elements.push(elem.deref().local_name.as_slice().to_string()); } } diff --git a/src/components/script/dom/node.rs b/src/components/script/dom/node.rs index 52b23c3b3e2..ba3d7bbff1b 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -1273,7 +1273,7 @@ impl Node { ElementNodeTypeId(..) => { let element: &JSRef<Element> = ElementCast::to_ref(node).unwrap(); let element = element.deref(); - let element = build_element_from_tag(element.local_name.clone(), + let element = build_element_from_tag(element.local_name.as_slice().into_string(), element.namespace.clone(), &*document); NodeCast::from_temporary(element) }, diff --git a/src/components/util/atom.rs b/src/components/util/atom.rs new file mode 100644 index 00000000000..769b75e48ca --- /dev/null +++ b/src/components/util/atom.rs @@ -0,0 +1,44 @@ +/* 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/. */ + +// Provides a wrapper around the Atom type in the string cache +// crate. It's needed so that it can implement the Encodable +// trait which is required by Servo. + +use serialize::{Encoder, Encodable}; +use std::fmt; +use string_cache::atom; + +#[deriving(Clone, Eq, PartialEq)] +pub struct Atom { + atom: atom::Atom, +} + +impl Atom { + #[inline(always)] + pub fn from_slice(string_to_add: &str) -> Atom { + Atom { + atom: atom::Atom::from_slice(string_to_add) + } + } +} + +impl Str for Atom { + #[inline(always)] + fn as_slice<'t>(&'t self) -> &'t str { + self.atom.as_slice() + } +} + +impl fmt::Show for Atom { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:s}", self.atom.as_slice()) + } +} + +impl<E, S: Encoder<E>> Encodable<S, E> for Atom { + fn encode(&self, _s: &mut S) -> Result<(), E> { + Ok(()) + } +} diff --git a/src/components/util/util.rs b/src/components/util/util.rs index 9df40c70f19..b378d5074f2 100644 --- a/src/components/util/util.rs +++ b/src/components/util/util.rs @@ -29,7 +29,9 @@ extern crate sync; extern crate task_info; extern crate std_time = "time"; extern crate std_url = "url"; +extern crate string_cache; +pub mod atom; pub mod cache; pub mod debug_utils; pub mod geometry; |