mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Merge pull request #2844 from glennw/atom-element
Convert element name to be stored as atom instead of string.
This commit is contained in:
commit
c3e6d956ff
6 changed files with 57 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1272,7 +1272,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().to_string(),
|
||||
element.namespace.clone(), &*document);
|
||||
NodeCast::from_temporary(element)
|
||||
},
|
||||
|
|
44
src/components/util/atom.rs
Normal file
44
src/components/util/atom.rs
Normal file
|
@ -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(slice: &str) -> Atom {
|
||||
Atom {
|
||||
atom: atom::Atom::from_slice(slice)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue