Implement parsed 'unsigned long' attributes.

This commit is partially based on earlier work by Bruno Abinader in #2073.
This commit is contained in:
Ms2ger 2014-06-08 17:05:38 +02:00
parent 972c69883e
commit b012c99e05
3 changed files with 34 additions and 5 deletions

View file

@ -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(),
}
}
}

View file

@ -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));
}
}

View file

@ -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),
}
}
}