diff options
Diffstat (limited to 'components/script/dom/macros.rs')
-rw-r--r-- | components/script/dom/macros.rs | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/components/script/dom/macros.rs b/components/script/dom/macros.rs index 76157789e71..1eff2cd9e31 100644 --- a/components/script/dom/macros.rs +++ b/components/script/dom/macros.rs @@ -39,7 +39,7 @@ macro_rules! make_bool_getter( #[macro_export] macro_rules! make_uint_getter( - ( $attr:ident, $htmlname:expr ) => ( + ($attr:ident, $htmlname:expr, $default:expr) => ( fn $attr(self) -> u32 { use dom::element::{Element, AttributeHandlers}; use dom::bindings::codegen::InheritTypes::ElementCast; @@ -47,9 +47,12 @@ macro_rules! make_uint_getter( use std::ascii::AsciiExt; let element: JSRef<Element> = ElementCast::from_ref(self); // FIXME(pcwalton): Do this at compile time, not runtime. - element.get_uint_attribute(&Atom::from_slice($htmlname)) + element.get_uint_attribute(&Atom::from_slice($htmlname), $default) } ); + ($attr:ident, $htmlname:expr) => { + make_uint_getter!($attr, $htmlname, 0); + }; ($attr:ident) => { make_uint_getter!($attr, to_lower!(stringify!($attr))); } @@ -153,15 +156,51 @@ macro_rules! make_bool_setter( #[macro_export] macro_rules! make_uint_setter( - ( $attr:ident, $htmlname:expr ) => ( + ($attr:ident, $htmlname:expr, $default:expr) => ( fn $attr(self, value: u32) { use dom::element::{Element, AttributeHandlers}; use dom::bindings::codegen::InheritTypes::ElementCast; + let value = if value > 2147483647 { + $default + } else { + value + }; let element: JSRef<Element> = ElementCast::from_ref(self); // FIXME(pcwalton): Do this at compile time, not at runtime. element.set_uint_attribute(&Atom::from_slice($htmlname), value) } ); + ($attr:ident, $htmlname:expr) => { + make_uint_setter!($attr, $htmlname, 0); + }; +); + +#[macro_export] +macro_rules! make_limited_uint_setter( + ($attr:ident, $htmlname:expr, $default:expr) => ( + fn $attr(self, value: u32) -> $crate::dom::bindings::error::ErrorResult { + use dom::element::AttributeHandlers; + use dom::bindings::codegen::InheritTypes::ElementCast; + use string_cache::Atom; + let value = if value == 0 { + return Err($crate::dom::bindings::error::Error::IndexSize); + } else if value > 2147483647 { + $default + } else { + value + }; + let element = ElementCast::from_ref(self); + // FIXME(pcwalton): Do this at compile time, not runtime. + element.set_uint_attribute(&Atom::from_slice($htmlname), value); + Ok(()) + } + ); + ($attr:ident, $htmlname:expr) => { + make_limited_uint_setter!($attr, $htmlname, 1); + }; + ($attr:ident) => { + make_limited_uint_setter!($attr, to_lower!(stringify!($attr))); + }; ); /// For use on non-jsmanaged types |