// 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.
#![allow(warnings)]
//! The HTML5 tree builder.
pub use crate::interface::{create_element, ElementFlags, NextParserState, Tracer, TreeSink};
pub use crate::interface::{AppendNode, AppendText, Attribute, NodeOrText};
pub use crate::interface::{LimitedQuirks, NoQuirks, Quirks, QuirksMode};
use self::types::*;
use crate::tendril::StrTendril;
use crate::{ExpandedName, LocalName, Namespace, QualName};
use crate::tokenizer;
use crate::tokenizer::states as tok_state;
use crate::tokenizer::{Doctype, EndTag, StartTag, Tag, TokenSink, TokenSinkResult};
use std::borrow::Cow::Borrowed;
use std::collections::VecDeque;
use std::default::Default;
use std::iter::{Enumerate, Rev};
use std::mem::replace;
use std::{fmt, slice};
use crate::tokenizer::states::{RawData, RawKind};
use crate::tree_builder::tag_sets::*;
use crate::tree_builder::types::*;
use crate::util::str::to_escaped_string;
use log::{debug, log_enabled, warn, Level};
use mac::{_tt_as_expr_hack, format_if, matches};
pub use self::PushFlag::*;
#[macro_use]
mod tag_sets;
mod data;
mod types;
include!(concat!(env!("OUT_DIR"), "/rules.rs"));
/// Tree builder options, with an impl for Default.
#[derive(Copy, Clone)]
pub struct TreeBuilderOpts {
/// Report all parse errors described in the spec, at some
/// performance penalty? Default: false
pub exact_errors: bool,
/// Is scripting enabled?
pub scripting_enabled: bool,
/// Is this an `iframe srcdoc` document?
pub iframe_srcdoc: bool,
/// Should we drop the DOCTYPE (if any) from the tree?
pub drop_doctype: bool,
/// Obsolete, ignored.
pub ignore_missing_rules: bool,
/// Initial TreeBuilder quirks mode. Default: NoQuirks
pub quirks_mode: QuirksMode,
}
impl Default for TreeBuilderOpts {
fn default() -> TreeBuilderOpts {
TreeBuilderOpts {
exact_errors: false,
scripting_enabled: true,
iframe_srcdoc: false,
drop_doctype: false,
ignore_missing_rules: false,
quirks_mode: NoQuirks,
}
}
}
/// The HTML tree builder.
pub struct TreeBuilder {
/// Options controlling the behavior of the tree builder.
opts: TreeBuilderOpts,
/// Consumer of tree modifications.
pub sink: Sink,
/// Insertion mode.
mode: InsertionMode,
/// Original insertion mode, used by Text and InTableText modes.
orig_mode: Option,
/// Stack of template insertion modes.
template_modes: Vec,
/// Pending table character tokens.
pending_table_text: Vec<(SplitStatus, StrTendril)>,
/// Quirks mode as set by the parser.
/// FIXME: can scripts etc. change this?
quirks_mode: QuirksMode,
/// The document node, which is created by the sink.
doc_handle: Handle,
/// Stack of open elements, most recently added at end.
open_elems: Vec,
/// List of active formatting elements.
active_formatting: Vec>,
//§ the-element-pointers
/// Head element pointer.
head_elem: Option,
/// Form element pointer.
form_elem: Option,
//§ END
/// Frameset-ok flag.
frameset_ok: bool,
/// Ignore a following U+000A LINE FEED?
ignore_lf: bool,
/// Is foster parenting enabled?
foster_parenting: bool,
/// The context element for the fragment parsing algorithm.
context_elem: Option,
/// Track current line
current_line: u64,
// WARNING: If you add new fields that contain Handles, you
// must add them to trace_handles() below to preserve memory
// safety!
//
// FIXME: Auto-generate the trace hooks like Servo does.
}
impl TreeBuilder
where
Handle: Clone,
Sink: TreeSink,
{
/// Create a new tree builder which sends tree modifications to a particular `TreeSink`.
///
/// The tree builder is also a `TokenSink`.
pub fn new(mut sink: Sink, opts: TreeBuilderOpts) -> TreeBuilder {
let doc_handle = sink.get_document();
TreeBuilder {
opts: opts,
sink: sink,
mode: Initial,
orig_mode: None,
template_modes: vec![],
pending_table_text: vec![],
quirks_mode: opts.quirks_mode,
doc_handle: doc_handle,
open_elems: vec![],
active_formatting: vec![],
head_elem: None,
form_elem: None,
frameset_ok: true,
ignore_lf: false,
foster_parenting: false,
context_elem: None,
current_line: 1,
}
}
/// Create a new tree builder which sends tree modifications to a particular `TreeSink`.
/// This is for parsing fragments.
///
/// The tree builder is also a `TokenSink`.
pub fn new_for_fragment(
mut sink: Sink,
context_elem: Handle,
form_elem: Option,
opts: TreeBuilderOpts,
) -> TreeBuilder {
let doc_handle = sink.get_document();
let context_is_template = sink.elem_name(&context_elem) == expanded_name!(html "template");
let mut tb = TreeBuilder {
opts: opts,
sink: sink,
mode: Initial,
orig_mode: None,
template_modes: if context_is_template {
vec![InTemplate]
} else {
vec![]
},
pending_table_text: vec![],
quirks_mode: opts.quirks_mode,
doc_handle: doc_handle,
open_elems: vec![],
active_formatting: vec![],
head_elem: None,
form_elem: form_elem,
frameset_ok: true,
ignore_lf: false,
foster_parenting: false,
context_elem: Some(context_elem),
current_line: 1,
};
// https://html.spec.whatwg.org/multipage/#parsing-html-fragments
// 5. Let root be a new html element with no attributes.
// 6. Append the element root to the Document node created above.
// 7. Set up the parser's stack of open elements so that it contains just the single element root.
tb.create_root(vec![]);
// 10. Reset the parser's insertion mode appropriately.
tb.mode = tb.reset_insertion_mode();
tb
}
// https://html.spec.whatwg.org/multipage/#concept-frag-parse-context
// Step 4. Set the state of the HTML parser's tokenization stage as follows:
pub fn tokenizer_state_for_context_elem(&self) -> tok_state::State {
let elem = self.context_elem.as_ref().expect("no context element");
let name = match self.sink.elem_name(elem) {
ExpandedName {
ns: &ns!(html),
local,
} => local,
_ => return tok_state::Data,
};
match *name {
local_name!("title") | local_name!("textarea") => tok_state::RawData(tok_state::Rcdata),
local_name!("style") |
local_name!("xmp") |
local_name!("iframe") |
local_name!("noembed") |
local_name!("noframes") => tok_state::RawData(tok_state::Rawtext),
local_name!("script") => tok_state::RawData(tok_state::ScriptData),
local_name!("noscript") => {
if self.opts.scripting_enabled {
tok_state::RawData(tok_state::Rawtext)
} else {
tok_state::Data
}
},
local_name!("plaintext") => tok_state::Plaintext,
_ => tok_state::Data,
}
}
/// Call the `Tracer`'s `trace_handle` method on every `Handle` in the tree builder's
/// internal state. This is intended to support garbage-collected DOMs.
pub fn trace_handles(&self, tracer: &Tracer) {
tracer.trace_handle(&self.doc_handle);
for e in &self.open_elems {
tracer.trace_handle(e);
}
for e in &self.active_formatting {
match e {
&Element(ref h, _) => tracer.trace_handle(h),
_ => (),
}
}
self.head_elem.as_ref().map(|h| tracer.trace_handle(h));
self.form_elem.as_ref().map(|h| tracer.trace_handle(h));
self.context_elem.as_ref().map(|h| tracer.trace_handle(h));
}
#[allow(dead_code)]
fn dump_state(&self, label: String) {
println!("dump_state on {}", label);
print!(" open_elems:");
for node in self.open_elems.iter() {
let name = self.sink.elem_name(node);
match *name.ns {
ns!(html) => print!(" {}", name.local),
_ => panic!(),
}
}
println!("");
print!(" active_formatting:");
for entry in self.active_formatting.iter() {
match entry {
&Marker => print!(" Marker"),
&Element(ref h, _) => {
let name = self.sink.elem_name(h);
match *name.ns {
ns!(html) => print!(" {}", name.local),
_ => panic!(),
}
},
}
}
println!("");
}
fn debug_step(&self, mode: InsertionMode, token: &Token) {
if log_enabled!(Level::Debug) {
debug!(
"processing {} in insertion mode {:?}",
to_escaped_string(token),
mode
);
}
}
fn process_to_completion(&mut self, mut token: Token) -> TokenSinkResult {
// Queue of additional tokens yet to be processed.
// This stays empty in the common case where we don't split whitespace.
let mut more_tokens = VecDeque::new();
loop {
let should_have_acknowledged_self_closing_flag = matches!(
token,
TagToken(Tag {
self_closing: true,
kind: StartTag,
..
})
);
let result = if self.is_foreign(&token) {
self.step_foreign(token)
} else {
let mode = self.mode;
self.step(mode, token)
};
match result {
Done => {
if should_have_acknowledged_self_closing_flag {
self.sink
.parse_error(Borrowed("Unacknowledged self-closing tag"));
}
token = unwrap_or_return!(
more_tokens.pop_front(),
tokenizer::TokenSinkResult::Continue
);
},
DoneAckSelfClosing => {
token = unwrap_or_return!(
more_tokens.pop_front(),
tokenizer::TokenSinkResult::Continue
);
},
Reprocess(m, t) => {
self.mode = m;
token = t;
},
ReprocessForeign(t) => {
token = t;
},
SplitWhitespace(mut buf) => {
let p = buf.pop_front_char_run(|c| c.is_ascii_whitespace());
let (first, is_ws) = unwrap_or_return!(p, tokenizer::TokenSinkResult::Continue);
let status = if is_ws { Whitespace } else { NotWhitespace };
token = CharacterTokens(status, first);
if buf.len32() > 0 {
more_tokens.push_back(CharacterTokens(NotSplit, buf));
}
},
Script(node) => {
assert!(more_tokens.is_empty());
return tokenizer::TokenSinkResult::Script(node);
},
ToPlaintext => {
assert!(more_tokens.is_empty());
return tokenizer::TokenSinkResult::Plaintext;
},
ToRawData(k) => {
assert!(more_tokens.is_empty());
return tokenizer::TokenSinkResult::RawData(k);
},
}
}
}
/// Are we parsing a HTML fragment?
pub fn is_fragment(&self) -> bool {
self.context_elem.is_some()
}
/// https://html.spec.whatwg.org/multipage/#appropriate-place-for-inserting-a-node
fn appropriate_place_for_insertion(
&mut self,
override_target: Option,
) -> InsertionPoint {
use self::tag_sets::*;
declare_tag_set!(foster_target = "table" "tbody" "tfoot" "thead" "tr");
let target = override_target.unwrap_or_else(|| self.current_node().clone());
if !(self.foster_parenting && self.elem_in(&target, foster_target)) {
if self.html_elem_named(&target, local_name!("template")) {
// No foster parenting (inside template).
let contents = self.sink.get_template_contents(&target);
return LastChild(contents);
} else {
// No foster parenting (the common case).
return LastChild(target);
}
}
// Foster parenting
let mut iter = self.open_elems.iter().rev().peekable();
while let Some(elem) = iter.next() {
if self.html_elem_named(&elem, local_name!("template")) {
let contents = self.sink.get_template_contents(&elem);
return LastChild(contents);
} else if self.html_elem_named(&elem, local_name!("table")) {
return TableFosterParenting {
element: elem.clone(),
prev_element: (*iter.peek().unwrap()).clone(),
};
}
}
let html_elem = self.html_elem();
LastChild(html_elem.clone())
}
fn insert_at(&mut self, insertion_point: InsertionPoint, child: NodeOrText) {
match insertion_point {
LastChild(parent) => self.sink.append(&parent, child),
BeforeSibling(sibling) => self.sink.append_before_sibling(&sibling, child),
TableFosterParenting {
element,
prev_element,
} => self
.sink
.append_based_on_parent_node(&element, &prev_element, child),
}
}
}
impl TokenSink for TreeBuilder
where
Handle: Clone,
Sink: TreeSink,
{
type Handle = Handle;
fn process_token(
&mut self,
token: tokenizer::Token,
line_number: u64,
) -> TokenSinkResult {
if line_number != self.current_line {
self.sink.set_current_line(line_number);
}
let ignore_lf = replace(&mut self.ignore_lf, false);
// Handle `ParseError` and `DoctypeToken`; convert everything else to the local `Token` type.
let token = match token {
tokenizer::ParseError(e) => {
self.sink.parse_error(e);
return tokenizer::TokenSinkResult::Continue;
},
tokenizer::DoctypeToken(dt) => {
if self.mode == Initial {
let (err, quirk) = data::doctype_error_and_quirks(&dt, self.opts.iframe_srcdoc);
if err {
self.sink.parse_error(format_if!(
self.opts.exact_errors,
"Bad DOCTYPE",
"Bad DOCTYPE: {:?}",
dt
));
}
let Doctype {
name,
public_id,
system_id,
force_quirks: _,
} = dt;
if !self.opts.drop_doctype {
self.sink.append_doctype_to_document(
name.unwrap_or(StrTendril::new()),
public_id.unwrap_or(StrTendril::new()),
system_id.unwrap_or(StrTendril::new()),
);
}
self.set_quirks_mode(quirk);
self.mode = BeforeHtml;
return tokenizer::TokenSinkResult::Continue;
} else {
self.sink.parse_error(format_if!(
self.opts.exact_errors,
"DOCTYPE in body",
"DOCTYPE in insertion mode {:?}",
self.mode
));
return tokenizer::TokenSinkResult::Continue;
}
},
tokenizer::TagToken(x) => TagToken(x),
tokenizer::CommentToken(x) => CommentToken(x),
tokenizer::NullCharacterToken => NullCharacterToken,
tokenizer::EOFToken => EOFToken,
tokenizer::CharacterTokens(mut x) => {
if ignore_lf && x.starts_with("\n") {
x.pop_front(1);
}
if x.is_empty() {
return tokenizer::TokenSinkResult::Continue;
}
CharacterTokens(NotSplit, x)
},
};
self.process_to_completion(token)
}
fn end(&mut self) {
for elem in self.open_elems.drain(..).rev() {
self.sink.pop(&elem);
}
}
fn adjusted_current_node_present_but_not_in_html_namespace(&self) -> bool {
!self.open_elems.is_empty() &&
self.sink.elem_name(self.adjusted_current_node()).ns != &ns!(html)
}
}
pub fn html_elem(open_elems: &[Handle]) -> &Handle {
&open_elems[0]
}
pub struct ActiveFormattingIter<'a, Handle: 'a> {
iter: Rev>>>,
}
impl<'a, Handle> Iterator for ActiveFormattingIter<'a, Handle> {
type Item = (usize, &'a Handle, &'a Tag);
fn next(&mut self) -> Option<(usize, &'a Handle, &'a Tag)> {
match self.iter.next() {
None | Some((_, &Marker)) => None,
Some((i, &Element(ref h, ref t))) => Some((i, h, t)),
}
}
}
pub enum PushFlag {
Push,
NoPush,
}
enum Bookmark {
Replace(Handle),
InsertAfter(Handle),
}
macro_rules! qualname {
("", $local:tt) => {
QualName {
prefix: None,
ns: ns!(),
local: local_name!($local),
}
};
($prefix: tt $ns:tt $local:tt) => {
QualName {
prefix: Some(namespace_prefix!($prefix)),
ns: ns!($ns),
local: local_name!($local),
}
};
}
#[doc(hidden)]
impl TreeBuilder
where
Handle: Clone,
Sink: TreeSink,
{
fn unexpected(&mut self, _thing: &T) -> ProcessResult {
self.sink.parse_error(format_if!(
self.opts.exact_errors,
"Unexpected token",
"Unexpected token {} in insertion mode {:?}",
to_escaped_string(_thing),
self.mode
));
Done
}
fn assert_named(&mut self, node: &Handle, name: LocalName) {
assert!(self.html_elem_named(&node, name));
}
/// Iterate over the active formatting elements (with index in the list) from the end
/// to the last marker, or the beginning if there are no markers.
fn active_formatting_end_to_marker<'a>(&'a self) -> ActiveFormattingIter<'a, Handle> {
ActiveFormattingIter {
iter: self.active_formatting.iter().enumerate().rev(),
}
}
fn position_in_active_formatting(&self, element: &Handle) -> Option {
self.active_formatting.iter().position(|n| match n {
&Marker => false,
&Element(ref handle, _) => self.sink.same_node(handle, element),
})
}
fn set_quirks_mode(&mut self, mode: QuirksMode) {
self.quirks_mode = mode;
self.sink.set_quirks_mode(mode);
}
fn stop_parsing(&mut self) -> ProcessResult {
Done
}
//§ parsing-elements-that-contain-only-text
// Switch to `Text` insertion mode, save the old mode, and
// switch the tokenizer to a raw-data state.
// The latter only takes effect after the current / next
// `process_token` of a start tag returns!
fn to_raw_text_mode(&mut self, k: RawKind) -> ProcessResult {
self.orig_mode = Some(self.mode);
self.mode = Text;
ToRawData(k)
}
// The generic raw text / RCDATA parsing algorithm.
fn parse_raw_data(&mut self, tag: Tag, k: RawKind) -> ProcessResult {
self.insert_element_for(tag);
self.to_raw_text_mode(k)
}
//§ END
fn current_node(&self) -> &Handle {
self.open_elems.last().expect("no current element")
}
fn adjusted_current_node(&self) -> &Handle {
if self.open_elems.len() == 1 {
if let Some(ctx) = self.context_elem.as_ref() {
return ctx;
}
}
self.current_node()
}
fn current_node_in(&self, set: TagSet) -> bool
where
TagSet: Fn(ExpandedName) -> bool,
{
set(self.sink.elem_name(self.current_node()))
}
// Insert at the "appropriate place for inserting a node".
fn insert_appropriately(&mut self, child: NodeOrText, override_target: Option) {
let insertion_point = self.appropriate_place_for_insertion(override_target);
self.insert_at(insertion_point, child);
}
fn adoption_agency(&mut self, subject: LocalName) {
// 1.
if self.current_node_named(subject.clone()) {
if self
.position_in_active_formatting(self.current_node())
.is_none()
{
self.pop();
return;
}
}
// 2. 3. 4.
for _ in 0..8 {
// 5.
let (fmt_elem_index, fmt_elem, fmt_elem_tag) = unwrap_or_return!(
// We clone the Handle and Tag so they don't cause an immutable borrow of self.
self.active_formatting_end_to_marker()
.filter(|&(_, _, tag)| tag.name == subject)
.next()
.map(|(i, h, t)| (i, h.clone(), t.clone())),
{
self.process_end_tag_in_body(Tag {
kind: EndTag,
name: subject,
self_closing: false,
attrs: vec![],
});
}
);
let fmt_elem_stack_index = unwrap_or_return!(
self.open_elems
.iter()
.rposition(|n| self.sink.same_node(n, &fmt_elem)),
{
self.sink
.parse_error(Borrowed("Formatting element not open"));
self.active_formatting.remove(fmt_elem_index);
}
);
// 7.
if !self.in_scope(default_scope, |n| self.sink.same_node(&n, &fmt_elem)) {
self.sink
.parse_error(Borrowed("Formatting element not in scope"));
return;
}
// 8.
if !self.sink.same_node(self.current_node(), &fmt_elem) {
self.sink
.parse_error(Borrowed("Formatting element not current node"));
}
// 9.
let (furthest_block_index, furthest_block) = unwrap_or_return!(
self.open_elems
.iter()
.enumerate()
.skip(fmt_elem_stack_index)
.filter(|&(_, open_element)| self.elem_in(open_element, special_tag))
.next()
.map(|(i, h)| (i, h.clone())),
// 10.
{
self.open_elems.truncate(fmt_elem_stack_index);
self.active_formatting.remove(fmt_elem_index);
}
);
// 11.
let common_ancestor = self.open_elems[fmt_elem_stack_index - 1].clone();
// 12.
let mut bookmark = Bookmark::Replace(fmt_elem.clone());
// 13.
let mut node;
let mut node_index = furthest_block_index;
let mut last_node = furthest_block.clone();
// 13.1.
let mut inner_counter = 0;
loop {
// 13.2.
inner_counter += 1;
// 13.3.
node_index -= 1;
node = self.open_elems[node_index].clone();
// 13.4.
if self.sink.same_node(&node, &fmt_elem) {
break;
}
// 13.5.
if inner_counter > 3 {
self.position_in_active_formatting(&node)
.map(|position| self.active_formatting.remove(position));
self.open_elems.remove(node_index);
continue;
}
let node_formatting_index = unwrap_or_else!(
self.position_in_active_formatting(&node),
// 13.6.
{
self.open_elems.remove(node_index);
continue;
}
);
// 13.7.
let tag = match self.active_formatting[node_formatting_index] {
Element(ref h, ref t) => {
assert!(self.sink.same_node(h, &node));
t.clone()
},
Marker => panic!("Found marker during adoption agency"),
};
// FIXME: Is there a way to avoid cloning the attributes twice here (once on their
// own, once as part of t.clone() above)?
let new_element = create_element(
&mut self.sink,
QualName::new(None, ns!(html), tag.name.clone()),
tag.attrs.clone(),
);
self.open_elems[node_index] = new_element.clone();
self.active_formatting[node_formatting_index] = Element(new_element.clone(), tag);
node = new_element;
// 13.8.
if self.sink.same_node(&last_node, &furthest_block) {
bookmark = Bookmark::InsertAfter(node.clone());
}
// 13.9.
self.sink.remove_from_parent(&last_node);
self.sink.append(&node, AppendNode(last_node.clone()));
// 13.10.
last_node = node.clone();
// 13.11.
}
// 14.
self.sink.remove_from_parent(&last_node);
self.insert_appropriately(AppendNode(last_node.clone()), Some(common_ancestor));
// 15.
// FIXME: Is there a way to avoid cloning the attributes twice here (once on their own,
// once as part of t.clone() above)?
let new_element = create_element(
&mut self.sink,
QualName::new(None, ns!(html), fmt_elem_tag.name.clone()),
fmt_elem_tag.attrs.clone(),
);
let new_entry = Element(new_element.clone(), fmt_elem_tag);
// 16.
self.sink.reparent_children(&furthest_block, &new_element);
// 17.
self.sink
.append(&furthest_block, AppendNode(new_element.clone()));
// 18.
// FIXME: We could probably get rid of the position_in_active_formatting() calls here
// if we had a more clever Bookmark representation.
match bookmark {
Bookmark::Replace(to_replace) => {
let index = self
.position_in_active_formatting(&to_replace)
.expect("bookmark not found in active formatting elements");
self.active_formatting[index] = new_entry;
},
Bookmark::InsertAfter(previous) => {
let index = self
.position_in_active_formatting(&previous)
.expect("bookmark not found in active formatting elements") +
1;
self.active_formatting.insert(index, new_entry);
let old_index = self
.position_in_active_formatting(&fmt_elem)
.expect("formatting element not found in active formatting elements");
self.active_formatting.remove(old_index);
},
}
// 19.
self.remove_from_stack(&fmt_elem);
let new_furthest_block_index = self
.open_elems
.iter()
.position(|n| self.sink.same_node(n, &furthest_block))
.expect("furthest block missing from open element stack");
self.open_elems
.insert(new_furthest_block_index + 1, new_element);
// 20.
}
}
fn push(&mut self, elem: &Handle) {
self.open_elems.push(elem.clone());
}
fn pop(&mut self) -> Handle {
let elem = self.open_elems.pop().expect("no current element");
self.sink.pop(&elem);
elem
}
fn remove_from_stack(&mut self, elem: &Handle) {
let sink = &mut self.sink;
let position = self
.open_elems
.iter()
.rposition(|x| sink.same_node(elem, &x));
if let Some(position) = position {
self.open_elems.remove(position);
sink.pop(elem);
}
}
fn is_marker_or_open(&self, entry: &FormatEntry) -> bool {
match *entry {
Marker => true,
Element(ref node, _) => self
.open_elems
.iter()
.rev()
.any(|n| self.sink.same_node(&n, &node)),
}
}
/// Reconstruct the active formatting elements.
fn reconstruct_formatting(&mut self) {
{
let last = unwrap_or_return!(self.active_formatting.last(), ());
if self.is_marker_or_open(last) {
return;
}
}
let mut entry_index = self.active_formatting.len() - 1;
loop {
if entry_index == 0 {
break;
}
entry_index -= 1;
if self.is_marker_or_open(&self.active_formatting[entry_index]) {
entry_index += 1;
break;
}
}
loop {
let tag = match self.active_formatting[entry_index] {
Element(_, ref t) => t.clone(),
Marker => panic!("Found marker during formatting element reconstruction"),
};
// FIXME: Is there a way to avoid cloning the attributes twice here (once on their own,
// once as part of t.clone() above)?
let new_element =
self.insert_element(Push, ns!(html), tag.name.clone(), tag.attrs.clone());
self.active_formatting[entry_index] = Element(new_element, tag);
if entry_index == self.active_formatting.len() - 1 {
break;
}
entry_index += 1;
}
}
/// Get the first element on the stack, which will be the element.
fn html_elem(&self) -> &Handle {
&self.open_elems[0]
}
/// Get the second element on the stack, if it's a HTML body element.
fn body_elem(&self) -> Option<&Handle> {
if self.open_elems.len() <= 1 {
return None;
}
let node = &self.open_elems[1];
if self.html_elem_named(node, local_name!("body")) {
Some(node)
} else {
None
}
}
/// Signal an error depending on the state of the stack of open elements at
/// the end of the body.
fn check_body_end(&mut self) {
declare_tag_set!(body_end_ok =
"dd" "dt" "li" "optgroup" "option" "p" "rp" "rt" "tbody" "td" "tfoot" "th"
"thead" "tr" "body" "html");
for elem in self.open_elems.iter() {
let error;
{
let name = self.sink.elem_name(elem);
if body_end_ok(name) {
continue;
}
error = format_if!(
self.opts.exact_errors,
"Unexpected open tag at end of body",
"Unexpected open tag {:?} at end of body",
name
);
}
self.sink.parse_error(error);
// FIXME: Do we keep checking after finding one bad tag?
// The spec suggests not.
return;
}
}
fn in_scope(&self, scope: TagSet, pred: Pred) -> bool
where
TagSet: Fn(ExpandedName) -> bool,
Pred: Fn(Handle) -> bool,
{
for node in self.open_elems.iter().rev() {
if pred(node.clone()) {
return true;
}
if scope(self.sink.elem_name(node)) {
return false;
}
}
// supposed to be impossible, because is always in scope
false
}
fn elem_in(&self, elem: &Handle, set: TagSet) -> bool
where
TagSet: Fn(ExpandedName) -> bool,
{
set(self.sink.elem_name(elem))
}
fn html_elem_named(&self, elem: &Handle, name: LocalName) -> bool {
let expanded = self.sink.elem_name(elem);
*expanded.ns == ns!(html) && *expanded.local == name
}
fn in_html_elem_named(&self, name: LocalName) -> bool {
self.open_elems
.iter()
.any(|elem| self.html_elem_named(elem, name.clone()))
}
fn current_node_named(&self, name: LocalName) -> bool {
self.html_elem_named(self.current_node(), name)
}
fn in_scope_named(&self, scope: TagSet, name: LocalName) -> bool
where
TagSet: Fn(ExpandedName) -> bool,
{
self.in_scope(scope, |elem| self.html_elem_named(&elem, name.clone()))
}
//§ closing-elements-that-have-implied-end-tags
fn generate_implied_end(&mut self, set: TagSet)
where
TagSet: Fn(ExpandedName) -> bool,
{
loop {
{
let elem = unwrap_or_return!(self.open_elems.last(), ());
let nsname = self.sink.elem_name(elem);
if !set(nsname) {
return;
}
}
self.pop();
}
}
fn generate_implied_end_except(&mut self, except: LocalName) {
self.generate_implied_end(|p| {
if *p.ns == ns!(html) && *p.local == except {
false
} else {
cursory_implied_end(p)
}
});
}
//§ END
// Pop elements until the current element is in the set.
fn pop_until_current(&mut self, pred: TagSet)
where
TagSet: Fn(ExpandedName) -> bool,
{
loop {
if self.current_node_in(|x| pred(x)) {
break;
}
self.open_elems.pop();
}
}
// Pop elements until an element from the set has been popped. Returns the
// number of elements popped.
fn pop_until
(&mut self, pred: P) -> usize
where
P: Fn(ExpandedName) -> bool,
{
let mut n = 0;
loop {
n += 1;
match self.open_elems.pop() {
None => break,
Some(elem) => {
if pred(self.sink.elem_name(&elem)) {
break;
}
},
}
}
n
}
fn pop_until_named(&mut self, name: LocalName) -> usize {
self.pop_until(|p| *p.ns == ns!(html) && *p.local == name)
}
// Pop elements until one with the specified name has been popped.
// Signal an error if it was not the first one.
fn expect_to_close(&mut self, name: LocalName) {
if self.pop_until_named(name.clone()) != 1 {
self.sink.parse_error(format_if!(
self.opts.exact_errors,
"Unexpected open element",
"Unexpected open element while closing {:?}",
name
));
}
}
fn close_p_element(&mut self) {
declare_tag_set!(implied = [cursory_implied_end] - "p");
self.generate_implied_end(implied);
self.expect_to_close(local_name!("p"));
}
fn close_p_element_in_button_scope(&mut self) {
if self.in_scope_named(button_scope, local_name!("p")) {
self.close_p_element();
}
}
// Check tags for type=hidden
fn is_type_hidden(&self, tag: &Tag) -> bool {
match tag
.attrs
.iter()
.find(|&at| at.name.expanded() == expanded_name!("", "type"))
{
None => false,
Some(at) => (&*at.value).eq_ignore_ascii_case("hidden"),
}
}
fn foster_parent_in_body(&mut self, token: Token) -> ProcessResult {
warn!("foster parenting not implemented");
self.foster_parenting = true;
let res = self.step(InBody, token);
// FIXME: what if res is Reprocess?
self.foster_parenting = false;
res
}
fn process_chars_in_table(&mut self, token: Token) -> ProcessResult {
declare_tag_set!(table_outer = "table" "tbody" "tfoot" "thead" "tr");
if self.current_node_in(table_outer) {
assert!(self.pending_table_text.is_empty());
self.orig_mode = Some(self.mode);
Reprocess(InTableText, token)
} else {
self.sink.parse_error(format_if!(
self.opts.exact_errors,
"Unexpected characters in table",
"Unexpected characters {} in table",
to_escaped_string(&token)
));
self.foster_parent_in_body(token)
}
}
// https://html.spec.whatwg.org/multipage/#reset-the-insertion-mode-appropriately
fn reset_insertion_mode(&mut self) -> InsertionMode {
for (i, mut node) in self.open_elems.iter().enumerate().rev() {
let last = i == 0usize;
if let (true, Some(ctx)) = (last, self.context_elem.as_ref()) {
node = ctx;
}
let name = match self.sink.elem_name(node) {
ExpandedName {
ns: &ns!(html),
local,
} => local,
_ => continue,
};
match *name {
local_name!("select") => {
for ancestor in self.open_elems[0..i].iter().rev() {
if self.html_elem_named(ancestor, local_name!("template")) {
return InSelect;
} else if self.html_elem_named(ancestor, local_name!("table")) {
return InSelectInTable;
}
}
return InSelect;
},
local_name!("td") | local_name!("th") => {
if !last {
return InCell;
}
},
local_name!("tr") => return InRow,
local_name!("tbody") | local_name!("thead") | local_name!("tfoot") => {
return InTableBody;
},
local_name!("caption") => return InCaption,
local_name!("colgroup") => return InColumnGroup,
local_name!("table") => return InTable,
local_name!("template") => return *self.template_modes.last().unwrap(),
local_name!("head") => {
if !last {
return InHead;
}
},
local_name!("body") => return InBody,
local_name!("frameset") => return InFrameset,
local_name!("html") => match self.head_elem {
None => return BeforeHead,
Some(_) => return AfterHead,
},
_ => (),
}
}
InBody
}
fn close_the_cell(&mut self) {
self.generate_implied_end(cursory_implied_end);
if self.pop_until(td_th) != 1 {
self.sink
.parse_error(Borrowed("expected to close