use html5ever::tendril::TendrilSink;
use html5ever::{ParseOpts, local_name, ns, parse_document};
use markup5ever_rcdom::{Handle, NodeData, RcDom};
pub(crate) const XHTML_NS: &str = "http://www.w3.org/1999/xhtml";
const SVG_NS: &str = "http://www.w3.org/2000/svg";
const MATHML_NS: &str = "http://www.w3.org/1998/Math/MathML";
const XLINK_NS: &str = "http://www.w3.org/1999/xlink";
pub(crate) type Id = usize;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Space {
Html,
Svg,
Math,
}
#[derive(Debug, Clone)]
pub(crate) enum Kind {
Document,
Element {
name: String,
space: Space,
attrs: Vec<(String, String)>,
},
Text(String),
}
#[derive(Debug, Clone)]
pub(crate) struct Node {
pub(crate) kind: Kind,
pub(crate) parent: Option<Id>,
pub(crate) children: Vec<Id>,
}
#[derive(Debug, Clone)]
pub(crate) struct Dom {
pub(crate) nodes: Vec<Node>,
}
impl Dom {
pub(crate) fn parse(text: &str) -> Dom {
let mut opts = ParseOpts::default();
opts.tree_builder.scripting_enabled = false;
let rc = parse_document(RcDom::default(), opts).one(text);
let mut dom = Dom {
nodes: vec![Node {
kind: Kind::Document,
parent: None,
children: Vec::new(),
}],
};
let mut stack: Vec<(Handle, Id)> = rc
.document
.children
.borrow()
.iter()
.rev()
.map(|child| (child.clone(), 0))
.collect();
while let Some((handle, parent)) = stack.pop() {
let kind = match &handle.data {
NodeData::Element { name, attrs, .. } => {
let space = if name.ns == ns!(svg) {
Space::Svg
} else if name.ns == ns!(mathml) {
Space::Math
} else {
Space::Html
};
let attrs = attrs
.borrow()
.iter()
.filter_map(|attr| {
let local = attr.name.local.to_string();
let qualified = if attr.name.ns == ns!(xlink) {
format!("xlink:{local}")
} else if attr.name.ns == ns!(xml) {
format!("xml:{local}")
} else if attr.name.ns == ns!(xmlns) || attr.name.local == local_name!("xmlns") {
return None;
} else {
local
};
Some((qualified, attr.value.to_string()))
})
.collect();
Kind::Element {
name: name.local.to_string(),
space,
attrs,
}
}
NodeData::Text { contents } => Kind::Text(contents.borrow().to_string()),
_ => continue,
};
let id = dom.push(kind);
dom.append(parent, id);
for child in handle.children.borrow().iter().rev() {
stack.push((child.clone(), id));
}
}
dom
}
pub(crate) fn push(&mut self, kind: Kind) -> Id {
self.nodes.push(Node {
kind,
parent: None,
children: Vec::new(),
});
self.nodes.len() - 1
}
pub(crate) fn element(&mut self, name: &str, attrs: &[(&str, &str)]) -> Id {
self.push(Kind::Element {
name: name.to_string(),
space: Space::Html,
attrs: attrs
.iter()
.map(|(key, value)| ((*key).to_string(), (*value).to_string()))
.collect(),
})
}
pub(crate) fn text_node(&mut self, text: &str) -> Id {
self.push(Kind::Text(text.to_string()))
}
pub(crate) fn append(&mut self, parent: Id, child: Id) {
self.detach(child);
self.nodes[child].parent = Some(parent);
self.nodes[parent].children.push(child);
}
pub(crate) fn insert(&mut self, parent: Id, at: usize, child: Id) {
self.detach(child);
self.nodes[child].parent = Some(parent);
let at = at.min(self.nodes[parent].children.len());
self.nodes[parent].children.insert(at, child);
}
pub(crate) fn insert_after(&mut self, sibling: Id, node: Id) {
if let Some(parent) = self.nodes[sibling].parent {
let at = self.nodes[parent]
.children
.iter()
.position(|&child| child == sibling)
.map_or(0, |at| at + 1);
self.insert(parent, at, node);
}
}
pub(crate) fn detach(&mut self, node: Id) {
if let Some(parent) = self.nodes[node].parent.take() {
self.nodes[parent].children.retain(|&child| child != node);
}
}
pub(crate) fn detach_all(&mut self, nodes: &[Id]) {
let gone: std::collections::HashSet<Id> = nodes.iter().copied().collect();
let mut parents: Vec<Id> = Vec::new();
for &node in nodes {
if let Some(parent) = self.nodes[node].parent.take() {
parents.push(parent);
}
}
parents.sort_unstable();
parents.dedup();
for parent in parents {
self.nodes[parent].children.retain(|child| !gone.contains(child));
}
}
pub(crate) fn unwrap(&mut self, node: Id) {
let Some(parent) = self.nodes[node].parent else {
return;
};
let at = self.nodes[parent]
.children
.iter()
.position(|&child| child == node)
.unwrap_or(0);
let children = std::mem::take(&mut self.nodes[node].children);
self.detach(node);
for (offset, child) in children.into_iter().enumerate() {
self.nodes[child].parent = Some(parent);
self.nodes[parent].children.insert(at + offset, child);
}
}
pub(crate) fn name(&self, node: Id) -> Option<&str> {
match &self.nodes[node].kind {
Kind::Element { name, .. } => Some(name),
_ => None,
}
}
pub(crate) fn is(&self, node: Id, name: &str) -> bool {
matches!(&self.nodes[node].kind, Kind::Element { name: own, space: Space::Html, .. } if own == name)
}
pub(crate) fn is_any(&self, node: Id, names: &[&str]) -> bool {
matches!(&self.nodes[node].kind, Kind::Element { name, space: Space::Html, .. } if names.contains(&name.as_str()))
}
pub(crate) fn space(&self, node: Id) -> Option<Space> {
match &self.nodes[node].kind {
Kind::Element { space, .. } => Some(*space),
_ => None,
}
}
pub(crate) fn rename(&mut self, node: Id, new: &str) {
if let Kind::Element { name, .. } = &mut self.nodes[node].kind {
*name = new.to_string();
}
}
pub(crate) fn attr(&self, node: Id, key: &str) -> Option<&str> {
match &self.nodes[node].kind {
Kind::Element { attrs, .. } => attrs
.iter()
.find(|(name, _)| name.eq_ignore_ascii_case(key))
.map(|(_, value)| value.as_str()),
_ => None,
}
}
pub(crate) fn set_attr(&mut self, node: Id, key: &str, value: &str) {
if let Kind::Element { attrs, .. } = &mut self.nodes[node].kind {
match attrs.iter_mut().find(|(name, _)| name.eq_ignore_ascii_case(key)) {
Some((_, old)) => *old = value.to_string(),
None => attrs.push((key.to_string(), value.to_string())),
}
}
}
pub(crate) fn remove_attr(&mut self, node: Id, key: &str) {
if let Kind::Element { attrs, .. } = &mut self.nodes[node].kind {
attrs.retain(|(name, _)| !name.eq_ignore_ascii_case(key));
}
}
pub(crate) fn classes(&self, node: Id) -> impl Iterator<Item = &str> {
self.attr(node, "class").unwrap_or_default().split_whitespace()
}
pub(crate) fn has_class(&self, node: Id, class: &str) -> bool {
self.classes(node).any(|own| own == class)
}
pub(crate) fn add_class(&mut self, node: Id, class: &str) {
if !self.has_class(node, class) {
let classes = match self.attr(node, "class") {
Some(old) if !old.trim().is_empty() => format!("{old} {class}"),
_ => class.to_string(),
};
self.set_attr(node, "class", &classes);
}
}
pub(crate) fn element_children(&self, node: Id) -> Vec<Id> {
self.nodes[node]
.children
.iter()
.copied()
.filter(|&child| matches!(self.nodes[child].kind, Kind::Element { .. }))
.collect()
}
pub(crate) fn descendants(&self, node: Id) -> Vec<Id> {
let mut out = Vec::new();
let mut stack = vec![node];
while let Some(next) = stack.pop() {
out.push(next);
stack.extend(self.nodes[next].children.iter().rev());
}
out
}
pub(crate) fn find_all(&self, node: Id, name: &str) -> Vec<Id> {
self.descendants(node)
.into_iter()
.filter(|&id| self.is(id, name))
.collect()
}
pub(crate) fn walk(&self, node: Id) -> impl Iterator<Item = Id> + '_ {
let mut stack = vec![node];
std::iter::from_fn(move || {
let next = stack.pop()?;
stack.extend(self.nodes[next].children.iter().rev());
Some(next)
})
}
pub(crate) fn has_text_outside(&self, node: Id, skip: impl Fn(Id) -> bool) -> bool {
let mut stack = vec![std::slice::from_ref(&node).iter()];
while let Some(children) = stack.last_mut() {
let Some(&next) = children.next() else {
stack.pop();
continue;
};
if skip(next) {
continue;
}
if let Kind::Text(text) = &self.nodes[next].kind
&& text.chars().any(|ch| !ch.is_whitespace())
{
return true;
}
stack.push(self.nodes[next].children.iter());
}
false
}
pub(crate) fn find(&self, node: Id, name: &str) -> Option<Id> {
self.walk(node).find(|&id| self.is(id, name))
}
pub(crate) fn contains(&self, ancestor: Id, node: Id) -> bool {
let mut at = Some(node);
while let Some(id) = at {
if id == ancestor {
return true;
}
at = self.nodes[id].parent;
}
false
}
pub(crate) fn attached(&self, node: Id) -> bool {
self.contains(0, node)
}
pub(crate) fn ancestor(&self, node: Id, test: impl Fn(Id) -> bool) -> Option<Id> {
let mut at = self.nodes[node].parent;
while let Some(id) = at {
if test(id) {
return Some(id);
}
at = self.nodes[id].parent;
}
None
}
pub(crate) fn text(&self, node: Id) -> String {
let mut out = String::new();
for id in self.descendants(node) {
if let Kind::Text(text) = &self.nodes[id].kind {
out.push_str(text);
}
}
out
}
pub(crate) fn clean_text(&self, node: Id) -> String {
self.text(node).split_whitespace().collect::<Vec<_>>().join(" ")
}
pub(crate) fn html_head_body(&self) -> (Option<Id>, Option<Id>, Option<Id>) {
let html = self.element_children(0).into_iter().find(|&id| self.is(id, "html"));
let part =
|name: &str| html.and_then(|html| self.element_children(html).into_iter().find(|&id| self.is(id, name)));
(html, part("head"), part("body"))
}
pub(crate) fn to_xhtml(&self, head: &[Id], body: Id, lang: Option<&str>) -> String {
let mut out = String::with_capacity(1 << 16);
out.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<html xmlns=\"");
out.push_str(XHTML_NS);
out.push_str("\" xmlns:xlink=\"");
out.push_str(XLINK_NS);
out.push('"');
if let Some(lang) = lang {
out.push_str(" xml:lang=\"");
escape_into(&mut out, lang, true);
out.push('"');
}
out.push_str("><head>");
for &node in head {
self.write(&mut out, node, Space::Html);
}
out.push_str("</head>");
if self.is(body, "body") {
self.write(&mut out, body, Space::Html);
} else {
out.push_str("<body>");
self.write(&mut out, body, Space::Html);
out.push_str("</body>");
}
out.push_str("</html>\n");
out
}
fn write(&self, out: &mut String, node: Id, outer: Space) {
enum Step {
Open(Id, Space),
Close(String),
}
let mut stack = vec![Step::Open(node, outer)];
while let Some(step) = stack.pop() {
let (id, outer) = match step {
Step::Close(name) => {
out.push_str("</");
out.push_str(&name);
out.push('>');
continue;
}
Step::Open(id, outer) => (id, outer),
};
match &self.nodes[id].kind {
Kind::Document => {
for &child in self.nodes[id].children.iter().rev() {
stack.push(Step::Open(child, outer));
}
}
Kind::Text(text) => escape_into(out, text, false),
Kind::Element { name, space, attrs } => {
let name = open_tag(out, name, *space, outer, attrs);
let children = &self.nodes[id].children;
if children.is_empty() {
out.push_str("/>");
continue;
}
out.push('>');
stack.push(Step::Close(name.to_string()));
for &child in children.iter().rev() {
stack.push(Step::Open(child, *space));
}
}
}
}
}
}
fn open_tag<'a>(out: &mut String, name: &'a str, space: Space, outer: Space, attrs: &[(String, String)]) -> &'a str {
let name = if is_xml_name(name) { name } else { "span" };
out.push('<');
out.push_str(name);
if space != outer {
out.push_str(" xmlns=\"");
out.push_str(match space {
Space::Html => XHTML_NS,
Space::Svg => SVG_NS,
Space::Math => MATHML_NS,
});
out.push('"');
}
for (key, value) in attrs {
let plain = key.strip_prefix("xlink:").or_else(|| key.strip_prefix("xml:"));
if !is_xml_name(plain.unwrap_or(key)) || (plain.is_none() && key.contains(':')) {
continue;
}
out.push(' ');
out.push_str(key);
out.push_str("=\"");
escape_into(out, value, true);
out.push('"');
}
name
}
fn is_xml_name(name: &str) -> bool {
let mut chars = name.chars();
chars.next().is_some_and(|first| first.is_alphabetic() || first == '_')
&& chars.all(|ch| ch.is_alphanumeric() || matches!(ch, '-' | '_' | '.'))
}
fn escape_into(out: &mut String, text: &str, attribute: bool) {
for ch in text.chars() {
match ch {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' if attribute => out.push_str("""),
'\t' | '\n' | '\r' if attribute => out.push(' '),
'\t' | '\n' | '\r' => out.push(ch),
'\u{0}'..='\u{1f}' | '\u{fffe}' | '\u{ffff}' => {}
_ => out.push(ch),
}
}
}
#[cfg(test)]
mod tests {
use super::Dom;
#[test]
fn walk_and_text_outside() {
let dom = Dom::parse(
"<div id=a> <span id=b>x</span> <i id=c> </i><b id=d>y</b></div><p id=e> <span id=f>z</span></p>",
);
let (_, _, body) = dom.html_head_body();
let body = body.unwrap();
assert_eq!(dom.walk(body).collect::<Vec<_>>(), dom.descendants(body));
let id = |name: &str| dom.walk(0).find(|&node| dom.attr(node, "id") == Some(name)).unwrap();
let (div, span, bold, para, inner) = (id("a"), id("b"), id("d"), id("e"), id("f"));
assert!(dom.has_text_outside(div, |node| node == span));
assert!(!dom.has_text_outside(div, |node| node == span || node == bold));
assert!(!dom.has_text_outside(para, |node| node == inner));
assert!(dom.has_text_outside(para, |_| false));
}
#[test]
fn broken_html_becomes_well_formed_xhtml() {
let dom = Dom::parse(
"<!doctype html><title>T</title><p>a<b>b<i>c</b>d<br><img src=x.png alt='a\"b' @click=go>\
<svg viewBox='0 0 10 10'><image xlink:href='p.png'/></svg> ©<math><mi>x</mi></math>",
);
let (_, _, body) = dom.html_head_body();
let xhtml = dom.to_xhtml(&[], body.unwrap(), Some("en"));
assert!(roxmltree::Document::parse(&xhtml).is_ok(), "{xhtml}");
assert!(xhtml.contains("<b>b<i>c</i></b><i>d<br/>"), "{xhtml}");
assert!(xhtml.contains(r#"<img src="x.png" alt="a"b"/>"#), "{xhtml}");
assert!(
xhtml
.contains(r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><image xlink:href="p.png"/>"#),
"{xhtml}"
);
assert!(
xhtml.contains("\u{a0}\u{a9}<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>x</mi></math>"),
"{xhtml}"
);
}
#[test]
fn detach_all_takes_nodes_out_once_and_keeps_the_others_in_order() {
let mut dom = Dom::parse("<body><p>a</p><p>b</p><p>c</p><p>d</p></body>");
let (_, _, body) = dom.html_head_body();
let body = body.unwrap();
let paragraphs = dom.find_all(body, "p");
dom.detach_all(&[paragraphs[0], paragraphs[2]]);
assert_eq!(dom.clean_text(body), "bd");
assert!(!dom.attached(paragraphs[0]) && !dom.attached(paragraphs[2]));
assert!(dom.attached(paragraphs[1]) && dom.attached(paragraphs[3]));
}
}