use std::ascii::AsciiExt;
use std::borrow::Cow;
use tendril::StrTendril;
use interface::{QualName, ExpandedName, Attribute};
pub use self::NodeOrText::{AppendNode, AppendText};
pub use self::QuirksMode::{Quirks, LimitedQuirks, NoQuirks};
pub enum NodeOrText<Handle> {
AppendNode(Handle),
AppendText(StrTendril),
}
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
#[cfg_attr(feature = "heap_size", derive(HeapSizeOf))]
pub enum QuirksMode {
Quirks,
LimitedQuirks,
NoQuirks,
}
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum NextParserState {
Suspend,
Continue,
}
#[derive(Default)]
pub struct ElementFlags {
pub template: bool,
pub mathml_annotation_xml_integration_point: bool,
_private: ()
}
pub fn create_element<Sink>(sink: &mut Sink, name: QualName, attrs: Vec<Attribute>) -> Sink::Handle
where Sink: TreeSink {
let mut flags = ElementFlags::default();
match name.expanded() {
expanded_name!(html "template") => {
flags.template = true
}
expanded_name!(mathml "annotation-xml") => {
flags.mathml_annotation_xml_integration_point = attrs.iter().any(|attr| {
attr.name.expanded() == expanded_name!("", "encoding") && (
attr.value.eq_ignore_ascii_case("text/html") ||
attr.value.eq_ignore_ascii_case("application/xhtml+xml")
)
})
}
_ => {}
}
sink.create_element(name, attrs, flags)
}
pub trait TreeSink {
type Handle: Clone;
type Output;
fn finish(self) -> Self::Output;
fn parse_error(&mut self, msg: Cow<'static, str>);
fn get_document(&mut self) -> Self::Handle;
fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> ExpandedName<'a>;
fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>, flags: ElementFlags)
-> Self::Handle;
fn create_comment(&mut self, text: StrTendril) -> Self::Handle;
fn create_pi(&mut self, target: StrTendril, data: StrTendril) -> Self::Handle;
fn append(&mut self, parent: &Self::Handle, child: NodeOrText<Self::Handle>);
fn append_doctype_to_document(&mut self,
name: StrTendril,
public_id: StrTendril,
system_id: StrTendril);
fn mark_script_already_started(&mut self, _node: &Self::Handle) {}
fn pop(&mut self, _node: &Self::Handle) {}
fn get_template_contents(&mut self, target: &Self::Handle) -> Self::Handle;
fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool;
fn same_tree(&self, _x: &Self::Handle, _y: &Self::Handle) -> bool { true }
fn set_quirks_mode(&mut self, mode: QuirksMode);
fn has_parent_node(&self, node: &Self::Handle) -> bool;
fn append_before_sibling(&mut self,
sibling: &Self::Handle,
new_node: NodeOrText<Self::Handle>);
fn add_attrs_if_missing(&mut self, target: &Self::Handle, attrs: Vec<Attribute>);
fn associate_with_form(&mut self, _target: &Self::Handle, _form: &Self::Handle) {}
fn remove_from_parent(&mut self, target: &Self::Handle);
fn reparent_children(&mut self, node: &Self::Handle, new_parent: &Self::Handle);
fn is_mathml_annotation_xml_integration_point(&self, _handle: &Self::Handle) -> bool {
false
}
fn set_current_line(&mut self, _line_number: u64) {}
fn complete_script(&mut self, _node: &Self::Handle) -> NextParserState {
NextParserState::Continue
}
}
pub trait Tracer {
type Handle;
fn trace_handle(&self, node: &Self::Handle);
}