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
use super::*;

/// The Page struct.
/// Stores a list of nested HTML elements.
pub struct Page {
   pub children: Vec<El>,
}

impl Page {
   /// Returns a new Page. Takes a slice of El as input.
   pub fn new(children: &[El]) -> Self {
      Page {
         children: children.to_vec(),
      }
   }
   
   /// Pushes an El onto children field vec.
   pub fn add(mut self, child: El) -> Self {
      self.children.push(child);
      self
   }
   
   /// Allows for finding a child element by its id attribute.
   pub fn id_find(&self, id: &str) -> Option<&El> {
      
      for child in self.children.iter() {
         
         let find = child.id_find(id);
         
         match find {
            Some(_) => { return find; },
            None => (),
         }
      }
      
      None
   }
   
   /// Formats the Page for display or storage. Automatically
   /// prepends '<!DOCTYPE HTML> to the beginning of the file.
   pub fn format(&self, make_pretty: bool) -> String {
      
      let mut f = Formatter {
         buf: String::new(),
      };
      
      f.write("<!DOCTYPE HTML>");
      
      for child in self.children.iter() {
         f = child.format(f, 0, make_pretty);
      }
      
      f.buf
      
   }
}

#[cfg(test)]
mod tests {
   
   use super::*;
   
   #[test]
   fn add() {
      
      let html = El::paired(Tag::Html, &[]);
      
      let page = Page::new(&[html]);
      
      assert!(page.children.len() == 1);
   }
   
   // #[test]
   // fn id_find_some() {
      
   //    let html = El::paired(Tag::Html, &[
         
   //       El::paired(Tag::Div, &[])
   //       .attributes(&[(Attr::Id, "div")]),
         
   //    ]).attributes(&[(Attr::Id, "html")]);
      
   //    let page = Page::new(&[html]);
      
   //    if let None = page.id_find("div") {
   //       panic!();
   //    }
   // }
   
   // #[test]
   // fn id_find_depth_4() {
      
   //    let html = El::paired(Tag::Html, &[
   //       El::paired(Tag::Div, &[
   //          El::paired(Tag::Div, &[
   //             El::paired(Tag::Div, &[])
   //                .attributes(&[
   //                   (Attr::Id, "div"),
   //                   (Attr::Name, "xD")
   //                ]),
   //          ]),
   //          El::paired(Tag::Div, &[])
   //             .attributes(&[
   //                (Attr::Id, "div"),
   //                (Attr::Name, "xP")
   //             ]),
   //       ]),
   //    ])
   //    .attributes(&[(Attr::Id, "html")]);
      
   //    let page = Page::new(&[html]);
      
   //    assert_eq!(page.id_find("div").unwrap().attributes[1].value, "xD");
   // }
   
   // #[test]
   // #[should_panic]
   // fn id_find_none() {
      
   //    let html = El::paired(Tag::Html, &[
         
   //       El::paired(Tag::Div, &[])
   //       .attributes(&[(Attr::Id, "div")]),
         
   //    ])
   //       .attributes(&[
   //          (Attr::Id, "html"),
   //       ]);
      
   //    let page = Page::new(&[html]);
      
   //    if let None = page.id_find("htmk") {
   //       panic!();
   //    }
   // }
   
   #[test]
   fn format_bare_tags() {
      
      use Tag::*;
      
      let page = Page::new(&[
         El::paired(Html, &[
            El::paired(Head, &[
               El::paired(Style, &[]),
            ]),
            El::paired(Body, &[]),
         ])
      ]).format(false);
      
      assert_eq!(page, "<!DOCTYPE HTML><html><head><style></style></head><body></body></html>");
   }
   
   #[test]
   fn format_bare_tags_pretty() {
      
      use Tag::*;
      
      let page = Page::new(&[
         El::paired(Html, &[
            El::paired(Head, &[
               El::paired(Style, &[]),
            ]),
            El::paired(Body, &[]),
         ])
      ]).format(true);
      
      assert_eq!(page, "<!DOCTYPE HTML>\n<html>\n   <head>\n      <style></style>\n   </head>\n   <body></body>\n</html>");
   }
}

impl std::fmt::Display for Page {
   fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
      
      write!(f, "{}", self.format(true))?;
      
      Ok(())
      
   }
}