mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
parent
f7bfea5879
commit
4c5bebeb10
3 changed files with 97 additions and 4 deletions
|
@ -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 {
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
use dom::attr::{Attr, AttrHelpers};
|
||||
use dom::bindings::cell::DOMRefCell;
|
||||
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
||||
use dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
|
||||
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
||||
use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
|
||||
|
@ -2278,9 +2279,27 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
|
|||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-node-lookupprefix
|
||||
fn LookupPrefix(self, _prefix: Option<DOMString>) -> Option<DOMString> {
|
||||
// FIXME (#1826) implement.
|
||||
None
|
||||
fn LookupPrefix(self, namespace: Option<DOMString>) -> Option<DOMString> {
|
||||
// Step 1.
|
||||
if null_str_as_empty(&namespace).is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Step 2.
|
||||
match self.type_id() {
|
||||
NodeTypeId::Element(..) => ElementCast::to_ref(self).unwrap().lookup_prefix(namespace),
|
||||
NodeTypeId::Document => {
|
||||
DocumentCast::to_ref(self).unwrap().GetDocumentElement().and_then(|element| {
|
||||
element.root().r().lookup_prefix(namespace)
|
||||
})
|
||||
},
|
||||
NodeTypeId::DocumentType | NodeTypeId::DocumentFragment => None,
|
||||
_ => {
|
||||
self.GetParentElement().and_then(|element| {
|
||||
element.root().r().lookup_prefix(namespace)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-node-lookupnamespaceuri
|
||||
|
@ -2517,4 +2536,3 @@ pub enum NodeDamage {
|
|||
/// Other parts of a node changed; attributes, text content, etc.
|
||||
OtherNodeDamage,
|
||||
}
|
||||
|
||||
|
|
48
tests/content/test_node_lookupPrefix.html
Normal file
48
tests/content/test_node_lookupPrefix.html
Normal file
|
@ -0,0 +1,48 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="harness.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<x xarg="xval">
|
||||
<!--comment-->
|
||||
<?test test?>
|
||||
TEST
|
||||
<x/>
|
||||
</x>
|
||||
<script>
|
||||
document.documentElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:x", "test");
|
||||
document.body.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:s", "test");
|
||||
var x = document.getElementsByTagName("x")[0];
|
||||
x.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:t", "test");
|
||||
var yprefix = document.createElementNS("ynamespace", "yprefix:ylocalname");
|
||||
var ynoprefix = document.createElementNS("ynamespace", "ylocalname");
|
||||
ynoprefix.setAttribute("ynoprefixattr", "yval");
|
||||
ynoprefix.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:yattr", "yval");
|
||||
|
||||
// Step 1
|
||||
is(document.lookupPrefix(""), null);
|
||||
is(document.lookupPrefix(null), null);
|
||||
|
||||
// Element step 1
|
||||
is(yprefix.lookupPrefix("ynamespace"), "yprefix");
|
||||
is(ynoprefix.lookupPrefix("ynamespace"), null);
|
||||
|
||||
// Element step 2
|
||||
is(x.lookupPrefix("test"), "t");
|
||||
is(ynoprefix.lookupPrefix("yval"), "yattr");
|
||||
|
||||
// analogous to wpt - https://github.com/w3c/web-platform-tests/blob/master/dom/nodes/Node-lookupPrefix.xhtml
|
||||
is(document.lookupPrefix("test"), "x");
|
||||
is(x.lookupPrefix("test"), "t");
|
||||
is(x.lookupPrefix("http://www.w3.org/1999/xhtml"), null);
|
||||
is(x.lookupPrefix("something"), null);
|
||||
is(x.lookupPrefix(null), null);
|
||||
is(x.parentNode.lookupPrefix("test"), "s");
|
||||
is(x.firstChild.lookupPrefix("test"), "t");
|
||||
is(x.childNodes[1].lookupPrefix("test"), "t");
|
||||
is(x.childNodes[2].lookupPrefix("test"), "t");
|
||||
is(x.lastChild.lookupPrefix("test"), "t");
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue