Change Namespace::to_str() to not allocate and return a reference.

This commit is contained in:
Simon Sapin 2013-12-09 15:10:09 +00:00
parent 28575c20bf
commit 61c7f2f220
5 changed files with 23 additions and 23 deletions

View file

@ -75,7 +75,7 @@ impl Attr {
} }
pub fn GetNamespaceURI(&self) -> Option<DOMString> { pub fn GetNamespaceURI(&self) -> Option<DOMString> {
self.namespace.to_str() self.namespace.to_str().map(|s| s.to_owned())
} }
pub fn GetPrefix(&self) -> Option<DOMString> { pub fn GetPrefix(&self) -> Option<DOMString> {

View file

@ -129,8 +129,8 @@ impl ElementLike for Element {
self.tag_name.as_slice() self.tag_name.as_slice()
} }
fn get_namespace<'a>(&'a self) -> ~str { fn get_namespace_url<'a>(&'a self) -> &'a str {
self.namespace.to_str().unwrap_or(~"") self.namespace.to_str().unwrap_or("")
} }
fn get_attr(&self, name: &str) -> Option<~str> { fn get_attr(&self, name: &str) -> Option<~str> {

View file

@ -19,26 +19,26 @@ pub enum Namespace {
impl Namespace { impl Namespace {
pub fn from_str(url: Option<DOMString>) -> Namespace { pub fn from_str(url: Option<DOMString>) -> Namespace {
match null_str_as_empty_ref(&url) { match null_str_as_empty_ref(&url) {
&"http://www.w3.org/1999/xhtml" => HTML, "http://www.w3.org/1999/xhtml" => HTML,
&"http://www.w3.org/XML/1998/namespace" => XML, "http://www.w3.org/XML/1998/namespace" => XML,
&"http://www.w3.org/2000/xmlns/" => XMLNS, "http://www.w3.org/2000/xmlns/" => XMLNS,
&"http://www.w3.org/1999/xlink" => XLink, "http://www.w3.org/1999/xlink" => XLink,
&"http://www.w3.org/2000/svg" => SVG, "http://www.w3.org/2000/svg" => SVG,
&"http://www.w3.org/1998/Math/MathML" => MathML, "http://www.w3.org/1998/Math/MathML" => MathML,
&"" => Null, "" => Null,
ns => Other(ns.to_owned()) ns => Other(ns.to_owned())
} }
} }
pub fn to_str(&self) -> Option<DOMString> { pub fn to_str<'a>(&'a self) -> Option<&'a str> {
match *self { match *self {
Null => None, Null => None,
HTML => Some(~"http://www.w3.org/1999/xhtml"), HTML => Some("http://www.w3.org/1999/xhtml"),
XML => Some(~"http://www.w3.org/XML/1998/namespace"), XML => Some("http://www.w3.org/XML/1998/namespace"),
XMLNS => Some(~"http://www.w3.org/2000/xmlns/"), XMLNS => Some("http://www.w3.org/2000/xmlns/"),
XLink => Some(~"http://www.w3.org/1999/xlink"), XLink => Some("http://www.w3.org/1999/xlink"),
SVG => Some(~"http://www.w3.org/2000/svg"), SVG => Some("http://www.w3.org/2000/svg"),
MathML => Some(~"http://www.w3.org/1998/Math/MathML"), MathML => Some("http://www.w3.org/1998/Math/MathML"),
Other(ref x) => Some(x.to_owned()) Other(ref x) => Some(x.as_slice())
} }
} }
} }

View file

@ -427,7 +427,7 @@ fn matches_simple_selector<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: Ele
} }
NamespaceSelector(ref url) => { NamespaceSelector(ref url) => {
do element.with_imm_element_like |element: &E| { do element.with_imm_element_like |element: &E| {
str::eq_slice(element.get_namespace(), *url) element.get_namespace_url() == url.as_slice()
} }
} }
// TODO: case-sensitivity depends on the document type and quirks mode // TODO: case-sensitivity depends on the document type and quirks mode
@ -538,11 +538,11 @@ fn matches_generic_nth_child<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: E
}; };
let mut element_local_name = ""; let mut element_local_name = "";
let mut element_namespace = ~""; let mut element_namespace = "";
if is_of_type { if is_of_type {
do element.with_imm_element_like |element: &E| { do element.with_imm_element_like |element: &E| {
element_local_name = element.get_local_name(); element_local_name = element.get_local_name();
element_namespace = element.get_namespace(); element_namespace = element.get_namespace_url();
} }
} }
@ -564,7 +564,7 @@ fn matches_generic_nth_child<N: TreeNode<T>, T: TreeNodeRefAsElement<N, E>, E: E
if is_of_type { if is_of_type {
do node.with_imm_element_like |node: &E| { do node.with_imm_element_like |node: &E| {
if element_local_name == node.get_local_name() && if element_local_name == node.get_local_name() &&
element_namespace == node.get_namespace() { element_namespace == node.get_namespace_url() {
index += 1; index += 1;
} }
} }

View file

@ -348,7 +348,7 @@ pub trait TreeNode<Ref: TreeNodeRef<Self>> {
pub trait ElementLike { pub trait ElementLike {
fn get_local_name<'a>(&'a self) -> &'a str; fn get_local_name<'a>(&'a self) -> &'a str;
fn get_namespace<'a>(&'a self) -> ~str; fn get_namespace_url<'a>(&'a self) -> &'a str;
fn get_attr(&self, name: &str) -> Option<~str>; fn get_attr(&self, name: &str) -> Option<~str>;
fn get_link(&self) -> Option<~str>; fn get_link(&self) -> Option<~str>;
} }