(open_elems: &[Handle]) -> &Handle {
open_elems.last().expect("no current element")
}
/// Helper macro that generates a [pattern](https://doc.rust-lang.org/reference/patterns.html) representing
/// a [`Tag`] to make matching on [`Tag`]s less verbose.
///
/// This macro accepts 4 forms:
///
/// - `tag!()` where `div` can be any valid tag name. This matches a start tag where the tag name is "div".
/// If the tag name contains characters other than [a-zA-Z0-9_] then it should be quoted a `<"div">`.
/// - `tag!(
)` where `div` can be any valid tag name. This matches a end tag where the tag name is "div".
/// If the tag name contains characters other than [a-zA-Z0-9_] then it should be quoted a `"div">`.
/// - `tag!(<>)`. This matches any start tag (regardless of tag name).
/// - `tag!(>)`. This matches any end tag (regardless of tag name).
///
/// Additionally any of the above can be freely combined with `|` to create an "or" match pattern.
/// For example `tag!( | >)` will match a "head" start tag or any end tag.
#[rustfmt::skip]
macro_rules! tag {
// Any start tag
(<>) => {
crate::tokenizer::Tag { kind: crate::tokenizer::StartTag, .. }
};
(<>|$($tail:tt)*) => {
tag!(<>) | tag!($($tail)*)
};
// Any end tag
(>) => {
crate::tokenizer::Tag { kind: crate::tokenizer::EndTag, .. }
};
(>|$($tail:tt)*) => {
tag!(>) | tag!($($tail)*)
};
// Named start tag
(<$tag:tt>) => {
crate::tokenizer::Tag { kind: crate::tokenizer::StartTag, name: local_name!($tag), .. }
};
(<$tag:tt>|$($tail:tt)*) => {
tag!(<$tag>) | tag!($($tail)*)
};
// Named end tag
($tag:tt>) => {
crate::tokenizer::Tag { kind: crate::tokenizer::EndTag, name: local_name!($tag), .. }
};
($tag:tt>|$($tail:tt)*) => {
tag!($tag>) | tag!($($tail)*)
};
}
#[doc(hidden)]
impl TreeBuilder
where
Handle: Clone,
Sink: TreeSink,
{
/// Process an HTML token.
///
///
pub(crate) fn step(&self, mode: InsertionMode, token: Token) -> ProcessResult {
self.debug_step(mode, &token);
match mode {
// § The "initial" insertion mode
// https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode
InsertionMode::Initial => match 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
// https://html.spec.whatwg.org/multipage/parsing.html#the-before-html-insertion-mode
InsertionMode::BeforeHtml => {
let anything_else = |token: Token| {
self.create_root(vec![]);
ProcessResult::Reprocess(InsertionMode::BeforeHead, token)
};
match token {
Token::Comment(text) => self.append_comment_to_doc(text),
Token::Characters(SplitStatus::NotSplit, text) => {
ProcessResult::SplitWhitespace(text)
},
Token::Characters(SplitStatus::Whitespace, _) => ProcessResult::Done,
Token::Tag(tag @ tag!()) => {
self.create_root(tag.attrs);
self.mode.set(InsertionMode::BeforeHead);
ProcessResult::Done
},
Token::Tag(tag!( | |