// Copyright 2014-2017 The html5ever Project Developers. See the
// COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 or the MIT license
// , at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// The tree builder rules, as a single, enormous nested match expression.
use crate::interface::Quirks;
use crate::tokenizer::states::{Rawtext, Rcdata, ScriptData};
use crate::tokenizer::TagKind::{EndTag, StartTag};
use crate::tree_builder::tag_sets::*;
use crate::tree_builder::types::*;
use crate::tree_builder::{
create_element, html_elem, ElemName, NodeOrText::AppendNode, StrTendril, Tag, TreeBuilder,
TreeSink,
};
use crate::QualName;
use markup5ever::{expanded_name, local_name, ns};
use std::borrow::Cow::Borrowed;
use crate::tendril::SliceExt;
use match_token::match_token;
fn any_not_whitespace(x: &StrTendril) -> bool {
// FIXME: this might be much faster as a byte scan
x.chars().any(|c| !c.is_ascii_whitespace())
}
fn current_node(open_elems: &[Handle]) -> &Handle {
open_elems.last().expect("no current element")
}
#[doc(hidden)]
impl TreeBuilder
where
Handle: Clone,
Sink: TreeSink,
{
pub(crate) fn step(&self, mode: InsertionMode, token: Token) -> ProcessResult {
self.debug_step(mode, &token);
match mode {
//§ the-initial-insertion-mode
InsertionMode::Initial => match_token!(token {
Token::Characters(SplitStatus::NotSplit, text) => ProcessResult::SplitWhitespace(text),
Token::Characters(SplitStatus::Whitespace, _) => ProcessResult::Done,
Token::Comment(text) => self.append_comment_to_doc(text),
token => {
if !self.opts.iframe_srcdoc {
self.unexpected(&token);
self.set_quirks_mode(Quirks);
}
ProcessResult::Reprocess(InsertionMode::BeforeHtml, token)
}
}),
//§ the-before-html-insertion-mode
InsertionMode::BeforeHtml => match_token!(token {
Token::Characters(SplitStatus::NotSplit, text) => ProcessResult::SplitWhitespace(text),
Token::Characters(SplitStatus::Whitespace, _) => ProcessResult::Done,
Token::Comment(text) => self.append_comment_to_doc(text),
tag @ => {
self.create_root(tag.attrs);
self.mode.set(InsertionMode::BeforeHead);
ProcessResult::Done
}