aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/script/dom')
-rw-r--r--src/components/script/dom/attr.rs9
-rw-r--r--src/components/script/dom/element.rs18
-rw-r--r--src/components/script/dom/htmlimageelement.rs12
3 files changed, 34 insertions, 5 deletions
diff --git a/src/components/script/dom/attr.rs b/src/components/script/dom/attr.rs
index f31a281b36d..c8e5ad88527 100644
--- a/src/components/script/dom/attr.rs
+++ b/src/components/script/dom/attr.rs
@@ -24,6 +24,7 @@ pub enum AttrSettingType {
pub enum AttrValue {
StringAttrValue(DOMString),
TokenListAttrValue(DOMString, Vec<(uint, uint)>),
+ UIntAttrValue(DOMString, u32),
}
impl AttrValue {
@@ -39,10 +40,16 @@ impl AttrValue {
return TokenListAttrValue(list, indexes);
}
+ pub fn from_u32(string: DOMString, default: u32) -> AttrValue {
+ let result: u32 = from_str(string.as_slice()).unwrap_or(default);
+ UIntAttrValue(string, result)
+ }
+
pub fn as_slice<'a>(&'a self) -> &'a str {
match *self {
StringAttrValue(ref value) |
- TokenListAttrValue(ref value, _) => value.as_slice(),
+ TokenListAttrValue(ref value, _) |
+ UIntAttrValue(ref value, _) => value.as_slice(),
}
}
}
diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs
index dfc7fc13e67..551ff261b3c 100644
--- a/src/components/script/dom/element.rs
+++ b/src/components/script/dom/element.rs
@@ -5,7 +5,7 @@
//! Element nodes.
use dom::attr::{Attr, ReplacedAttr, FirstSetAttr, AttrMethods};
-use dom::attr::{AttrValue, StringAttrValue};
+use dom::attr::{AttrValue, StringAttrValue, UIntAttrValue};
use dom::attrlist::AttrList;
use dom::bindings::codegen::Bindings::ElementBinding;
use dom::bindings::codegen::InheritTypes::{ElementDerived, NodeCast};
@@ -243,6 +243,7 @@ pub trait AttributeHandlers {
fn get_string_attribute(&self, name: &str) -> DOMString;
fn set_string_attribute(&self, name: &str, value: DOMString);
fn set_tokenlist_attribute(&self, name: &str, value: DOMString);
+ fn get_uint_attribute(&self, name: &str) -> u32;
fn set_uint_attribute(&self, name: &str, value: u32);
}
@@ -388,9 +389,22 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
self.set_attribute(name, AttrValue::from_tokenlist(value));
}
+ fn get_uint_attribute(&self, name: &str) -> u32 {
+ assert!(name == name.to_ascii_lower().as_slice());
+ let attribute = self.get_attribute(Null, name).root();
+ match attribute {
+ Some(attribute) => {
+ match *attribute.deref().value() {
+ UIntAttrValue(_, value) => value,
+ _ => fail!("Expected a UIntAttrValue"),
+ }
+ }
+ None => 0,
+ }
+ }
fn set_uint_attribute(&self, name: &str, value: u32) {
assert!(name == name.to_ascii_lower().as_slice());
- self.set_attribute(name, StringAttrValue(value.to_str()));
+ self.set_attribute(name, UIntAttrValue(value.to_str(), value));
}
}
diff --git a/src/components/script/dom/htmlimageelement.rs b/src/components/script/dom/htmlimageelement.rs
index d2a0346c879..2d8f7fe52f2 100644
--- a/src/components/script/dom/htmlimageelement.rs
+++ b/src/components/script/dom/htmlimageelement.rs
@@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+use dom::attr::AttrValue;
use dom::bindings::codegen::Bindings::HTMLImageElementBinding;
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast, HTMLElementCast, HTMLImageElementDerived};
use dom::bindings::js::{JS, JSRef, Temporary};
@@ -199,7 +200,7 @@ impl<'a> HTMLImageElementMethods for JSRef<'a, HTMLImageElement> {
fn Hspace(&self) -> u32 {
let element: &JSRef<Element> = ElementCast::from_ref(self);
- from_str::<u32>(element.get_string_attribute("hspace").as_slice()).unwrap()
+ element.get_uint_attribute("hspace")
}
fn SetHspace(&self, hspace: u32) {
@@ -209,7 +210,7 @@ impl<'a> HTMLImageElementMethods for JSRef<'a, HTMLImageElement> {
fn Vspace(&self) -> u32 {
let element: &JSRef<Element> = ElementCast::from_ref(self);
- from_str::<u32>(element.get_string_attribute("vspace").as_slice()).unwrap()
+ element.get_uint_attribute("vspace")
}
fn SetVspace(&self, vspace: u32) {
@@ -269,4 +270,11 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLImageElement> {
self_alias.update_image(None, None);
}
}
+
+ fn parse_plain_attribute(&self, name: &str, value: DOMString) -> AttrValue {
+ match name {
+ "width" | "height" | "hspace" | "vspace" => AttrValue::from_u32(value, 0),
+ _ => self.super_type().unwrap().parse_plain_attribute(name, value),
+ }
+ }
}