rust-fel 0.1.2

A small library for making client-side single-page apps
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use crate::element::Element;
use crate::props::Props;
use std::fmt;

#[doc(hidden)]
#[derive(Debug, Default, Clone)]
struct StackElement {
    val: String,
    arena_position: usize,
}

/// Takes a string which is formatted in an HTML-like structure.
/// Parses the string contents and builds a [rust_fel::ArenaTree](../rsx/struct.ArenaTree.html)
/// # Arguments
///
/// * `html_string` - Must have a parent wrapping html element. All text must have a wrapping element. Text and non text elements cannot be siblings.
///
/// # Examples
///```ignore
///   // <div></div> <div></div> will not work.
///   // <div><div></div></div> will work.
///   // <div> Hi <span>Hello</span></div> will not work.
///   // <div> </span>Hi</span><span>Hello</span></div> will work.
///
///   let arena_tree =
///       parse_html_to_arena_tree("<div |class=classname|><div>here is some text</div></div>".to_owned());
///       assert_eq!(arena_tree.arena[2].parent, 1);
///   let arena_tree =
///      parse_html_to_arena_tree("<div><div><span>here is some text</span></div></div>".to_owned());
///      assert_eq!(arena_tree.arena[3].parent, 2);
///```
#[doc(hidden)]
pub fn parse_html_to_arena_tree(html_string: String) -> ArenaTree {
    let mut tokens = html_string.chars().peekable();
    let mut element_type: String = String::new();
    let mut is_open_tag: bool = false;
    let mut has_text: bool = false;
    let mut text: String = String::new();
    let mut stack: Vec<StackElement> = vec![];
    let mut arena_tree: ArenaTree = ArenaTree::default();
    let mut has_attributes: bool = false;
    let mut attributes: String = String::new();

    while let Some(character) = tokens.next() {
        let string_character = character.to_string();

        if string_character == "<" {
            if has_text {
                // This pattern may need to be function-ized since we may need to repeat.
                // If the child is a text elment we insert
                // We need to let arena tree know the positions of the parent
                // In arenatree.insert we set the parent's and the children of the element being inserted
                if !stack.is_empty() {
                    arena_tree.set_current_parent_idx(stack.last().unwrap().arena_position);
                } else {
                    arena_tree.set_current_parent_idx(0);
                }
                arena_tree.insert(Node {
                    element_type: "TEXT_ELEMENT".to_owned(),
                    text: Some(text.clone()),
                    ..Default::default()
                });
            }
            if tokens.peek().unwrap().to_string() != "/" {
                is_open_tag = true;
            }
            if tokens.peek().unwrap().to_string() == "/" {
                is_open_tag = false;
                stack.pop();
            }

            has_text = false;
            text = "".to_owned();
            continue;
        }

        // Either last element of string or there will be a text child or another element
        if string_character == ">" {
            if element_type != "" {
                let next_token = tokens.peek().unwrap().to_string();

                // Here we must have text if it's not another element
                if next_token != "<" {
                    has_text = true;
                };

                // Here we insert our element type and all attributes collected
                let el = element_type.clone();
                if !stack.is_empty() {
                    arena_tree.set_current_parent_idx(stack.last().unwrap().arena_position);
                } else {
                    arena_tree.set_current_parent_idx(0);
                }

                let mut class_name = None;
                let mut href = None;
                let mut id = None;
                let mut src = None;
                let mut role = None;
                let mut type_attr = None;
                let mut data_cy = None;
                if !attributes.is_empty() {
                    let attributes_split = attributes.split(' ').filter(|s| !s.is_empty());
                    for attribute in attributes_split {
                        let attr = attribute.split('=').collect::<Vec<&str>>();
                        match attr[0] {
                            "class" => class_name = Some(attr[1].to_owned()),
                            "href" => href = Some(attr[1].to_owned()),
                            "src" => src = Some(attr[1].to_owned()),
                            "role" => role = Some(attr[1].to_owned()),
                            "type" => type_attr = Some(attr[1].to_owned()),
                            "id" => id = Some(attr[1].to_owned()),
                            "data-cy" => data_cy = Some(attr[1].to_owned()),
                            // Try Rc<RefCell>attribute handlers at the top of this function.
                            // match attribute handlers borrow_mut()
                            // "on_click" => {
                            //   match attribute_handlers {
                            //     Some(the_vec) => {
                            //       let idx = attr[1].to_owned().parse::<usize>().unwrap();
                            //       let handler = the_vec.into_iter().nth(idx).unwrap();
                            //       on_click = Some(handler);
                            //     }
                            //     None => (),
                            //   };
                            // }
                            _ => (),
                        };
                    }
                }
                arena_tree.insert(Node {
                    element_type: element_type.clone(),
                    class_name,
                    href,
                    data_cy,
                    id,
                    src,
                    role,
                    type_attr,
                    ..Default::default()
                });
                stack.push(StackElement {
                    val: el,
                    arena_position: arena_tree.arena.len() - 1,
                });
                // Reset everything so we have no data and can reade child element
                element_type = String::new();
                attributes = String::new();
                has_attributes = false;
            }
            continue;
        }

        if string_character == "|" && !has_attributes {
            has_attributes = true;
            continue;
        }
        // Down here we decide what types of variables to push to
        if is_open_tag && !has_text {
            if string_character != " " && !has_attributes {
                element_type.push_str(&string_character);
                continue;
            }

            if has_attributes && string_character != "|" {
                attributes.push_str(&string_character);
                continue;
            }
        }

        if has_text {
            text.push_str(&string_character);
            continue;
        }
    }

    if !stack.is_empty() {
        panic!("Your HTML is not formed correctly");
    }

    arena_tree
}

