Skip to main content

paige/element/
mod.rs

1mod attributes;
2pub use attributes::*;
3
4mod properties;
5pub use properties::*;
6
7mod tags;
8pub use tags::*;
9
10/// An HTML element.
11#[derive(Clone)]
12pub struct El {
13   pub name: String,
14   pub is_text: bool,
15   pub paired: bool,
16   pub attributes: Vec<Attr>,
17   pub style: Vec<Prop>,
18   pub content: Vec<El>,
19}
20
21impl El {
22   
23   /// Creates a new element that displays its name as text and nothing else
24   /// when formatted. This is useful for making innerHTML something other
25   /// than a tag, such as inside the <style> or <script> tags.
26   pub fn text<N: Into<String>>(text: N) -> Self {
27      El {
28         is_text: true,
29         paired: false,
30         name: text.into(),
31         attributes: vec![],
32         style: vec![],
33         content: vec![],
34      }
35   }
36   
37   /// Takes a slice of Attr as input and pushes each member onto .attributes vec.
38   pub fn attributes(mut self, values: &[Attr]) -> Self {
39      for val in values {
40         self.attributes.push(val.clone());
41      }
42      self
43   }
44   
45   /// Takes a slice of Prop as input and pushes each member onto .style vec.
46   pub fn style(mut self, values: &[Prop]) -> Self {
47      for val in values {
48         self.style.push(val.clone());
49      }
50      self
51   }
52   
53   /// Takes a slice of El and pushes each element onto .content.
54   pub fn content(mut self, values: &[El]) -> Self {
55      for val in values {
56         self.content.push(val.clone());
57      }
58      self
59   }
60   
61   /// Allows for finding a child element by its id attribute.
62   pub fn id_find(&mut self, id: &str) -> Option<&mut El> {
63      
64      for attr in self.attributes.iter() {
65         if let Attr::Id(val) = attr {
66            if *val == *id {
67               return Some(self);
68            }
69         }
70      }
71      
72      if !self.is_text && self.paired {
73         
74         for child in self.content.iter_mut() {
75            
76            let find = child.id_find(id);
77            
78            match find {
79               Some(_) => { return find; },
80               None => (),
81            }
82         }
83      }
84      
85      None
86   }
87   
88}