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
/// Tags, such as `div` or `table`.
///
/// To create a `Tag` `DomNode`, simply import the tag function
/// and call it with a type that implements `Into<TagProperties>`.
///
/// Example:
///
/// TODO

use {DomNode, DomNodes, DomValue, KeyValue, Listeners};
use empty::{empty, EmptyNodes, empty_listeners, EmptyListeners};

/// Properties used to create a `Tag` `DomNode`.
///
/// This is primarily used as an input (via `Into<TagProperties>`) for the various tag functions.
/// Note the large number of `From/Into` impls for this struct. Thes allows users to avoid fully
/// specifying all the fields for `TagProperties` by simply calling the tag function with the
/// appropriate combination of listeners, attributes, and children.
///
/// Note that multiple listeners or multiple children must be grouped into a single tuple.
pub struct TagProperties<
    Children: DomNodes,
    Attributes: AsRef<[KeyValue]>,
    Listens: Listeners<Message=Children::Message>>
{
    children: Children,
    key: Option<u32>,
    attributes: Attributes,
    listeners: Listens,
}

type EmptyAttrs = [KeyValue; 0];

/// Create an attributes (`Attrs`) struct from the given array of key-value pairs.
///
/// Use this function to create a tag with a given list of attributes.
///
/// Example:
///
/// ```rust
/// use domafic::DomNode;
/// use domafic::empty::empty;
/// use domafic::tags::{attributes, div};
/// use domafic::AttributeValue::Str;
///
/// let div_with_attrs = div((
///     attributes([("key", Str("value"))]),
///     // Need to manually specify message type since it cannot be inferred
///     empty::<()>()
/// ));
/// assert_eq!(div_with_attrs.get_attribute(0), Some(&("key", Str("value"))));
/// ```
pub fn attributes<A: AsRef<[KeyValue]>>(attrs: A) -> Attrs<A> {
    Attrs(attrs)
}

/// Wrapper for an array of attributes re
pub struct Attrs<A: AsRef<[KeyValue]>>(A);

// No children, attributes, or listeners
impl<M> From<()> for TagProperties<EmptyNodes<M>, EmptyAttrs, EmptyListeners<M>> {
    fn from(_props: ()) -> TagProperties<EmptyNodes<M>, EmptyAttrs, EmptyListeners<M>> {
        TagProperties {
            children: empty(),
            key: None,
            attributes: [],
            listeners: empty_listeners(),
        }
    }
}

// Just children
impl<C: DomNodes> From<C> for TagProperties<C, EmptyAttrs, EmptyListeners<C::Message>> {
    fn from(nodes: C) -> TagProperties<C, EmptyAttrs, EmptyListeners<C::Message>> {
        TagProperties {
            children: nodes,
            key: None,
            attributes: [],
            listeners: empty_listeners(),
        }
    }
}

// Just attributes
impl<M, A: AsRef<[KeyValue]>>
    From<Attrs<A>> for TagProperties<EmptyNodes<M>, A, EmptyListeners<M>>
{
    fn from(props: Attrs<A>) -> TagProperties<EmptyNodes<M>, A, EmptyListeners<M>> {
        TagProperties {
            children: empty(),
            key: None,
            attributes: props.0,
            listeners: empty_listeners(),
        }
    }
}

// Just listeners
impl<L: Listeners> From<L> for TagProperties<EmptyNodes<L::Message>, EmptyAttrs, L>
{
    fn from(props: L) -> TagProperties<EmptyNodes<L::Message>, EmptyAttrs, L> {
        TagProperties {
            children: empty(),
            key: None,
            attributes: [],
            listeners: props,
        }
    }
}

// (attributes, children)
impl<C: DomNodes, A: AsRef<[KeyValue]>>
    From<(Attrs<A>, C)> for TagProperties<C, A, EmptyListeners<C::Message>>
{
    fn from(props: (Attrs<A>, C)) -> TagProperties<C, A, EmptyListeners<C::Message>> {
        TagProperties {
            children: props.1,
            key: None,
            attributes: (props.0).0,
            listeners: empty_listeners(),
        }
    }
}

// (attributes, listeners)
impl<A: AsRef<[KeyValue]>, L: Listeners>
    From<(Attrs<A>, L)> for TagProperties<EmptyNodes<L::Message>, A, L>
{
    fn from(props: (Attrs<A>, L)) -> TagProperties<EmptyNodes<L::Message>, A, L> {
        TagProperties {
            children: empty(),
            key: None,
            attributes: (props.0).0,
            listeners: props.1,
        }
    }
}

// (listeners, attributes)
impl<A: AsRef<[KeyValue]>, L: Listeners>
    From<(L, Attrs<A>)> for TagProperties<EmptyNodes<L::Message>, A, L>
{
    fn from(props: (L, Attrs<A>)) -> TagProperties<EmptyNodes<L::Message>, A, L> {
        TagProperties {
            children: empty(),
            key: None,
            attributes: (props.1).0,
            listeners: props.0,
        }
    }
}

