Cleanup and modernize script::dom::document

This commit is contained in:
Corey Farwell 2015-03-23 21:43:53 -04:00
parent bfb80f20b5
commit e770f05e0c

View file

@ -339,9 +339,7 @@ impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
let root: JSRef<Node> = NodeCast::from_ref(root.r()); let root: JSRef<Node> = NodeCast::from_ref(root.r());
for node in root.traverse_preorder() { for node in root.traverse_preorder() {
let elem: Option<JSRef<Element>> = ElementCast::to_ref(node); let elem: Option<JSRef<Element>> = ElementCast::to_ref(node);
match elem { if let Some(elem) = elem {
None => {},
Some(elem) => {
if (*elements)[head].root().r() == elem { if (*elements)[head].root().r() == elem {
head += 1; head += 1;
} }
@ -350,7 +348,6 @@ impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
} }
} }
} }
}
elements.insert_unrooted(head, &element); elements.insert_unrooted(head, &element);
} }
@ -392,9 +389,7 @@ impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
let root = NodeCast::from_ref(root); let root = NodeCast::from_ref(root);
let win = self.window.root(); let win = self.window.root();
let address = match win.r().layout().hit_test(root.to_trusted_node_address(), *point) { let address = match win.r().layout().hit_test(root.to_trusted_node_address(), *point) {
Ok(HitTestResponse(node_address)) => { Ok(HitTestResponse(node_address)) => Some(node_address),
Some(node_address)
}
Err(()) => { Err(()) => {
debug!("layout query error"); debug!("layout query error");
None None
@ -412,12 +407,8 @@ impl<'a> DocumentHelpers<'a> for JSRef<'a, Document> {
let root: JSRef<Node> = NodeCast::from_ref(root); let root: JSRef<Node> = NodeCast::from_ref(root);
let win = self.window.root(); let win = self.window.root();
match win.r().layout().mouse_over(root.to_trusted_node_address(), *point) { match win.r().layout().mouse_over(root.to_trusted_node_address(), *point) {
Ok(MouseOverResponse(node_address)) => { Ok(MouseOverResponse(node_address)) => node_address,
node_address Err(()) => vec!(),
}
Err(()) => {
vec!()
}
} }
} }
@ -918,27 +909,22 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
// http://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid // http://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid
fn GetElementById(self, id: DOMString) -> Option<Temporary<Element>> { fn GetElementById(self, id: DOMString) -> Option<Temporary<Element>> {
let id = Atom::from_slice(id.as_slice()); let id = Atom::from_slice(&id);
// FIXME(https://github.com/rust-lang/rust/issues/23338) // FIXME(https://github.com/rust-lang/rust/issues/23338)
let idmap = self.idmap.borrow(); let idmap = self.idmap.borrow();
match idmap.get(&id) { idmap.get(&id).map(|ref elements| Temporary::new((*elements)[0].clone()))
None => None,
Some(ref elements) => Some(Temporary::new((*elements)[0].clone())),
}
} }
// http://dom.spec.whatwg.org/#dom-document-createelement // http://dom.spec.whatwg.org/#dom-document-createelement
fn CreateElement(self, local_name: DOMString) -> Fallible<Temporary<Element>> { fn CreateElement(self, mut local_name: DOMString) -> Fallible<Temporary<Element>> {
if xml_name_type(local_name.as_slice()) == InvalidXMLName { if xml_name_type(&local_name) == InvalidXMLName {
debug!("Not a valid element name"); debug!("Not a valid element name");
return Err(InvalidCharacter); return Err(InvalidCharacter);
} }
let local_name = if self.is_html_document { if self.is_html_document {
local_name.as_slice().to_ascii_lowercase() local_name = local_name.to_ascii_lowercase()
} else { }
local_name let name = QualName::new(ns!(HTML), Atom::from_slice(&local_name));
};
let name = QualName::new(ns!(HTML), Atom::from_slice(local_name.as_slice()));
Ok(Element::create(name, None, self, ElementCreator::ScriptCreated)) Ok(Element::create(name, None, self, ElementCreator::ScriptCreated))
} }
@ -947,7 +933,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
namespace: Option<DOMString>, namespace: Option<DOMString>,
qualified_name: DOMString) -> Fallible<Temporary<Element>> { qualified_name: DOMString) -> Fallible<Temporary<Element>> {
let ns = namespace::from_domstring(namespace); let ns = namespace::from_domstring(namespace);
match xml_name_type(qualified_name.as_slice()) { match xml_name_type(&qualified_name) {
InvalidXMLName => { InvalidXMLName => {
debug!("Not a valid element name"); debug!("Not a valid element name");
return Err(InvalidCharacter); return Err(InvalidCharacter);
@ -960,7 +946,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
} }
let (prefix_from_qname, local_name_from_qname) let (prefix_from_qname, local_name_from_qname)
= get_attribute_parts(qualified_name.as_slice()); = get_attribute_parts(&qualified_name);
match (&ns, prefix_from_qname, local_name_from_qname) { match (&ns, prefix_from_qname, local_name_from_qname) {
// throw if prefix is not null and namespace is null // throw if prefix is not null and namespace is null
(&ns!(""), Some(_), _) => { (&ns!(""), Some(_), _) => {
@ -968,12 +954,12 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
return Err(NamespaceError); return Err(NamespaceError);
}, },
// throw if prefix is "xml" and namespace is not the XML namespace // throw if prefix is "xml" and namespace is not the XML namespace
(_, Some(ref prefix), _) if "xml" == prefix.as_slice() && ns != ns!(XML) => { (_, Some(ref prefix), _) if "xml" == *prefix && ns != ns!(XML) => {
debug!("Namespace must be the xml namespace if the prefix is 'xml'"); debug!("Namespace must be the xml namespace if the prefix is 'xml'");
return Err(NamespaceError); return Err(NamespaceError);
}, },
// throw if namespace is the XMLNS namespace and neither qualifiedName nor prefix is "xmlns" // throw if namespace is the XMLNS namespace and neither qualifiedName nor prefix is "xmlns"
(&ns!(XMLNS), Some(ref prefix), _) if "xmlns" == prefix.as_slice() => {}, (&ns!(XMLNS), Some(ref prefix), _) if "xmlns" == *prefix => {},
(&ns!(XMLNS), _, "xmlns") => {}, (&ns!(XMLNS), _, "xmlns") => {},
(&ns!(XMLNS), _, _) => { (&ns!(XMLNS), _, _) => {
debug!("The prefix or the qualified name must be 'xmlns' if namespace is the XMLNS namespace "); debug!("The prefix or the qualified name must be 'xmlns' if namespace is the XMLNS namespace ");
@ -989,15 +975,15 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
// http://dom.spec.whatwg.org/#dom-document-createattribute // http://dom.spec.whatwg.org/#dom-document-createattribute
fn CreateAttribute(self, local_name: DOMString) -> Fallible<Temporary<Attr>> { fn CreateAttribute(self, local_name: DOMString) -> Fallible<Temporary<Attr>> {
if xml_name_type(local_name.as_slice()) == InvalidXMLName { if xml_name_type(&local_name) == InvalidXMLName {
debug!("Not a valid element name"); debug!("Not a valid element name");
return Err(InvalidCharacter); return Err(InvalidCharacter);
} }
let window = self.window.root(); let window = self.window.root();
let name = Atom::from_slice(local_name.as_slice()); let name = Atom::from_slice(&local_name);
// repetition used because string_cache::atom::Atom is non-copyable // repetition used because string_cache::atom::Atom is non-copyable
let l_name = Atom::from_slice(local_name.as_slice()); let l_name = Atom::from_slice(&local_name);
let value = AttrValue::String("".to_owned()); let value = AttrValue::String("".to_owned());
Ok(Attr::new(window.r(), name, value, l_name, ns!(""), None, None)) Ok(Attr::new(window.r(), name, value, l_name, ns!(""), None, None))
@ -1009,8 +995,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
} }
// http://dom.spec.whatwg.org/#dom-document-createtextnode // http://dom.spec.whatwg.org/#dom-document-createtextnode
fn CreateTextNode(self, data: DOMString) fn CreateTextNode(self, data: DOMString) -> Temporary<Text> {
-> Temporary<Text> {
Text::new(data, self) Text::new(data, self)
} }
@ -1020,15 +1005,15 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
} }
// http://dom.spec.whatwg.org/#dom-document-createprocessinginstruction // http://dom.spec.whatwg.org/#dom-document-createprocessinginstruction
fn CreateProcessingInstruction(self, target: DOMString, fn CreateProcessingInstruction(self, target: DOMString, data: DOMString) ->
data: DOMString) -> Fallible<Temporary<ProcessingInstruction>> { Fallible<Temporary<ProcessingInstruction>> {
// Step 1. // Step 1.
if xml_name_type(target.as_slice()) == InvalidXMLName { if xml_name_type(&target) == InvalidXMLName {
return Err(InvalidCharacter); return Err(InvalidCharacter);
} }
// Step 2. // Step 2.
if data.as_slice().contains("?>") { if data.contains("?>") {
return Err(InvalidCharacter); return Err(InvalidCharacter);
} }
@ -1070,7 +1055,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
fn CreateEvent(self, interface: DOMString) -> Fallible<Temporary<Event>> { fn CreateEvent(self, interface: DOMString) -> Fallible<Temporary<Event>> {
let window = self.window.root(); let window = self.window.root();
match interface.as_slice().to_ascii_lowercase().as_slice() { match interface.to_ascii_lowercase().as_slice() {
"uievents" | "uievent" => Ok(EventCast::from_temporary( "uievents" | "uievent" => Ok(EventCast::from_temporary(
UIEvent::new_uninitialized(window.r()))), UIEvent::new_uninitialized(window.r()))),
"mouseevents" | "mouseevent" => Ok(EventCast::from_temporary( "mouseevents" | "mouseevent" => Ok(EventCast::from_temporary(
@ -1120,11 +1105,11 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
t t
}); });
for text in children { for text in children {
title.push_str(text.characterdata().data().as_slice()); title.push_str(&text.characterdata().data());
} }
}); });
}); });
let v: Vec<&str> = split_html_space_chars(title.as_slice()).collect(); let v: Vec<&str> = split_html_space_chars(&title).collect();
v.connect(" ") v.connect(" ")
} }