1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![allow(clippy::module_name_repetitions)]
#![allow(non_snake_case)]

pub mod elements;
#[doc(hidden)]
pub use paste::paste;

use crate::{Element, Node};

/// A typed HTML element.
pub trait TypedElement: Default {
    /// The attributes of the element.
    type Attributes: TypedAttributes;

    /// Create an element from its attributes.
    fn from_attributes(
        attributes: Self::Attributes,
        other_attributes: Vec<(String, Option<String>)>,
    ) -> Self;

    /// Convert the typed element into an [`Element`].
    fn into_element(self, children: Option<Vec<Node>>) -> Element;

    /// Convert the typed element into a [`Node`].
    ///
    /// By default, this is equivalent to calling [`TypedElement::into_element`]
    /// and then just wrapping it in a [`Node::Element`].
    fn into_node(self, children: Option<Vec<Node>>) -> Node {
        Node::Element(self.into_element(children))
    }
}

/// A typed set of HTML attributes.
pub trait TypedAttributes: Default {
    /// Convert the typed attributes into a set of attributes.
    fn into_attributes(self) -> Vec<(String, Option<String>)>;
}

#[allow(missing_docs)]
#[macro_export]
macro_rules! typed_elements {
    ($vis:vis $($ElementName:ident $(($name:literal))? $([$AttributeName:ident])? $({ $($attribute:ident),* $(,)? })?;)*) => {
        $(
            $crate::typed_element!{
                $vis $ElementName $(($name))? $([$AttributeName])? $({ $($attribute),* })?
            }
        )*
    };
}

#[allow(missing_docs)]
#[macro_export]
macro_rules! typed_element {
    ($vis:vis $ElementName:ident $(($name:literal))? $([$AttributeName:ident])? $({ $($attribute:ident $(: $atype:ty)?),* $(,)? })?) => {
        $crate::typed_attributes!{
            ($vis $ElementName) $([$vis $AttributeName])? $({
                accesskey,
                autocapitalize,
                autofocus,
                class,
                contenteditable,
                dir,
                draggable,
                enterkeyhint,
                exportparts,
                hidden,
                id,
                inert,
                inputmode,
                is,
                itemid,
                itemprop,
                itemref,
                itemscope,
                itemtype,
                lang,
                nonce,
                part,
                popover,
                role,
                slot,
                spellcheck,
                style,
                tabindex,
                title,
                translate,
                virtualkeyboardpolicy,
                $($attribute $(: $atype)?),*
            })?
        }

        #[derive(::std::fmt::Debug, ::std::clone::Clone, ::std::default::Default)]
        #[allow(non_camel_case_types)]
        #[allow(missing_docs)]
        $vis struct $ElementName {
            $vis attributes: <Self as $crate::typed::TypedElement>::Attributes,
            $vis other_attributes: ::std::vec::Vec<(::std::string::String, ::std::option::Option<::std::string::String>)>,
        }

        impl $crate::typed::TypedElement for $ElementName {
            type Attributes = $crate::typed_attributes!(@NAME ($ElementName) $([$AttributeName])?);

            fn from_attributes(
                attributes: Self::Attributes,
                other_attributes: ::std::vec::Vec<(::std::string::String, ::std::option::Option<::std::string::String>)>,
            ) -> Self {
                Self { attributes, other_attributes }
            }

            fn into_element(mut self, children: ::std::option::Option<::std::vec::Vec<$crate::Node>>) -> $crate::Element {
                let mut attributes = $crate::typed::TypedAttributes::into_attributes(self.attributes);
                attributes.append(&mut self.other_attributes);

                $crate::Element {
                    name: ::std::convert::From::from($crate::typed_element!(@NAME_STR $ElementName$(($name))?)),
                    attributes,
                    children,
                }
            }
        }
    };
    (@NAME_STR $ElementName:ident) => {
        stringify!($ElementName)
    };
    (@NAME_STR $ElementName:ident($name:literal)) => {
        $name
    };
}

#[allow(missing_docs)]
#[macro_export]
macro_rules! typed_attributes {
    {
        $(($vise:vis $ElementName:ident))? $([$visa:vis $AttributeName:ident])? {
            $($attribute:ident $(: $atype:ty)?),* $(,)?
        }
    } => {
        $crate::typed_attributes!(@STRUCT $(($vise $ElementName))? $([$visa $AttributeName])? { $($attribute $(: $atype)?),* });

        impl $crate::typed::TypedAttributes for $crate::typed_attributes!(@NAME $(($ElementName))? $([$AttributeName])?) {
            fn into_attributes(self) -> ::std::vec::Vec<(::std::string::String, ::std::option::Option<::std::string::String>)> {
                [$((::std::stringify!($attribute), self.$attribute.map(|opt| opt.map(|a| ::std::string::ToString::to_string(&a))))),*]
                    .into_iter()
                    .flat_map(|(key, maybe_value)| {
                        maybe_value.map(|value| (key.strip_prefix("r#").unwrap_or(key).replace('_', "-"), value))
                    })
                    .collect()
            }
        }
    };
    (($_vise:vis $_ElementName:ident) $([$_visa:vis $_AttributeName:ident])?) => {};
    (@NAME ($ElementName:ident)) => {
        $crate::typed::paste!([< $ElementName:camel Attributes >])
    };
    (@NAME $(($ElementName:ident))? [$AttributeName:ident]) => {
        $AttributeName
    };
    {
        @STRUCT ($vis:vis $ElementName:ident) {
            $($attribute:ident $(:$atype:ty)?),* $(,)?
        }
    } => {
        $crate::typed::paste! {
            #[derive(::std::fmt::Debug, ::std::clone::Clone, ::std::default::Default)]
            #[allow(missing_docs)]
            $vis struct [< $ElementName:camel Attributes >] {
                $($vis $attribute: ::std::option::Option<::std::option::Option<$crate::typed_attributes!(@ATTR_TYPE $($atype)?)>>,)*
            }
        }
    };
    {
        @STRUCT $(($_vis:vis $ElementName:ident))? [$vis:vis $AttributeName:ident] {
            $($attribute:ident $(: $atype:ty)?),* $(,)?
        }
    } => {
        #[derive(::std::fmt::Debug, ::std::clone::Clone, ::std::default::Default)]
        #[allow(missing_docs)]
        $vis struct $AttributeName {
            $($vis $attribute: ::std::option::Option<::std::option::Option<$crate::typed_attributes!(@ATTR_TYPE $($atype)?)>>,)*
        }
    };
    (@ATTR_TYPE $atype:ty) => {$atype};
    (@ATTR_TYPE) => {::std::string::String};
}