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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/// Compute the name of the HTML attribute from the name of the builder method.
macro_rules! attr_name {
    (accept_charset) => {
        "accept-charset"
    };
    (as_) => {
        "as"
    };
    (async_) => {
        "async"
    };
    (for_) => {
        "for"
    };
    (http_equiv) => {
        "http-equiv"
    };
    (current_time) => {
        "currentTime"
    };
    (loop_) => {
        "loop"
    };
    (type_) => {
        "type"
    };
    ($attr:ident) => {
        stringify!($attr)
    };
}

/// Stamps a *string* attribute method with the provided identifier as the name,
/// optionally passing docs.
macro_rules! attr_method {
    (
        $(#[$outer:meta])*
        $publicity:vis $attr:ident(bool)
    ) => {
        $(#[$outer])*
        $publicity fn $attr(self, to_set: bool) -> Self {
            #[allow(unused)]
            use crate::interfaces::element::ElementBuilder;
            if to_set {
                self.attribute(attr_name!($attr), "")
            } else {
                self
            }
        }
    };
    (
        $(#[$outer:meta])*
        $publicity:vis $attr:ident
    ) => {
        attr_method! {
            $(#[$outer])*
            $publicity $attr(impl ToString)
        }
    };
    (
        $(#[$outer:meta])*
        $publicity:vis $attr:ident($arg:ty)
    ) => {
        $(#[$outer])*
        $publicity fn $attr(self, to_set: $arg) -> Self {
            #[allow(unused)]
            use crate::interfaces::element::ElementBuilder;
            self.attribute(attr_name!($attr), to_set.to_string())
        }
    };
}

/// Define an element type.
macro_rules! element {
    (
        $(#[$outer:meta])*
        <$name:ident>
        $(categories { $($category:ident),+ })?
        $(children {
            $(tags { $(< $child_tag:ident >),+ })?
            $(categories { $($child_category:ident),+ })?
        })?
        $(attributes {$(
            $(#[$attr_meta:meta])*
            $attr:ident $(( $attr_ty:ty ))?
        )*})?
    ) => { paste::item! {
        $(#[$outer])*
        ///
        /// A function for creating a builder which will accept attributes and produce the element.
        #[topo::nested]
        pub fn $name() -> [<$name:camel Builder>] {
            #[allow(unused)]
            use augdom::Dom;
            #[allow(unused)]
            use crate::interfaces::node::NodeWrapper;

            let elem = moxie::cache(stringify!($name), |ty| {
                $crate::prelude::document().create_element(ty)
            });
            [<$name:camel Builder>] { inner: crate::cached_node::CachedNode::new(elem) }
        }

        $(#[$outer])*
        ///
        /// A type for initializing the element's attributes before calling `build`.
        #[must_use = "needs to be built"]
        pub struct [<$name:camel Builder>] {
            inner: crate::cached_node::CachedNode,
        }

        impl crate::interfaces::element::ElementBuilder for [<$name:camel Builder>] {}
        impl crate::interfaces::node::NodeWrapper for [<$name:camel Builder>] {}

        impl $crate::interfaces::node::NodeBuilder for [<$name:camel>] {
            type Output = Self;

            /// Initialize the element with all of the attributes so far.
            fn build(self) -> Self {
                self
            }
        }

        impl $crate::interfaces::node::NodeBuilder for [<$name:camel Builder>] {
            type Output = [<$name:camel>];

            /// Initialize the element with all of the attributes so far.
            fn build(self) -> [<$name:camel>] {
                use crate::interfaces::node::sealed::Memoized;
                self.node().remove_trailing_children();

                [<$name:camel>] { inner: self.inner }
            }
        }

        impl crate::interfaces::node::sealed::Memoized for [<$name:camel Builder>] {
            fn node(&self) -> &crate::cached_node::CachedNode {
                &self.inner
            }
        }

        // children
        $(
            // child tags
            $($(
                impl crate::interfaces::node::Parent<
                    crate::elements::just_all_of_it_ok::[<$child_tag:camel>]>
                for [< $name:camel Builder >] {}
            )+)?

            // child categories
            $($(
                impl<Child> crate::interfaces::node::Parent<Child>
                for [< $name:camel Builder >]
                where Child: crate::interfaces::content_categories::[<$child_category Content>] {}
            )+)?
        )?

        // attributes
        $(impl [< $name:camel Builder >] {
            $(attr_method! {
                $(#[$attr_meta])*
                pub $attr $(($attr_ty))?
            })*
        })?

        $(#[$outer])*
        ///
        /// The initialized element, ready to be bound to a parent.
        #[must_use = "needs to be bound to a parent"]
        pub struct [<$name:camel>] {
            inner: crate::cached_node::CachedNode,
        }

        impl crate::interfaces::node::NodeWrapper for [<$name:camel>] {}
        impl crate::interfaces::node::sealed::Memoized for [<$name:camel>] {
            fn node(&self) -> &crate::cached_node::CachedNode {
                &self.inner
            }
        }
        impl crate::interfaces::element::Element for [<$name:camel>] {}

        // content categories
        $($(
            impl crate::interfaces::content_categories::[<$category Content>]
            for [< $name:camel >] {}
        )+)?
    }};
}

/// Define an HTML element type, which is essentially an `element!` with the
/// `HtmlElementBuilder` and `GlobalEventHandler` traits.
macro_rules! html_element {
    (
        $(#[$outer:meta])*
        <$name:ident>
        $($rem:tt)*
    ) => { paste::item! {
        element! {
            $(#[$outer])*
            <$name>
            $($rem)*
        }

        impl crate::interfaces::html_element::HtmlElementBuilder for [<$name:camel Builder>] {}
        impl crate::interfaces::global_events::GlobalEventHandler for [<$name:camel Builder>] {}

        impl<E> crate::interfaces::event_target::EventTarget<E> for [<$name:camel Builder>]
        where E: crate::interfaces::global_events::GlobalEvent {}
    }};
}

macro_rules! only_text_children {
    (<$name:ident>) => {
        paste::item! {
            impl crate::interfaces::node::Parent<crate::text::Text>
            for [<$name:camel Builder>] {}
        }
    };
}