From c961bf0779b15c223fee30df567c4861c5445dad Mon Sep 17 00:00:00 2001 From: = Date: Sun, 11 Dec 2022 10:26:21 -0600 Subject: [PATCH] improve and unify element macro --- packages/html/src/elements.rs | 1046 +++++++++++++++++---------------- 1 file changed, 539 insertions(+), 507 deletions(-) diff --git a/packages/html/src/elements.rs b/packages/html/src/elements.rs index efbb4d797..aa23d89d4 100644 --- a/packages/html/src/elements.rs +++ b/packages/html/src/elements.rs @@ -4,58 +4,124 @@ use crate::{GlobalAttributes, SvgAttributes}; pub type AttributeDiscription = (&'static str, Option<&'static str>, bool); +macro_rules! impl_attribute { + ( + $(#[$attr_method:meta])* + $fil:ident: $vil:ident (DEFAULT), + ) => { + pub const $fil: AttributeDiscription = (stringify!($fil), None, false); + }; + + ( + $(#[$attr_method:meta])* + $fil:ident: $vil:ident ($name:literal), + ) => { + pub const $fil: AttributeDiscription = ($name, None, false); + }; + + ( + $(#[$attr_method:meta])* + $fil:ident: $vil:ident (volatile), + ) => { + pub const $fil: AttributeDiscription = (stringify!($fil), None, true); + }; + + ( + $(#[$attr_method:meta])* + $fil:ident: $vil:ident (in $ns:ident), + ) => { + pub const $fil: AttributeDiscription = (stringify!($fil), Some(stringify!($ns)), false) + }; + + ( + $(#[$attr_method:meta])* + $fil:ident: $vil:ident (in $ns:ident : volatile), + ) => { + pub const $fil: AttributeDiscription = (stringify!($fil), Some(stringify!($ns)), true) + }; +} + +macro_rules! impl_element { + ( + $(#[$attr:meta])* + $name:ident None { + $( + $(#[$attr_method:meta])* + $fil:ident: $vil:ident $extra:tt, + )* + } + ) => { + #[allow(non_camel_case_types)] + $(#[$attr])* + pub struct $name; + + + impl $name { + pub const TAG_NAME: &'static str = stringify!($name); + pub const NAME_SPACE: Option<&'static str> = None; + + $( + impl_attribute!( + $(#[$attr_method])* + $fil: $vil ($extra), + ); + )* + } + + impl GlobalAttributes for $name {} + }; + + ( + $(#[$attr:meta])* + $name:ident $namespace:tt { + $( + $(#[$attr_method:meta])* + $fil:ident: $vil:ident $extra:tt, + )* + } + ) => { + #[allow(non_camel_case_types)] + $(#[$attr])* + pub struct $name; + + impl SvgAttributes for $name {} + + impl $name { + pub const TAG_NAME: &'static str = stringify!($name); + pub const NAME_SPACE: Option<&'static str> = Some($namespace); + + $( + impl_attribute!( + $(#[$attr_method])* + $fil: $vil in $namespace $extra + ); + )* + } + } +} + macro_rules! builder_constructors { ( $( $(#[$attr:meta])* - $name:ident { + $name:ident $namespace:tt { $( $(#[$attr_method:meta])* - $fil:ident: $vil:ident, + $fil:ident: $vil:ident $extra:tt, )* }; )* ) => { $( - #[allow(non_camel_case_types)] - $(#[$attr])* - pub struct $name; - - - impl $name { - pub const TAG_NAME: &'static str = stringify!($name); - pub const NAME_SPACE: Option<&'static str> = None; - - $( - pub const $fil: AttributeDiscription = (stringify!($fil), None, false); - )* - } - - impl GlobalAttributes for $name {} - )* - }; - - ( $( - $(#[$attr:meta])* - $name:ident <> $namespace:tt { - $($fil:ident: $vil:ident,)* - }; - )* ) => { - $( - #[allow(non_camel_case_types)] - $(#[$attr])* - pub struct $name; - - impl SvgAttributes for $name {} - - impl $name { - pub const TAG_NAME: &'static str = stringify!($name); - pub const NAME_SPACE: Option<&'static str> = Some($namespace); - - $( - pub const $fil: AttributeDiscription = (stringify!($fil), Some(stringify!($namespace)), false) - )* - } + impl_element!( + $(#[$attr])* + $name $namespace { + $( + $(#[$attr_method])* + $fil: $vil $extra, + )* + } + ); )* }; } @@ -75,63 +141,63 @@ builder_constructors! { /// [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) /// element. /// - base { - href: Uri, - target: Target, + base None { + href: Uri DEFAULT, + target: Target DEFAULT, }; /// Build a /// [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head) /// element. - head {}; + head None {}; /// Build a /// [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link) /// element. - link { + link None { // as: Mime, - crossorigin: CrossOrigin, - href: Uri, - hreflang: LanguageTag, - media: String, // FIXME media query - rel: LinkType, - sizes: String, // FIXME - title: String, // FIXME - r#type: Mime, - integrity: String, + crossorigin: CrossOrigin DEFAULT, + href: Uri DEFAULT, + hreflang: LanguageTag DEFAULT, + media: String DEFAULT, // FIXME media query + rel: LinkType DEFAULT, + sizes: String DEFAULT, // FIXME + title: String DEFAULT, // FIXME + r#type: Mime DEFAULT, + integrity: String DEFAULT, }; /// Build a /// [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta) /// element. - meta { - charset: String, // FIXME IANA standard names - content: String, - http_equiv: HTTPEquiv, - name: Metadata, + meta None { + charset: String DEFAULT, // FIXME IANA standard names + content: String DEFAULT, + http_equiv: HTTPEquiv DEFAULT, + name: Metadata DEFAULT, }; /// Build a /// [`