Compute attribute name atoms at compile-time.

This commit is contained in:
Eli Friedman 2015-11-27 12:19:29 -08:00
parent 3720e4d5ef
commit c02c0576c8
28 changed files with 152 additions and 187 deletions

View file

@ -4,84 +4,63 @@
#[macro_export]
macro_rules! make_getter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self) -> DOMString {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
element.get_string_attribute(&Atom::from($htmlname))
element.get_string_attribute(&atom!($htmlname))
}
);
($attr:ident) => {
make_getter!($attr, to_lower!(stringify!($attr)));
}
);
#[macro_export]
macro_rules! make_bool_getter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self) -> bool {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
// FIXME(pcwalton): Do this at compile time, not runtime.
element.has_attribute(&Atom::from($htmlname))
element.has_attribute(&atom!($htmlname))
}
);
($attr:ident) => {
make_bool_getter!($attr, to_lower!(stringify!($attr)));
}
);
#[macro_export]
macro_rules! make_uint_getter(
($attr:ident, $htmlname:expr, $default:expr) => (
($attr:ident, $htmlname:tt, $default:expr) => (
fn $attr(&self) -> u32 {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
// FIXME(pcwalton): Do this at compile time, not runtime.
element.get_uint_attribute(&Atom::from($htmlname), $default)
element.get_uint_attribute(&atom!($htmlname), $default)
}
);
($attr:ident, $htmlname:expr) => {
($attr:ident, $htmlname:tt) => {
make_uint_getter!($attr, $htmlname, 0);
};
($attr:ident) => {
make_uint_getter!($attr, to_lower!(stringify!($attr)));
}
);
#[macro_export]
macro_rules! make_url_getter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self) -> DOMString {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
// FIXME(pcwalton): Do this at compile time, not runtime.
element.get_url_attribute(&Atom::from($htmlname))
element.get_url_attribute(&atom!($htmlname))
}
);
($attr:ident) => {
// FIXME(pcwalton): Do this at compile time, not runtime.
make_url_getter!($attr, to_lower!(stringify!($attr)));
}
);
#[macro_export]
macro_rules! make_url_or_base_getter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self) -> DOMString {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
let url = element.get_url_attribute(&Atom::from($htmlname));
let url = element.get_url_attribute(&atom!($htmlname));
if url.is_empty() {
let window = window_from_node(self);
DOMString::from(window.get_url().serialize())
@ -90,21 +69,17 @@ macro_rules! make_url_or_base_getter(
}
}
);
($attr:ident) => {
make_url_or_base_getter!($attr, to_lower!(stringify!($attr)));
}
);
#[macro_export]
macro_rules! make_enumerated_getter(
( $attr:ident, $htmlname:expr, $default:expr, $(($choices: pat))|+) => (
( $attr:ident, $htmlname:tt, $default:expr, $(($choices: pat))|+) => (
fn $attr(&self) -> DOMString {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use std::ascii::AsciiExt;
use string_cache::Atom;
let element = self.upcast::<Element>();
let mut val = element.get_string_attribute(&Atom::from(*$htmlname));
let mut val = element.get_string_attribute(&atom!($htmlname));
val.make_ascii_lowercase();
// https://html.spec.whatwg.org/multipage/#attr-fs-method
match &*val {
@ -113,72 +88,62 @@ macro_rules! make_enumerated_getter(
}
}
);
($attr:ident, $default:expr, $(($choices: pat))|+) => {
make_enumerated_getter!($attr, &to_lower!(stringify!($attr)), $default, $(($choices))|+);
}
);
// concat_idents! doesn't work for function name positions, so
// we have to specify both the content name and the HTML name here
#[macro_export]
macro_rules! make_setter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: DOMString) {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
// FIXME(pcwalton): Do this at compile time, not at runtime.
element.set_string_attribute(&Atom::from($htmlname), value)
element.set_string_attribute(&atom!($htmlname), value)
}
);
);
#[macro_export]
macro_rules! make_bool_setter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: bool) {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
// FIXME(pcwalton): Do this at compile time, not at runtime.
element.set_bool_attribute(&Atom::from($htmlname), value)
element.set_bool_attribute(&atom!($htmlname), value)
}
);
);
#[macro_export]
macro_rules! make_uint_setter(
($attr:ident, $htmlname:expr, $default:expr) => (
($attr:ident, $htmlname:tt, $default:expr) => (
fn $attr(&self, value: u32) {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use dom::values::UNSIGNED_LONG_MAX;
use string_cache::Atom;
let value = if value > UNSIGNED_LONG_MAX {
$default
} else {
value
};
let element = self.upcast::<Element>();
// FIXME(pcwalton): Do this at compile time, not at runtime.
element.set_uint_attribute(&Atom::from($htmlname), value)
element.set_uint_attribute(&atom!($htmlname), value)
}
);
($attr:ident, $htmlname:expr) => {
($attr:ident, $htmlname:tt) => {
make_uint_setter!($attr, $htmlname, 0);
};
);
#[macro_export]
macro_rules! make_limited_uint_setter(
($attr:ident, $htmlname:expr, $default:expr) => (
($attr:ident, $htmlname:tt, $default:expr) => (
fn $attr(&self, value: u32) -> $crate::dom::bindings::error::ErrorResult {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use dom::values::UNSIGNED_LONG_MAX;
use string_cache::Atom;
let value = if value == 0 {
return Err($crate::dom::bindings::error::Error::IndexSize);
} else if value > UNSIGNED_LONG_MAX {
@ -187,60 +152,50 @@ macro_rules! make_limited_uint_setter(
value
};
let element = self.upcast::<Element>();
// FIXME(pcwalton): Do this at compile time, not runtime.
element.set_uint_attribute(&Atom::from($htmlname), value);
element.set_uint_attribute(&atom!($htmlname), value);
Ok(())
}
);
($attr:ident, $htmlname:expr) => {
($attr:ident, $htmlname:tt) => {
make_limited_uint_setter!($attr, $htmlname, 1);
};
($attr:ident) => {
make_limited_uint_setter!($attr, to_lower!(stringify!($attr)));
};
);
#[macro_export]
macro_rules! make_atomic_setter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: DOMString) {
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
// FIXME(pcwalton): Do this at compile time, not at runtime.
element.set_atomic_attribute(&Atom::from($htmlname), value)
element.set_atomic_attribute(&atom!($htmlname), value)
}
);
);
#[macro_export]
macro_rules! make_legacy_color_setter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: DOMString) {
use dom::attr::AttrValue;
use dom::bindings::inheritance::Castable;
use dom::element::Element;
use string_cache::Atom;
let element = self.upcast::<Element>();
let value = AttrValue::from_legacy_color(value);
// FIXME(pcwalton): Do this at compile time, not at runtime.
element.set_attribute(&Atom::from($htmlname), value)
element.set_attribute(&atom!($htmlname), value)
}
);
);
#[macro_export]
macro_rules! make_dimension_setter(
( $attr:ident, $htmlname:expr ) => (
( $attr:ident, $htmlname:tt ) => (
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::from_dimension(value);
// FIXME(pcwalton): Do this at compile time, not at runtime.
element.set_attribute(&Atom::from($htmlname), value)
element.set_attribute(&atom!($htmlname), value)
}
);
);