servo: Merge #5401 - Cleanup and modernize script::dom::element (from frewsxcv:cleanup-element); r=Ms2ger

Source-Repo: https://github.com/servo/servo
Source-Revision: e916ee081aac4739728843b5761c860983f6f913
This commit is contained in:
Corey Farwell 2015-03-27 16:25:00 -06:00
Родитель ebfcbc1f1f
Коммит 9e49a08e92
1 изменённых файлов: 74 добавлений и 107 удалений

Просмотреть файл

@ -132,7 +132,7 @@ impl Element {
document: JSRef<Document>) -> Element { document: JSRef<Document>) -> Element {
Element { Element {
node: Node::new_inherited(NodeTypeId::Element(type_id), document), node: Node::new_inherited(NodeTypeId::Element(type_id), document),
local_name: Atom::from_slice(local_name.as_slice()), local_name: Atom::from_slice(&local_name),
namespace: namespace, namespace: namespace,
prefix: prefix, prefix: prefix,
attrs: DOMRefCell::new(vec!()), attrs: DOMRefCell::new(vec!()),
@ -458,7 +458,7 @@ impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
fn parsed_name(self, name: DOMString) -> DOMString { fn parsed_name(self, name: DOMString) -> DOMString {
if self.html_element_in_html_document() { if self.html_element_in_html_document() {
name.as_slice().to_ascii_lowercase() name.to_ascii_lowercase()
} else { } else {
name name
} }
@ -486,12 +486,10 @@ impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
fn summarize(self) -> Vec<AttrInfo> { fn summarize(self) -> Vec<AttrInfo> {
let attrs = self.Attributes().root(); let attrs = self.Attributes().root();
let mut i = 0;
let mut summarized = vec!(); let mut summarized = vec!();
while i < 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();
summarized.push(attr.r().summarize()); summarized.push(attr.r().summarize());
i += 1;
} }
summarized summarized
} }
@ -516,24 +514,18 @@ impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
let index = declarations.normal let index = declarations.normal
.iter() .iter()
.position(|decl| decl.name() == property); .position(|decl| decl.name() == property);
match index { if let Some(index) = index {
Some(index) => {
declarations.normal.make_unique().remove(index); declarations.normal.make_unique().remove(index);
return; return;
} }
None => ()
}
let index = declarations.important let index = declarations.important
.iter() .iter()
.position(|decl| decl.name() == property); .position(|decl| decl.name() == property);
match index { if let Some(index) = index {
Some(index) => {
declarations.important.make_unique().remove(index); declarations.important.make_unique().remove(index);
return; return;
} }
None => ()
}
}); });
} }
@ -665,7 +657,7 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
// FIXME(https://github.com/rust-lang/rust/issues/23338) // FIXME(https://github.com/rust-lang/rust/issues/23338)
let attr = attr.r(); let attr = attr.r();
let attr_local_name = attr.local_name(); let attr_local_name = attr.local_name();
if *attr_local_name == *local_name { if attr_local_name == local_name {
Some(Temporary::from_rooted(attr)) Some(Temporary::from_rooted(attr))
} else { } else {
None None
@ -699,7 +691,7 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
assert!(!name.as_slice().contains(":")); assert!(!name.as_slice().contains(":"));
self.do_set_attribute(name.clone(), value, name.clone(), self.do_set_attribute(name.clone(), value, name.clone(),
ns!(""), None, |attr| *attr.local_name() == *name); ns!(""), None, |attr| attr.local_name() == name);
} }
// https://html.spec.whatwg.org/multipage/dom.html#attr-data-* // https://html.spec.whatwg.org/multipage/dom.html#attr-data-*
@ -711,7 +703,7 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
} }
// Steps 2-5. // Steps 2-5.
let name = Atom::from_slice(name.as_slice()); let name = Atom::from_slice(&name);
let value = self.parse_attribute(&ns!(""), &name, value); let value = self.parse_attribute(&ns!(""), &name, value);
self.do_set_attribute(name.clone(), value, name.clone(), ns!(""), None, |attr| { self.do_set_attribute(name.clone(), value, name.clone(), ns!(""), None, |attr| {
*attr.name() == name && *attr.namespace() == ns!("") *attr.name() == name && *attr.namespace() == ns!("")
@ -763,9 +755,7 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
*attr.r().local_name() == local_name *attr.r().local_name() == local_name
}); });
match idx { if let Some(idx) = idx {
None => (),
Some(idx) => {
if namespace == ns!("") { if namespace == ns!("") {
let attr = (*self.attrs.borrow())[idx].root(); let attr = (*self.attrs.borrow())[idx].root();
vtable_for(&NodeCast::from_ref(self)).before_remove_attr(attr.r()); vtable_for(&NodeCast::from_ref(self)).before_remove_attr(attr.r());
@ -776,12 +766,12 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
let node: JSRef<Node> = NodeCast::from_ref(self); let node: JSRef<Node> = NodeCast::from_ref(self);
if node.is_in_doc() { if node.is_in_doc() {
let document = document_from_node(self).root(); let document = document_from_node(self).root();
if local_name == atom!("style") { let damage = if local_name == atom!("style") {
document.r().content_changed(node, NodeDamage::NodeStyleDamaged); NodeDamage::NodeStyleDamaged
} else { } else {
document.r().content_changed(node, NodeDamage::OtherNodeDamage); NodeDamage::OtherNodeDamage
} };
} document.r().content_changed(node, damage);
} }
}; };
} }
@ -936,7 +926,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
None => self.local_name.as_slice().into_cow() None => self.local_name.as_slice().into_cow()
}; };
if self.html_element_in_html_document() { if self.html_element_in_html_document() {
qualified_name.as_slice().to_ascii_uppercase() qualified_name.to_ascii_uppercase()
} else { } else {
qualified_name.into_owned() qualified_name.into_owned()
} }
@ -982,7 +972,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
// http://dom.spec.whatwg.org/#dom-element-getattribute // http://dom.spec.whatwg.org/#dom-element-getattribute
fn GetAttribute(self, name: DOMString) -> Option<DOMString> { fn GetAttribute(self, name: DOMString) -> Option<DOMString> {
let name = self.parsed_name(name); let name = self.parsed_name(name);
self.get_attribute(ns!(""), &Atom::from_slice(name.as_slice())).root() self.get_attribute(ns!(""), &Atom::from_slice(&name)).root()
.map(|s| s.r().Value()) .map(|s| s.r().Value())
} }
@ -991,7 +981,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
namespace: Option<DOMString>, namespace: Option<DOMString>,
local_name: DOMString) -> Option<DOMString> { local_name: DOMString) -> Option<DOMString> {
let namespace = namespace::from_domstring(namespace); let namespace = namespace::from_domstring(namespace);
self.get_attribute(namespace, &Atom::from_slice(local_name.as_slice())).root() self.get_attribute(namespace, &Atom::from_slice(&local_name)).root()
.map(|attr| attr.r().Value()) .map(|attr| attr.r().Value())
} }
@ -1000,16 +990,15 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
name: DOMString, name: DOMString,
value: DOMString) -> ErrorResult { value: DOMString) -> ErrorResult {
// Step 1. // Step 1.
match xml_name_type(name.as_slice()) { if xml_name_type(&name) == InvalidXMLName {
InvalidXMLName => return Err(InvalidCharacter), return Err(InvalidCharacter);
_ => {}
} }
// Step 2. // Step 2.
let name = self.parsed_name(name); let name = self.parsed_name(name);
// Step 3-5. // Step 3-5.
let name = Atom::from_slice(name.as_slice()); let name = Atom::from_slice(&name);
let value = self.parse_attribute(&ns!(""), &name, value); let value = self.parse_attribute(&ns!(""), &name, value);
self.do_set_attribute(name.clone(), value, name.clone(), ns!(""), None, |attr| { self.do_set_attribute(name.clone(), value, name.clone(), ns!(""), None, |attr| {
*attr.name() == name *attr.name() == name
@ -1025,7 +1014,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
// Step 1. // Step 1.
let namespace = namespace::from_domstring(namespace_url); let namespace = namespace::from_domstring(namespace_url);
let name_type = xml_name_type(name.as_slice()); let name_type = xml_name_type(&name);
match name_type { match name_type {
// Step 2. // Step 2.
InvalidXMLName => return Err(InvalidCharacter), InvalidXMLName => return Err(InvalidCharacter),
@ -1035,28 +1024,26 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
} }
// Step 4. // Step 4.
let (prefix, local_name) = get_attribute_parts(name.as_slice()); let (prefix, local_name) = get_attribute_parts(&name);
match prefix {
Some(ref prefix_str) => { if let Some(ref prefix_str) = prefix {
// Step 5. // Step 5.
if namespace == ns!("") { if namespace == ns!("") {
return Err(NamespaceError); return Err(NamespaceError);
} }
// Step 6. // Step 6.
if "xml" == prefix_str.as_slice() && namespace != ns!(XML) { if "xml" == *prefix_str && namespace != ns!(XML) {
return Err(NamespaceError); return Err(NamespaceError);
} }
// Step 7b. // Step 7b.
if "xmlns" == prefix_str.as_slice() && namespace != ns!(XMLNS) { if "xmlns" == *prefix_str && namespace != ns!(XMLNS) {
return Err(NamespaceError); return Err(NamespaceError);
} }
},
None => {}
} }
let name = Atom::from_slice(name.as_slice()); let name = Atom::from_slice(&name);
let local_name = Atom::from_slice(local_name); let local_name = Atom::from_slice(local_name);
let xmlns = atom!("xmlns"); let xmlns = atom!("xmlns");
@ -1084,7 +1071,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
// http://dom.spec.whatwg.org/#dom-element-removeattribute // http://dom.spec.whatwg.org/#dom-element-removeattribute
fn RemoveAttribute(self, name: DOMString) { fn RemoveAttribute(self, name: DOMString) {
let name = self.parsed_name(name); let name = self.parsed_name(name);
self.remove_attribute(ns!(""), name.as_slice()) self.remove_attribute(ns!(""), &name)
} }
// http://dom.spec.whatwg.org/#dom-element-removeattributens // http://dom.spec.whatwg.org/#dom-element-removeattributens
@ -1092,7 +1079,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
namespace: Option<DOMString>, namespace: Option<DOMString>,
localname: DOMString) { localname: DOMString) {
let namespace = namespace::from_domstring(namespace); let namespace = namespace::from_domstring(namespace);
self.remove_attribute(namespace, localname.as_slice()) self.remove_attribute(namespace, &localname)
} }
// http://dom.spec.whatwg.org/#dom-element-hasattribute // http://dom.spec.whatwg.org/#dom-element-hasattribute
@ -1240,7 +1227,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
// http://dom.spec.whatwg.org/#dom-element-matches // http://dom.spec.whatwg.org/#dom-element-matches
fn Matches(self, selectors: DOMString) -> Fallible<bool> { fn Matches(self, selectors: DOMString) -> Fallible<bool> {
match parse_author_origin_selector_list_from_str(selectors.as_slice()) { match parse_author_origin_selector_list_from_str(&selectors) {
Err(()) => Err(Syntax), Err(()) => Err(Syntax),
Ok(ref selectors) => { Ok(ref selectors) => {
let root: JSRef<Node> = NodeCast::from_ref(self); let root: JSRef<Node> = NodeCast::from_ref(self);
@ -1251,7 +1238,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
// https://dom.spec.whatwg.org/#dom-element-closest // https://dom.spec.whatwg.org/#dom-element-closest
fn Closest(self, selectors: DOMString) -> Fallible<Option<Temporary<Element>>> { fn Closest(self, selectors: DOMString) -> Fallible<Option<Temporary<Element>>> {
match parse_author_origin_selector_list_from_str(selectors.as_slice()) { match parse_author_origin_selector_list_from_str(&selectors) {
Err(()) => Err(Syntax), Err(()) => Err(Syntax),
Ok(ref selectors) => { Ok(ref selectors) => {
let root: JSRef<Node> = NodeCast::from_ref(self); let root: JSRef<Node> = NodeCast::from_ref(self);
@ -1267,14 +1254,12 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
pub fn get_attribute_parts<'a>(name: &'a str) -> (Option<&'a str>, &'a str) { pub fn get_attribute_parts<'a>(name: &'a str) -> (Option<&'a str>, &'a str) {
//FIXME: Throw for XML-invalid names //FIXME: Throw for XML-invalid names
//FIXME: Throw for XMLNS-invalid names //FIXME: Throw for XMLNS-invalid names
let (prefix, local_name) = if name.contains(":") { if name.contains(":") {
let mut parts = name.splitn(1, ':'); let mut parts = name.splitn(1, ':');
(Some(parts.next().unwrap()), parts.next().unwrap()) (Some(parts.next().unwrap()), parts.next().unwrap())
} else { } else {
(None, name) (None, name)
}; }
(prefix, local_name)
} }
impl<'a> VirtualMethods for JSRef<'a, Element> { impl<'a> VirtualMethods for JSRef<'a, Element> {
@ -1288,10 +1273,10 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
s.after_set_attr(attr); s.after_set_attr(attr);
} }
let node: JSRef<Node> = NodeCast::from_ref(*self);
match attr.local_name() { match attr.local_name() {
&atom!("style") => { &atom!("style") => {
// Modifying the `style` attribute might change style. // Modifying the `style` attribute might change style.
let node: JSRef<Node> = NodeCast::from_ref(*self);
let doc = document_from_node(*self).root(); let doc = document_from_node(*self).root();
let base_url = doc.r().url(); let base_url = doc.r().url();
let value = attr.value(); let value = attr.value();
@ -1304,7 +1289,6 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
} }
&atom!("class") => { &atom!("class") => {
// Modifying a class can change style. // Modifying a class can change style.
let node: JSRef<Node> = NodeCast::from_ref(*self);
if node.is_in_doc() { if node.is_in_doc() {
let document = document_from_node(*self).root(); let document = document_from_node(*self).root();
document.r().content_changed(node, NodeDamage::NodeStyleDamaged); document.r().content_changed(node, NodeDamage::NodeStyleDamaged);
@ -1312,7 +1296,6 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
} }
&atom!("id") => { &atom!("id") => {
// Modifying an ID might change style. // Modifying an ID might change style.
let node: JSRef<Node> = NodeCast::from_ref(*self);
let value = attr.value(); let value = attr.value();
if node.is_in_doc() { if node.is_in_doc() {
let doc = document_from_node(*self).root(); let doc = document_from_node(*self).root();
@ -1325,7 +1308,6 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
} }
_ => { _ => {
// Modifying any other attribute might change arbitrary things. // Modifying any other attribute might change arbitrary things.
let node: JSRef<Node> = NodeCast::from_ref(*self);
if node.is_in_doc() { if node.is_in_doc() {
let document = document_from_node(*self).root(); let document = document_from_node(*self).root();
document.r().content_changed(node, NodeDamage::OtherNodeDamage); document.r().content_changed(node, NodeDamage::OtherNodeDamage);
@ -1339,12 +1321,12 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
s.before_remove_attr(attr); s.before_remove_attr(attr);
} }
let node: JSRef<Node> = NodeCast::from_ref(*self);
match attr.local_name() { match attr.local_name() {
&atom!("style") => { &atom!("style") => {
// Modifying the `style` attribute might change style. // Modifying the `style` attribute might change style.
*self.style_attribute.borrow_mut() = None; *self.style_attribute.borrow_mut() = None;
let node: JSRef<Node> = NodeCast::from_ref(*self);
if node.is_in_doc() { if node.is_in_doc() {
let doc = document_from_node(*self).root(); let doc = document_from_node(*self).root();
doc.r().content_changed(node, NodeDamage::NodeStyleDamaged); doc.r().content_changed(node, NodeDamage::NodeStyleDamaged);
@ -1352,7 +1334,6 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
} }
&atom!("id") => { &atom!("id") => {
// Modifying an ID can change style. // Modifying an ID can change style.
let node: JSRef<Node> = NodeCast::from_ref(*self);
let value = attr.value(); let value = attr.value();
if node.is_in_doc() { if node.is_in_doc() {
let doc = document_from_node(*self).root(); let doc = document_from_node(*self).root();
@ -1365,7 +1346,6 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
} }
&atom!("class") => { &atom!("class") => {
// Modifying a class can change style. // Modifying a class can change style.
let node: JSRef<Node> = NodeCast::from_ref(*self);
if node.is_in_doc() { if node.is_in_doc() {
let document = document_from_node(*self).root(); let document = document_from_node(*self).root();
document.r().content_changed(node, NodeDamage::NodeStyleDamaged); document.r().content_changed(node, NodeDamage::NodeStyleDamaged);
@ -1373,7 +1353,6 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
} }
_ => { _ => {
// Modifying any other attribute might change arbitrary things. // Modifying any other attribute might change arbitrary things.
let node: JSRef<Node> = NodeCast::from_ref(*self);
if node.is_in_doc() { if node.is_in_doc() {
let doc = document_from_node(*self).root(); let doc = document_from_node(*self).root();
doc.r().content_changed(node, NodeDamage::OtherNodeDamage); doc.r().content_changed(node, NodeDamage::OtherNodeDamage);
@ -1397,8 +1376,7 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
if !tree_in_doc { return; } if !tree_in_doc { return; }
match self.get_attribute(ns!(""), &atom!("id")).root() { if let Some(attr) = self.get_attribute(ns!(""), &atom!("id")).root() {
Some(attr) => {
let doc = document_from_node(*self).root(); let doc = document_from_node(*self).root();
let value = attr.r().Value(); let value = attr.r().Value();
if !value.is_empty() { if !value.is_empty() {
@ -1406,8 +1384,6 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
doc.r().register_named_element(*self, value); doc.r().register_named_element(*self, value);
} }
} }
_ => ()
}
} }
fn unbind_from_tree(&self, tree_in_doc: bool) { fn unbind_from_tree(&self, tree_in_doc: bool) {
@ -1417,8 +1393,7 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
if !tree_in_doc { return; } if !tree_in_doc { return; }
match self.get_attribute(ns!(""), &atom!("id")).root() { if let Some(attr) = self.get_attribute(ns!(""), &atom!("id")).root() {
Some(attr) => {
let doc = document_from_node(*self).root(); let doc = document_from_node(*self).root();
let value = attr.r().Value(); let value = attr.r().Value();
if !value.is_empty() { if !value.is_empty() {
@ -1426,8 +1401,6 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
doc.r().unregister_named_element(*self, value); doc.r().unregister_named_element(*self, value);
} }
} }
_ => ()
}
} }
} }
@ -1532,20 +1505,14 @@ impl<'a> style::node::TElement<'a> for JSRef<'a, Element> {
fn each_class<F>(self, mut callback: F) fn each_class<F>(self, mut callback: F)
where F: FnMut(&Atom) where F: FnMut(&Atom)
{ {
match self.get_attribute(ns!(""), &atom!("class")).root() { if let Some(ref attr) = self.get_attribute(ns!(""), &atom!("class")).root() {
None => {} if let Some(tokens) = attr.r().value().tokens() {
Some(ref attr) => { for token in tokens {
match attr.r().value().tokens() {
None => {}
Some(tokens) => {
for token in tokens.iter() {
callback(token) callback(token)
} }
} }
} }
} }
}
}
fn has_nonzero_border(self) -> bool { fn has_nonzero_border(self) -> bool {
let table_element: Option<JSRef<HTMLTableElement>> = HTMLTableElementCast::to_ref(self); let table_element: Option<JSRef<HTMLTableElement>> = HTMLTableElementCast::to_ref(self);
match table_element { match table_element {