diff options
author | Corey Farwell <coreyf@rwell.org> | 2015-11-09 18:17:14 -0500 |
---|---|---|
committer | Corey Farwell <coreyf@rwell.org> | 2015-11-09 20:32:56 -0500 |
commit | 73314ab10ce27e2f0860520dadeee2ce6eb88f3c (patch) | |
tree | d6e98f87d527ec06a988f73b705efc4f4a7f540d /components/script/dom | |
parent | f39faaf99471f5419710e1a3d434e5d5fab0d221 (diff) | |
download | servo-73314ab10ce27e2f0860520dadeee2ce6eb88f3c.tar.gz servo-73314ab10ce27e2f0860520dadeee2ce6eb88f3c.zip |
Add Dimension member to AttrValue
Fixes #8417
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/attr.rs | 23 | ||||
-rw-r--r-- | components/script/dom/macros.rs | 15 |
2 files changed, 36 insertions, 2 deletions
diff --git a/components/script/dom/attr.rs b/components/script/dom/attr.rs index 856fb90aa5b..315f26a3b7c 100644 --- a/components/script/dom/attr.rs +++ b/components/script/dom/attr.rs @@ -21,7 +21,7 @@ use std::mem; use std::ops::Deref; use string_cache::{Atom, Namespace}; use style::values::specified::Length; -use util::str::{DOMString, parse_unsigned_integer, parse_legacy_color}; +use util::str::{DOMString, LengthOrPercentageOrAuto, parse_unsigned_integer, parse_legacy_color, parse_length}; use util::str::{split_html_space_chars, str_join}; #[derive(JSTraceable, PartialEq, Clone, HeapSizeOf)] @@ -32,6 +32,7 @@ pub enum AttrValue { Atom(Atom), Length(DOMString, Option<Length>), Color(DOMString, Option<RGBA>), + Dimension(DOMString, LengthOrPercentageOrAuto), } impl AttrValue { @@ -83,6 +84,11 @@ impl AttrValue { AttrValue::Color(string, parsed) } + pub fn from_dimension(string: DOMString) -> AttrValue { + let parsed = parse_length(&string); + AttrValue::Dimension(string, parsed) + } + /// Assumes the `AttrValue` is a `TokenList` and returns its tokens /// /// ## Panics @@ -131,6 +137,18 @@ impl AttrValue { } } + /// Assumes the `AttrValue` is a `Dimension` and returns its value + /// + /// ## Panics + /// + /// Panics if the `AttrValue` is not a `Dimension` + pub fn as_dimension(&self) -> &LengthOrPercentageOrAuto { + match *self { + AttrValue::Dimension(_, ref l) => l, + _ => panic!("Dimension not found"), + } + } + /// Return the AttrValue as its integer representation, if any. /// This corresponds to attribute values returned as `AttrValue::UInt(_)` /// by `VirtualMethods::parse_plain_attribute()`. @@ -156,7 +174,8 @@ impl Deref for AttrValue { AttrValue::TokenList(ref value, _) | AttrValue::UInt(ref value, _) | AttrValue::Length(ref value, _) | - AttrValue::Color(ref value, _) => &value, + AttrValue::Color(ref value, _) | + AttrValue::Dimension(ref value, _) => &value, AttrValue::Atom(ref value) => &value, } } diff --git a/components/script/dom/macros.rs b/components/script/dom/macros.rs index d230bec5404..007abac569d 100644 --- a/components/script/dom/macros.rs +++ b/components/script/dom/macros.rs @@ -230,6 +230,21 @@ macro_rules! make_legacy_color_setter( ); ); +#[macro_export] +macro_rules! make_dimension_setter( + ( $attr:ident, $htmlname:expr ) => ( + fn $attr(&self, value: DOMString) { + use dom::bindings::inheritance::Castable; + use dom::element::Element; + use string_cache::Atom; + let element = self.upcast::<Element>(); + let value = AttrValue::parse_dimension(value); + // FIXME(pcwalton): Do this at compile time, not at runtime. + element.set_attribute(&Atom::from_slice($htmlname), value) + } + ); +); + /// For use on non-jsmanaged types /// Use #[derive(JSTraceable)] on JS managed types macro_rules! no_jsmanaged_fields( |