Implement node::LookupPrefix and add test.

Tracking issue #1826.
This commit is contained in:
Md. Enzam Hossain 2015-03-26 03:26:58 +00:00
parent f7bfea5879
commit 4c5bebeb10
3 changed files with 97 additions and 4 deletions

View file

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