https://html.spec.whatwg.org/multipage/parsing.html#tree-construction
(&mut self)
| 200 | |
| 201 | /// https://html.spec.whatwg.org/multipage/parsing.html#tree-construction |
| 202 | pub fn construct_tree(&mut self) -> Rc<RefCell<Window>> { |
| 203 | let mut token = self.t.next(); |
| 204 | |
| 205 | while token.is_some() { |
| 206 | match self.mode { |
| 207 | // https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode |
| 208 | InsertionMode::Initial => { |
| 209 | // Ignore <!doctype html>. |
| 210 | // <!doctype html> is generated as a Char token. |
| 211 | if let Some(HtmlToken::Char(_)) = token { |
| 212 | token = self.t.next(); |
| 213 | continue; |
| 214 | } |
| 215 | |
| 216 | self.mode = InsertionMode::BeforeHtml; |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | // https://html.spec.whatwg.org/multipage/parsing.html#the-before-html-insertion-mode |
| 221 | InsertionMode::BeforeHtml => { |
| 222 | match token { |
| 223 | Some(HtmlToken::Char(c)) => { |
| 224 | if c == ' ' || c == '\n' { |
| 225 | token = self.t.next(); |
| 226 | continue; |
| 227 | } |
| 228 | } |
| 229 | Some(HtmlToken::StartTag { |
| 230 | ref tag, |
| 231 | self_closing: _, |
| 232 | ref attributes, |
| 233 | }) => { |
| 234 | // A start tag whose tag name is "html" |
| 235 | // Create an element for the token in the HTML namespace, with the Document |
| 236 | // as the intended parent. Append it to the Document object. Put this |
| 237 | // element in the stack of open elements. |
| 238 | if tag == "html" { |
| 239 | self.insert_element(tag, attributes.to_vec()); |
| 240 | self.mode = InsertionMode::BeforeHead; |
| 241 | token = self.t.next(); |
| 242 | continue; |
| 243 | } |
| 244 | } |
| 245 | Some(HtmlToken::EndTag { ref tag }) => { |
| 246 | // Any other end tag |
| 247 | // Parse error. Ignore the token. |
| 248 | if tag != "head" || tag != "body" || tag != "html" || tag != "br" { |
| 249 | // Ignore the token. |
| 250 | token = self.t.next(); |
| 251 | continue; |
| 252 | } |
| 253 | } |
| 254 | Some(HtmlToken::Eof) | None => { |
| 255 | return self.window.clone(); |
| 256 | } |
| 257 | } |
| 258 | self.insert_element("html", Vec::new()); |
| 259 | self.mode = InsertionMode::BeforeHead; |