#[cfg(test)]
#[test]
pub fn is_parent_correct() {
    let arena_tree = parse_html_to_arena_tree(
        "<div |class=classname|><div>here is some text</div></div>".to_owned(),
    );
    assert_eq!(arena_tree.arena[2].parent, 1);
    let arena_tree =
        parse_html_to_arena_tree("<div><div>here is some text</div><span></span></div>".to_owned());
    assert_eq!(arena_tree.arena[2].parent, 1);
    let arena_tree =
        parse_html_to_arena_tree("<div><div><span>here is some text</span></div></div>".to_owned());
    assert_eq!(arena_tree.arena[3].parent, 2);
    let arena_tree = parse_html_to_arena_tree("<div>Hi there</div>".to_owned());
    assert_eq!(arena_tree.arena[1].parent, 0);
}

#[cfg(test)]
#[test]
#[should_panic(expected = "Your HTML is not formed correctly")]
pub fn is_correct_html() {
    parse_html_to_arena_tree("<div |class=classname|><div>here is some text</div>".to_owned());
    parse_html_to_arena_tree("<div><div>here is some text<div></div>".to_owned());
}

#[cfg(test)]
#[test]
pub fn is_correct_attributes() {
    let arena_tree = parse_html_to_arena_tree(
        "<div |class=classname href=https://www.google.com |><div |class=hi href=https://www.googles.com |>here is some text</div></div>"
            .to_owned(),
    );
    assert_eq!(
        arena_tree.arena[0].class_name.as_ref().unwrap(),
        &"classname".to_owned()
    );
    assert_eq!(
        arena_tree.arena[0].href.as_ref().unwrap(),
        &"https://www.google.com".to_owned()
    );
    assert_eq!(
        arena_tree.arena[1].class_name.as_ref().unwrap(),
        &"hi".to_owned()
    );
    assert_eq!(
        arena_tree.arena[1].href.as_ref().unwrap(),
        &"https://www.googles.com".to_owned()
    );
    assert_ne!(
        arena_tree.arena[1].href.as_ref().unwrap(),
        &"https://www.google.com".to_owned()
    );
    let arena_tree =
        parse_html_to_arena_tree("<script |src=https://www.google.com |></script>".to_owned());
    assert_eq!(
        arena_tree.arena[0].src.as_ref().unwrap(),
        &"https://www.google.com".to_owned()
    );

    let arena_tree = parse_html_to_arena_tree("<button | type=button role=button |><button | type=button role=button |></button></button>".to_owned());
    assert_eq!(
        arena_tree.arena[0].type_attr.as_ref().unwrap(),
        &"button".to_owned()
    );
    assert_eq!(
        arena_tree.arena[0].role.as_ref().unwrap(),
        &"button".to_owned()
    );
    assert_eq!(
        arena_tree.arena[1].type_attr.as_ref().unwrap(),
        &"button".to_owned()
    );
    assert_eq!(
        arena_tree.arena[1].role.as_ref().unwrap(),
        &"button".to_owned()
    );
    let arena_tree = parse_html_to_arena_tree("<button | data-cy=cypress role=button |><button | data-cy=cypress type=button role=button |></button></button>".to_owned());
    assert_eq!(
        arena_tree.arena[0].data_cy.as_ref().unwrap(),
        &"cypress".to_owned()
    );
    assert_eq!(
        arena_tree.arena[1].data_cy.as_ref().unwrap(),
        &"cypress".to_owned()
    );
}

