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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//! provides functionalities and macro for building html elements
macro_rules! declare_tags {
    ( $(
         $(#[$attr:meta])*
         $name:ident;
       )*
     ) => {
        $(
            doc_comment!{
                concat!("Creates an html [",stringify!($name),"](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/",stringify!($name),") element"),

            $(#[$attr])*
            #[inline]
            #[allow(non_snake_case)]
            pub fn $name<MSG>(attrs: Vec<$crate::Attribute<MSG>>, children: Vec<$crate::Node<MSG>>) -> $crate::Node<MSG>
                {
                    $crate::html::html_element(stringify!($name), attrs, children)
                }
            }

         )*
    }
}

/// declare an element tag in a macro
/// This will create a macro which creates an element with the tag name
/// which matches the function name.
/// Example:
/// ```rust,ignore
/// div!([], [])
/// ```
/// is a macro which creates a `div` element tag
/// This macro is generated using
/// ```rust,ignore
/// declare_tags_macro!{div;}
/// ```
///
/// Note: The $ dollar sign is explcitly pass to prevent
/// rustc to attempt to expand the inner repetion of the macro
macro_rules! declare_tags_macro {
    (($d:tt) $($name: ident;)*) => {
        $(

            #[macro_export]
            /// TODO:
            macro_rules! $name {

                // 000: no trailing commas
                ( [$d($att: expr),*], [$d($children: expr),*] ) => {
                    $crate::html::$name(vec![$d($att),*], vec![$d($children),*])
                };

                ///////////////////////////////////////////////////////////////
                //
                // The next code is just the same logic as the first, it is just
                // here to deal with irregular comma placement
                //
                ///////////////////////////////////////////////////////////////

                // 001: trailing commas in params only
                ( [$d($att: expr),*], [$d($children: expr),*], ) => {
                    $crate::html::$name(vec![$d($att),*], vec![$d($children),*])
                };
                // 010: trailing commas in children only
                ( [$d($att: expr),*], [$d($children: expr,)*] ) => {
                    $crate::html::$name(vec![$d($att),*], vec![$d($children),*])
                };
                // 011: trailing commas in children and params,
                ( [$d($att: expr),*], [$d($children: expr,)*], ) => {
                    $crate::html::$name(vec![$d($att),*], vec![$d($children),*])
                };
                // 100: trailing commas in attributes only
                ( [$d($att: expr,)*], [$d($children: expr),*] ) => {
                    $crate::html::$name(vec![$d($att),*], vec![$d($children),*])
                };
                // 101: trailing commas in attributes and params,
                ( [$d($att: expr,)*], [$d($children: expr,)*], ) => {
                    $crate::html::$name(vec![$d($att),*], vec![$d($children),*])
                };
                // 110: trailing commas in attributes and children
                ( [$d($att: expr,)*], [$d($children: expr,)*] ) => {
                    $crate::html::$name(vec![$d($att),*], vec![$d($children),*])
                };
                // 111: trailing commas in attributes, children, params
                ( [$d($att: expr,)*], [$d($children: expr,)*], ) => {
                    $crate::html::$name(vec![$d($att),*], vec![$d($children),*])
                };

                /////////////////////////////////////////////////
                //
                // Pass through the expression as it was with the old function call
                //
                /////////////////////////////////////////////////

                // Pass through the div(vec![], vec![])
                ( $att: expr, $children: expr ) => {
                    $crate::html::$name( $att, $children)
                };

                // Pass through the div!(vec![], vec![],) with trailing comma
                ( $att: expr, $children: expr, ) => {
                    $crate::html::$name( $att, $children)
                };
            }
        )*
    };
}

macro_rules! declare_common_tags_and_macro {
    ($($(#[$attr:meta])* $name:ident;)*) => {

        pub(crate) mod commons {
            declare_tags! { $($name;)* }

            pub(crate) mod macros{
                // we passed `$` which is the $d:tt in declare_tags_macro
                // passing the `$` token is needed so as to prevent the called macro from unrolling
                // it.
                declare_tags_macro! {($) $($name;)* }
            }
        }

        #[cfg(feature = "with-parser")]
        /// These are the comonly used html tags such as div, input, buttons,.. etc
        pub const HTML_TAGS: [&'static str; 112] = [$(stringify!($name),)*];
    };
}

macro_rules! declare_tags_and_macro {
    ($($(#[$attr:meta])* $name:ident;)*) => {

        declare_tags! { $($name;)* }

        declare_tags_macro! {($) $($name;)* }

    };
}

macro_rules! declare_tags_non_common{

    ( $(
         $(#[$attr:meta])*
         $name:ident;
       )*
     ) => {
        declare_tags!{ $($name;)*}

        #[cfg(feature = "with-parser")]
        /// These are html tags which are non commonly used.
        /// Put together in this collection to avoid import conflicts with the commonly used
        /// ones.
        pub const HTML_TAGS_NON_COMMON:[&'static str;1] = [$(stringify!($name),)*];
    }
}

macro_rules! declare_tags_and_macro_non_common{

    ( $(
         $(#[$attr:meta])*
         $name:ident;
       )*
     ) => {
        declare_tags_and_macro!{ $($name;)*}

        #[cfg(feature = "with-parser")]
        /// These are html tags with macro which are non commonly used.
        /// Put together in this collection to avoid import conflicts with the commonly used
        /// ones.
        pub const HTML_TAGS_WITH_MACRO_NON_COMMON:[&'static str;2] = [$(stringify!($name),)*];
    }
}

// Organized in the same order as
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element
//
// Does not include obsolete elements.
declare_common_tags_and_macro! {
    base;
    head;
    link;
    meta;
    body;
    address;
    article;
    aside;
    footer;
    header;
    h1;
    h2;
    h3;
    h4;
    h5;
    h6;
    hgroup;
    main;
    nav;
    section;
    blockquote;
    dd;
    div;
    dl;
    dt;
    figcaption;
    figure;
    hr;
    html;
    li;
    ol;
    p;
    pre;
    ul;
    a;
    abbr;
    b;
    bdi;
    bdo;
    br;
    cite;
    code;
    data;
    dfn;
    em;
    i;
    kbd;
    mark;
    q;
    rb;
    rp;
    rt;
    rtc;
    ruby;
    s;
    samp;
    small;
    span;
    strong;
    sub;
    sup;
    time;
    u;
    var;
    wbr;
    area;
    audio;
    img;
    map;
    track;
    video;
    embed;
    iframe;
    object;
    param;
    picture;
    source;
    canvas;
    noscript;
    script;
    del;
    ins;
    caption;
    col;
    colgroup;
    table;
    tbody;
    td;
    tfoot;
    th;
    thead;
    tr;
    button;
    datalist;
    fieldset;
    form;
    input;
    label;
    legend;
    meter;
    optgroup;
    option;
    output;
    progress;
    select;
    textarea;
    details;
    dialog;
    menu;
    menuitem;
    summary;
    template;
}

declare_tags_non_common! {
    style;  //  conflicts with html::attributes::style, attribute::style    > tags::style
}

// These are non-common tags
// which the users need to explicitly import using
// html::tags::style, html::tags::html, etc
//
declare_tags_and_macro_non_common! {
    title; // conflicts with html::attributes::title  , attributes::title   > tags::title
    slot;  // conflicts with html::attributes::slot   , attrributes::slot   > tags::slot
}