Cleanup attribute setting functions a bit.

This commit is contained in:
Ms2ger 2013-11-13 17:38:08 +01:00
parent 4eb8449621
commit 591de8cff5
3 changed files with 21 additions and 29 deletions

View file

@ -6,7 +6,7 @@
use dom::attrlist::AttrList; use dom::attrlist::AttrList;
use dom::bindings::utils::{Reflectable, DOMString, ErrorResult, Fallible, Reflector}; use dom::bindings::utils::{Reflectable, DOMString, ErrorResult, Fallible, Reflector};
use dom::bindings::utils::{null_str_as_empty, null_str_as_empty_ref, NamespaceError}; use dom::bindings::utils::{null_str_as_empty, NamespaceError};
use dom::bindings::utils::{InvalidCharacter, QName, Name, InvalidXMLName, xml_name_type}; use dom::bindings::utils::{InvalidCharacter, QName, Name, InvalidXMLName, xml_name_type};
use dom::htmlcollection::HTMLCollection; use dom::htmlcollection::HTMLCollection;
use dom::clientrect::ClientRect; use dom::clientrect::ClientRect;
@ -182,20 +182,19 @@ impl<'self> Element {
pub fn set_attr(&mut self, pub fn set_attr(&mut self,
abstract_self: AbstractNode<ScriptView>, abstract_self: AbstractNode<ScriptView>,
raw_name: &Option<DOMString>, name: DOMString,
raw_value: &Option<DOMString>) -> ErrorResult { value: DOMString) -> ErrorResult {
self.set_attribute(abstract_self, namespace::Null, raw_name, raw_value) self.set_attribute(abstract_self, namespace::Null, name, value)
} }
pub fn set_attribute(&mut self, pub fn set_attribute(&mut self,
abstract_self: AbstractNode<ScriptView>, abstract_self: AbstractNode<ScriptView>,
namespace: Namespace, namespace: Namespace,
raw_name: &Option<DOMString>, raw_name: DOMString,
raw_value: &Option<DOMString>) -> ErrorResult { value: DOMString) -> ErrorResult {
//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 name = null_str_as_empty(raw_name).to_ascii_lower(); let name = raw_name.to_ascii_lower();
let value = null_str_as_empty(raw_value);
let (prefix, local_name) = if name.contains(":") { let (prefix, local_name) = if name.contains(":") {
let parts: ~[&str] = name.splitn_iter(':', 1).collect(); let parts: ~[&str] = name.splitn_iter(':', 1).collect();
(Some(parts[0].to_owned()), parts[1].to_owned()) (Some(parts[0].to_owned()), parts[1].to_owned())
@ -243,19 +242,18 @@ impl<'self> Element {
} }
}); });
self.after_set_attr(abstract_self, &namespace, local_name, raw_value); self.after_set_attr(abstract_self, &namespace, local_name, value);
Ok(()) Ok(())
} }
fn after_set_attr(&mut self, fn after_set_attr(&mut self,
abstract_self: AbstractNode<ScriptView>, abstract_self: AbstractNode<ScriptView>,
namespace: &Namespace, namespace: &Namespace,
local_name: ~str, local_name: DOMString,
value: &Option<DOMString>) { value: DOMString) {
if "style" == local_name && *namespace == namespace::Null { if "style" == local_name && *namespace == namespace::Null {
self.style_attribute = Some(style::parse_style_attribute( self.style_attribute = Some(style::parse_style_attribute(value));
null_str_as_empty_ref(value)));
} }
// TODO: update owner document's id hashmap for `document.getElementById()` // TODO: update owner document's id hashmap for `document.getElementById()`
@ -266,12 +264,12 @@ impl<'self> Element {
match abstract_self.type_id() { match abstract_self.type_id() {
ElementNodeTypeId(HTMLImageElementTypeId) => { ElementNodeTypeId(HTMLImageElementTypeId) => {
do abstract_self.with_mut_image_element |image| { do abstract_self.with_mut_image_element |image| {
image.AfterSetAttr(&local_name, &null_str_as_empty(value)); image.AfterSetAttr(&local_name, &value);
} }
} }
ElementNodeTypeId(HTMLIframeElementTypeId) => { ElementNodeTypeId(HTMLIframeElementTypeId) => {
do abstract_self.with_mut_iframe_element |iframe| { do abstract_self.with_mut_iframe_element |iframe| {
iframe.AfterSetAttr(&local_name, &null_str_as_empty(value)); iframe.AfterSetAttr(&local_name, &value);
} }
} }
_ => () _ => ()
@ -297,7 +295,7 @@ impl Element {
} }
pub fn SetId(&mut self, abstract_self: AbstractNode<ScriptView>, id: &DOMString) { pub fn SetId(&mut self, abstract_self: AbstractNode<ScriptView>, id: &DOMString) {
self.set_attribute(abstract_self, namespace::Null, &Some(~"id"), &Some(id.clone())); self.set_attribute(abstract_self, namespace::Null, ~"id", id.clone());
} }
pub fn Attributes(&mut self, abstract_self: AbstractNode<ScriptView>) -> @mut AttrList { pub fn Attributes(&mut self, abstract_self: AbstractNode<ScriptView>) -> @mut AttrList {
@ -325,7 +323,7 @@ impl Element {
abstract_self: AbstractNode<ScriptView>, abstract_self: AbstractNode<ScriptView>,
name: &DOMString, name: &DOMString,
value: &DOMString) -> ErrorResult { value: &DOMString) -> ErrorResult {
self.set_attr(abstract_self, &Some(name.clone()), &Some(value.clone())); self.set_attr(abstract_self, name.clone(), value.clone());
Ok(()) Ok(())
} }
@ -342,7 +340,7 @@ impl Element {
} }
let namespace = Namespace::from_str(namespace_url); let namespace = Namespace::from_str(namespace_url);
self.set_attribute(abstract_self, namespace, &Some(name.clone()), &Some(value.clone())) self.set_attribute(abstract_self, namespace, name.clone(), value.clone())
} }
pub fn RemoveAttribute(&self, _name: &DOMString) -> ErrorResult { pub fn RemoveAttribute(&self, _name: &DOMString) -> ErrorResult {

View file

@ -82,9 +82,7 @@ impl HTMLImageElement {
abstract_self: AbstractNode<ScriptView>, abstract_self: AbstractNode<ScriptView>,
src: &DOMString) -> ErrorResult { src: &DOMString) -> ErrorResult {
let node = &mut self.htmlelement.element; let node = &mut self.htmlelement.element;
node.set_attr(abstract_self, node.set_attr(abstract_self, ~"src", src.clone());
&Some(~"src"),
&Some(src.clone()));
Ok(()) Ok(())
} }
@ -127,9 +125,7 @@ impl HTMLImageElement {
abstract_self: AbstractNode<ScriptView>, abstract_self: AbstractNode<ScriptView>,
width: u32) -> ErrorResult { width: u32) -> ErrorResult {
let node = &mut self.htmlelement.element; let node = &mut self.htmlelement.element;
node.set_attr(abstract_self, node.set_attr(abstract_self, ~"width", width.to_str());
&Some(~"width"),
&Some(width.to_str()));
Ok(()) Ok(())
} }
@ -148,9 +144,7 @@ impl HTMLImageElement {
abstract_self: AbstractNode<ScriptView>, abstract_self: AbstractNode<ScriptView>,
height: u32) -> ErrorResult { height: u32) -> ErrorResult {
let node = &mut self.htmlelement.element; let node = &mut self.htmlelement.element;
node.set_attr(abstract_self, node.set_attr(abstract_self, ~"height", height.to_str());
&Some(~"height"),
&Some(height.to_str()));
Ok(()) Ok(())
} }

View file

@ -342,8 +342,8 @@ pub fn parse_html(cx: *JSContext,
for attr in tag.attributes.iter() { for attr in tag.attributes.iter() {
element.set_attribute(node, element.set_attribute(node,
namespace::Null, namespace::Null,
&Some(attr.name.clone()), attr.name.clone(),
&Some(attr.value.clone())); attr.value.clone());
} }
} }