// (listeners, children)
impl<C: DomNodes, L: Listeners<Message=<C as DomNodes>::Message>>
    From<(L, C)> for TagProperties<C, EmptyAttrs, L>
{
    fn from(props: (L, C)) -> TagProperties<C, EmptyAttrs, L> {
        TagProperties {
            children: props.1,
            key: None,
            attributes: [],
            listeners: props.0,
        }
    }
}

// (attributes, listeners, children)
impl<C: DomNodes, A: AsRef<[KeyValue]>, L: Listeners<Message=<C as DomNodes>::Message>>
    From<(Attrs<A>, L, C)> for TagProperties<C, A, L>
{
    fn from(props: (Attrs<A>, L, C)) -> TagProperties<C, A, L> {
        TagProperties {
            children: props.2,
            key: None,
            attributes: (props.0).0,
            listeners: props.1,
        }
    }
}

// (listeners, attributes, children)
impl<C: DomNodes, A: AsRef<[KeyValue]>, L: Listeners<Message=<C as DomNodes>::Message>>
    From<(L, Attrs<A>, C)> for TagProperties<C, A, L>
{
    fn from(props: (L, Attrs<A>, C)) -> TagProperties<C, A, L> {
        TagProperties {
            children: props.2,
            key: None,
            attributes: (props.1).0,
            listeners: props.0,
        }
    }
}

/// A tag element, such as `div` or `span`.
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct Tag<
    Children: DomNodes,
    Attributes: AsRef<[KeyValue]>,
    L: Listeners<Message=Children::Message>>
{
    tagname: &'static str,
    children: Children,
    key: Option<u32>,
    attributes: Attributes,
    listeners: L,
}

impl<C: DomNodes, A: AsRef<[KeyValue]>, L: Listeners<Message=C::Message>> DomNode for Tag<C, A, L> {
    type Message = C::Message;
    type Children = C;
    type Listeners = L;
    type WithoutListeners = Tag<C, A, EmptyListeners<Self::Message>>;
    fn key(&self) -> Option<u32> { self.key }
    fn get_attribute(&self, index: usize) -> Option<&KeyValue> {
        self.attributes.as_ref().get(index)
    }
    fn children(&self) -> &Self::Children {
        &self.children
    }
    fn listeners(&self) -> &Self::Listeners {
        &self.listeners
    }
    fn children_and_listeners(&self) -> (&Self::Children, &Self::Listeners) {
        (&self.children, &self.listeners)
    }
    fn split_listeners(self) -> (Self::WithoutListeners, Self::Listeners) {
        let Tag { tagname, children, key, attributes, listeners } = self;
        (
            Tag {
                tagname: tagname,
                children: children,
                key: key,
                attributes: attributes,
                listeners: empty_listeners()
            },
            listeners
        )
    }
    fn value(&self) -> DomValue {
        DomValue::Element {
            tag: self.tagname,
        }
    }
}

#[cfg(feature = "use_std")]
use std::fmt;
#[cfg(feature = "use_std")]
impl<C, A, L> fmt::Display for Tag<C, A, L>
    where
    C: DomNodes,
    A: AsRef<[KeyValue]>,
    L: Listeners<Message=C::Message>
{
    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.displayable().fmt(formatter)
    }
}


macro_rules! impl_tags {
    ($($tagname:ident),*) => { $(
        /// Creates a tag of the given type.
        ///
        /// Note the use of `Into<TagProperties>`. This allows for a wide variety of input
        /// parameters such as `div(())`, `div(...children...)`,
        /// `div((...attributes..., ...children..))`, `div((...attributes..., ...listeners...))`
        /// and more.
        pub fn $tagname<
            C: DomNodes,
            A: AsRef<[KeyValue]>,
            L: Listeners<Message=C::Message>,
            T: Into<TagProperties<C, A, L>>
            >(properties: T)
            -> Tag<C, A, L>
        {
            let TagProperties {
                children,
                key,
                attributes,
                listeners,
            } = properties.into();

            Tag {
                tagname: stringify!($tagname),
                children: children,
                key: key,
                attributes: attributes,
                listeners: listeners,
            }
        }
    )* }
}

impl_tags!(
    a, abbr, acronym, address, applet, area, article, aside, audio, b, base, basefont, bdi,
    bdo, big, blockquote, body, br, button, canvas, caption, center, cite, code, col, colgroup,
    datalist, dd, del, details, dfn, dialog, dir, div, dl, dt, em, embed, fieldset,
    figcaption, figure, font, footer, form, frame, framset, h1, h2, h3, h4, h5, h6, head,
    header, hr, i, iframe, img, input, ins, kbd, keygen, label, legend, li, link, main, map,
    mark, menu, menuitem, meta, meter, nav, noframes, noscript, object, ol, optgroup, option,
    output, p, param, pre, progress, q, rp, rt, ruby, s, samp, script, section, select, small,
    source, span, strike, strong, style, sub, summary, sup, table, tbody, td, textarea, tfoot,
    th, thead, time, title, tr, track, tt, u, ul, var, video, wbr
);