/// Create a Virtual Dom of [rust_fel::Element](../rsx/struct.Element.html) from a string of [HTML](https://developer.mozilla.org/en-US/docs/Web/HTML]).
/// # Arguments
///
/// * `html_string` - Must have a parent wrapping [HTML Element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element). All text must have a wrapping [HTML Element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element). Text and non text [HTML Elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element) cannot be siblings.
/// # Examples
/// ```
/// use rust_fel::html;
/// let html = html(
///      "<div |class=classname|><span |role=button|>Span Text</span><p>Paragraph Text</p></div>"
///          .to_owned(),
///  );
///  assert_eq!(html.html_type, "div".to_owned());
///  assert_eq!(html.props.class_name.unwrap(), "classname".to_owned());
///
///  let children = html.props.children.unwrap();
///  let first_child = children.iter().nth(0);
///  let second_child = children.iter().nth(1);
///  assert_eq!(first_child.unwrap().html_type, "span");
///  assert_eq!(first_child.unwrap().props.role.as_ref().unwrap(), "button");
///  assert_eq!(second_child.unwrap().html_type, "p");
///
///  let second_childs_child = &second_child
///      .unwrap()
///      .props
///      .children
///      .iter()
///      .nth(0)
///      .unwrap()
///      .iter()
///      .nth(0)
///      .unwrap();
///  assert_eq!(second_childs_child.html_type, "TEXT_ELEMENT");
/// ```

pub fn html(html_string: String) -> Element {
    let arena_tree = parse_html_to_arena_tree(html_string);
    arena_tree.create_element_from_tree()
}

/// A structure which builds an ```arena``` ([std::vec::Vec](https://doc.rust-lang.org/std/vec/struct.Vec.html)) of [rust_fel::Node](../rsx/struct.Node.html)'s that represent a tree structure.
/// # Examples
/// ```ignore
///   arena_tree.insert(Node {
///     element_type: element_type.clone(),
///     class_name,
///     href,
///     data_cy,
///     id,
///     src,
///     role,
///     type_attr,
///     ..Default::default()
///     });
/// ```
#[doc(hidden)]
#[derive(Debug, Default)]
pub struct ArenaTree {
    current_parent_idx: usize,
    arena: Vec<Node>,
}

impl ArenaTree {
    fn set_current_parent_idx(&mut self, idx: usize) {
        self.current_parent_idx = idx;
    }

    fn insert(&mut self, mut node: Node) {
        node.parent = self.current_parent_idx;
        node.idx = self.arena.len();
        self.arena.push(node);
        let child_index = self.arena.len() - 1;
        let parent_node = &mut self.arena[self.current_parent_idx];
        if child_index > 0 {
            parent_node.add_child(child_index);
        }
    }

    fn create_element_from_tree(&self) -> Element {
        let arena = &self.arena;

        fn children(node: &Node, arena: &[Node]) -> Option<Vec<Element>> {
            Some(
                node.children
                    .iter()
                    .map(|child| create(&arena[child.to_owned()], &arena))
                    .collect::<Vec<Element>>(),
            )
        };

        fn create(node: &Node, arena: &[Node]) -> Element {
            let text = match &node.text {
                Some(x) => Some(x.to_owned()),
                None => None,
            };

            let class_name = match &node.class_name {
                Some(x) => Some(x.to_owned()),
                None => None,
            };

            let href = match &node.href {
                Some(x) => Some(x.to_owned()),
                None => None,
            };

            let data_cy = match &node.data_cy {
                Some(x) => Some(x.to_owned()),
                None => None,
            };

            let id = match &node.id {
                Some(x) => Some(x.to_owned()),
                None => None,
            };

            let src = match &node.src {
                Some(x) => Some(x.to_owned()),
                None => None,
            };

            let type_attr = match &node.type_attr {
                Some(x) => Some(x.to_owned()),
                None => None,
            };

            let role = match &node.role {
                Some(x) => Some(x.to_owned()),
                None => None,
            };

            Element {
                html_type: node.element_type.clone(),
                props: Props {
                    children: children(node, arena),
                    text,
                    class_name,
                    href,
                    data_cy,
                    id,
                    src,
                    type_attr,
                    role,
                    ..Default::default()
                },
            }
        }
        let node = &arena[0];
        create(node, arena)
    }
}
/// A ```Node``` is an intermediary representation of an HTML element.
/// A ```Node``` is constructed as a result of a html string being parsed. It will be inserted into a arena tree after initialization.
/// # Examples
/// ```ignore
///   arena_tree.insert(Node {
///     element_type: element_type.clone(),
///     class_name,
///     href,
///     data_cy,
///     id,
///     src,
///     role,
///     type_attr,
///     ..Default::default()
///     });
/// ```

#[doc(hidden)]
#[derive(Default)]
pub struct Node {
    idx: usize,
    element_type: String,
    parent: usize,
    children: Vec<usize>,
    text: Option<String>,
    id: Option<String>,
    class_name: Option<String>,
    href: Option<String>,
    src: Option<String>,
    type_attr: Option<String>,
    role: Option<String>,
    data_cy: Option<String>,
    // on_click: Option<ClosureProp>,
}

impl fmt::Debug for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{:#?}, {:#?} this is a node",
            self.element_type, self.class_name
        )
    }
}

impl Node {
    fn add_child(&mut self, child_idx: usize) {
        self.children.push(child_idx);
    }
}