Auto merge of #6318 - Ms2ger:optimize-lookup-prefix, r=nox

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/6318)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-06-10 04:30:38 -06:00
commit 3ece6bc166
2 changed files with 12 additions and 7 deletions

View file

@ -561,7 +561,7 @@ pub trait ElementHelpers<'a> {
fn get_important_inline_style_declaration(self, property: &Atom) -> Option<PropertyDeclaration>; fn get_important_inline_style_declaration(self, property: &Atom) -> Option<PropertyDeclaration>;
fn serialize(self, traversal_scope: TraversalScope) -> Fallible<DOMString>; fn serialize(self, traversal_scope: TraversalScope) -> Fallible<DOMString>;
fn get_root_element(self) -> Temporary<Element>; fn get_root_element(self) -> Temporary<Element>;
fn lookup_prefix(self, namespace: Option<DOMString>) -> Option<DOMString>; fn lookup_prefix(self, namespace: Namespace) -> Option<DOMString>;
} }
impl<'a> ElementHelpers<'a> for JSRef<'a, Element> { impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
@ -722,21 +722,23 @@ impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
} }
// https://dom.spec.whatwg.org/#locate-a-namespace-prefix // https://dom.spec.whatwg.org/#locate-a-namespace-prefix
fn lookup_prefix(self, namespace: Option<DOMString>) -> Option<DOMString> { fn lookup_prefix(self, namespace: Namespace) -> Option<DOMString> {
for node in NodeCast::from_ref(self).inclusive_ancestors() { for node in NodeCast::from_ref(self).inclusive_ancestors() {
match ElementCast::to_ref(node.root().r()) { match ElementCast::to_ref(node.root().r()) {
Some(element) => { Some(element) => {
// Step 1. // Step 1.
if element.GetNamespaceURI() == namespace && element.GetPrefix().is_some() { if *element.namespace() == namespace {
return element.GetPrefix(); if let Some(prefix) = element.GetPrefix() {
return Some(prefix);
}
} }
// Step 2. // Step 2.
let attrs = element.Attributes().root(); let attrs = element.Attributes().root();
for i in 0..attrs.r().Length() { for i in 0..attrs.r().Length() {
let attr = attrs.r().Item(i).unwrap().root(); let attr = attrs.r().Item(i).unwrap().root();
if attr.r().GetPrefix() == Some("xmlns".to_owned()) && if *attr.r().prefix() == Some(atom!("xmlns")) &&
Some(attr.r().Value()) == namespace { **attr.r().value() == *namespace.0 {
return Some(attr.r().LocalName()); return Some(attr.r().LocalName());
} }
} }

View file

@ -52,6 +52,7 @@ use devtools_traits::NodeInfo;
use parse::html::parse_html_fragment; use parse::html::parse_html_fragment;
use script_traits::UntrustedNodeAddress; use script_traits::UntrustedNodeAddress;
use util::geometry::Au; use util::geometry::Au;
use util::namespace;
use util::str::{DOMString, null_str_as_empty}; use util::str::{DOMString, null_str_as_empty};
use selectors::parser::{Selector, AttrSelector, NamespaceConstraint}; use selectors::parser::{Selector, AttrSelector, NamespaceConstraint};
use selectors::parser::parse_author_origin_selector_list_from_str; use selectors::parser::parse_author_origin_selector_list_from_str;
@ -2470,8 +2471,10 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
// https://dom.spec.whatwg.org/#dom-node-lookupprefix // https://dom.spec.whatwg.org/#dom-node-lookupprefix
fn LookupPrefix(self, namespace: Option<DOMString>) -> Option<DOMString> { fn LookupPrefix(self, namespace: Option<DOMString>) -> Option<DOMString> {
let namespace = namespace::from_domstring(namespace);
// Step 1. // Step 1.
if null_str_as_empty(&namespace).is_empty() { if namespace == ns!("") {
return None; return None;
} }