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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//! This file exports helper macros for element creation, populated by a higher-level macro,
//! and macros for creating the parts of elements. (attrs, style, events)

/// Copied from [https://github.com/rust-lang/rust/issues/35853][https://github.com/rust-lang/rust/issues/35853]
macro_rules! with_dollar_sign {
    ($($body:tt)*) => {
        macro_rules! __with_dollar_sign { $($body)* }
        __with_dollar_sign!($);
    }
}

/// Create macros exposed to the package that allow shortcuts for Dom elements.
/// In the matching mattern below, we match the name we want to use with the name under
/// the seed::dom_types::Tag enum. Eg the div! macro uses seed::dom_types::Tag::Div.
macro_rules! element {
    // Create shortcut macros for any element; populate these functions in this module.
    ($($Tag:ident => $Tag_camel:ident);+) => {
        // This replaces $d with $ in the inner macro.
        with_dollar_sign! {
            ($d:tt) => {
                $(
                    #[macro_export]
                    macro_rules! $Tag {
                        ( $d($d part:expr),* $d(,)* ) => {
                            {
                                let mut el = El::empty(seed::dom_types::Tag::$Tag_camel);
                                $d ( $d part.update(&mut el); )*
                                el
                            }
                        };
                    }
                )+
            }
        }
   }
}
/// Similar to the element! macro above, but with a namespace for svg.
macro_rules! element_svg {
    // Create shortcut macros for any element; populate these functions in this module.
    ($($Tag:ident => $Tag_camel:ident);+) => {
        // This replaces $d with $ in the inner macro.
        with_dollar_sign! {
            ($d:tt) => {
                $(
                    #[macro_export]
                    macro_rules! $Tag {
                        ( $d($d part:expr),* $d(,)* ) => {
                            {
                                let mut el = El::empty_svg(seed::dom_types::Tag::$Tag_camel);
                                $d ( $d part.update(&mut el); )*
                                el
                            }
                        };
                    }
                )+
            }
        }
   }
}

/// El must be exposed in the module where this is called for these to work.
element! {
    address => Address; article => Article; aside => Aside; footer => Footer;
    header => Header; h1 => H1;
    h2 => H2; h3 => H3; h4 => H4; h5 => H5; h6 => H6;
    hgroup => Hgroup; main => Main; nav => Nav; section => Section;

    blockquote => BlockQuote;
    dd => Dd; dir => Dir; div => Div; dl => Dl; dt => Dt; figcaption => FigCaption; figure => Figure;
    hr => Hr; li => Li; ol => Ol; p => P; pre => Pre; ul => Ul;

    a => A; abbr => Abbr;
    b => B; bdi => Bdi; bdo => Bdo; br => Br; cite => Cite; code => Code; data => Data;
    dfn => Dfn; em => Em; i => I; kbd => Kbd; mark => Mark; q => Q; rb => Rb;
    rp => Rp; rt => Rt; rtc => Rtc; ruby => Ruby; s => S; samp => Samp; small => Small;
    span => Span; strong => Strong; sub => Sub; sup => Sup; time => Time; tt => Tt;
    u => U; var => Var; wbr => Wbr;

    area => Area; audio => Audio; img => Img; map => Map; track => Track; video => Video;

    applet => Applet; embed => Embed; iframe => Iframe;
    noembed => NoEmbed; object => Object; param => Param; picture => Picture; source => Source;

    canvas => Canvas; noscript => NoScript; Script => Script;

    del => Del; ins => Ins;

    caption => Caption; col => Col; colgroup => ColGroup; table => Table; tbody => Tbody;
    td => Td; tfoot => Tfoot; th => Th; thead => Thead; tr => Tr;

    button => Button; datalist => DataList; fieldset => FieldSet; form => Form; input => Input;
    label => Label; legend => Legend; meter => Meter; optgroup => OptGroup; option => Option;
    output => Output; progress => Progress; select => Select; textarea => TextArea;

    details => Details; dialog => Dialog; menu => Menu; menuitem => MenuItem; summary => Summary;

    content => Content; element => Element; shadow => Shadow; slot => Slot; template => Template
}

element_svg! {
    // SVG shape elements
    line_ => Line;
    rect => Rect; circle => Circle; ellipse => Elipse; polygon => Polygon; polyline => Polyline;
    mesh => Mesh; path => Path; defs => Defs; marker => Marker; mask => Mask;
    // SVG container elements
    svg => Svg; g => G;
    // SVG gradient elements
    linear_gradient => LinearGradient; radial_gradient => RadialGradient; mesh_gradient => MeshGradient;
    stop => Stop;
    // SVG gradphics elements
    image => Image;
    // SVG graphics referencing elements
    r#use => Use;
    // SVG text content elements
    text => Text; tref => TRef; tspan => TSpan
}

// TODO:
// Create the following svg macros
// - missing-glyph
// - pattern
// - switch
// - symbol
// - unknown

// - add SVG animation elements
// - add SVG filter primitive elements
// - add SVG descriptive elements
// - add SVG font elements
// - add SVG paint server elements
// - altGlyph
// - altGlyphDef
// - altGlyphItem
// - glyph
// - glyphRef
// - textPath

// Add the following SVG element macros:
//  - clipPath
//  - color-profile
//  - cursor
//  - filter
//  - foreignObject
//  - hatchpath
//  - meshpatch
//  - meshrow
//  - style
//  - view

#[macro_export]
macro_rules! custom {
    ( $($part:expr),* $(,)* ) => {
        {
            let mut el = El::empty(seed::dom_types::Tag::Custom("missingtagname".into()));
            $ ( $part.update(&mut el); )*
            el
        }
    };
}

/// Provide a shortcut for creating attributes.
#[macro_export]
macro_rules! attrs {
    { $($key:expr => $value:expr);* $(;)* } => {
        {
            let mut vals = std::collections::HashMap::new();
            $(
                // We can handle arguments of multiple types by using this:
                // Strings, &strs, bools, numbers etc.
                vals.insert($key.into(), $value.to_string());
            )*
            seed::dom_types::Attrs::new(vals)
        }
     };
}

/// Convenience macro. Ideal when there are multiple classes, and no other attrs.
#[macro_export]
macro_rules! class {
    { $($class:expr),* $(,)* } => {
        {
            let mut result = seed::dom_types::Attrs::empty();
            let mut classes = Vec::new();
            $(
                classes.push($class);
            )*
            result.add_multiple("class".into(), classes);
            result
        }
     };
}

/// Convenience macro, for brevity.
#[macro_export]
macro_rules! id {
    { $id:expr } => {
        {
            seed::dom_types::Attrs::from_id($id)
        }
     };
}

// todo: Once the macro_at_most_once_rep is in stable, you can use $(;)? here (
// todo: and in el creation macros) to make only trailing comma/semicolon acceptable.
/// Provide a shortcut for creating styles.
#[macro_export]
macro_rules! style {
    { $($key:expr => $value:expr);* $(;)* } => {
        {
            let mut vals = std::collections::HashMap::new();
            $(
                // We can handle arguments of multiple types by using this:
                // Strings, &strs, bools, numbers etc.
                vals.insert(String::from($key), $value.to_string());
            )*
            seed::dom_types::Style::new(vals)
        }
     };
}

//#[macro_export]
//macro_rules! events {
//    { $($event_str:expr => $handler:expr);+ } => {
//        {
//            let mut result = Vec::new();
//            $(
//                match $event_str {
////                    _ => result.push(seed::dom_types::Listener::new_input($event_str.into(), $handler)),
//
//
//                    _ => result.push(seed::dom_types::Listener::new_input($event_str.into(), $handler)),
////
////
////
////  "input" => result.push(seed::dom_types::Listener::new_input($event_str.into(), $handler)),
////                    _ => result.push(seed::dom_types::Listener::new($event_str.into(), $handler)),
//
//                }
//
////                result.push(seed::dom_types::Listener::new($event_str.into(), $handler));
//            )+
//            result
//        }
//     };
//}
//

///// Attempt to apply the correct input type based on its trigger.
//#[macro_export]
//macro_rules! ev2 {
//    { $event_str:expr, $handler:expr } => {
//        {
//            match event_str {
//                "input" => seed::dom_types::input_ev($event_str, $handler),
////                "change" => seed::dom_types::input_ev($event_str, $handler),
////
////                "keydown" => seed::dom_types::keyboard_ev($event_str, $handler),
////
////                "click" => seed::dom_types::ev($event_str, $handler),
////                "auxclick" => seed::dom_types::ev($event_str, $handler),
////                "dblclick" => seed::dom_types::ev($event_str, $handler),
////                "contextmenu" => seed::dom_types::input_ev($event_str, $handler),
//
//                _ => seed::dom_types::raw_ev($event_str, $handler),
//
//            }
//        }
//    };
//}

///// Attempt to apply the correct input type based on its trigger.
//#[macro_export]
//macro_rules! ev2 {
//    { input, $handler:expr } => {
//        {
//            seed::dom_types::input_ev("input", $handler),
//        }
//    };
//    { click, $handler:expr } => {
//        {
//            seed::dom_types::simple_ev("click", $handler),
//        }
//    };
//
//
//
//}

/// A convenience function for logging to the web browser's console.  We use
/// a macro to supplement the log function to allow multiple inputs.
#[macro_export]
macro_rules! log {
    { $($expr:expr),* $(,)* } => {
        {
            let mut text = String::new();
            $(
                text += &format!("{:#?}", $expr);
                text += " ";
            )*
            web_sys::console::log_1(&text.into());
        }
     };
}

/// Similar to log!
#[macro_export]
macro_rules! error {
    { $($expr:expr),* $(,)* } => {
        {
            let mut text = String::new();
            $(
                text += &format!("{:#?}", $expr);
                text += " ";
            )*
            web_sys::console::error_1(&text.into());
        }
     };
}

/// A HashMap literal, where the keys and valsmust implement ToString.
#[macro_export]
macro_rules! hashmap_string {
    { $($key:expr => $value:expr),* $(,)* } => {
        {
            let mut result = std::collections::HashMap::new();
            $(
                // We can handle arguments of multiple types by using this:
                // Strings, &strs, bools, numbers etc.
                result.insert($key.to_string(), $value.to_string());
            )*
            result
        }
     };
}