#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HNode {
Element { tag: String, attrs: Vec<(String, String)>, children: Vec<HNode> },
Text(String),
}
pub const ALLOWED_TAGS: &[&str] = &[
"a", "abbr", "b", "blockquote", "br", "code", "dd", "del", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "kbd", "li",
"mark", "ol", "p", "pre", "q", "s", "small", "span", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "u", "ul",
];
const VOID: &[&str] = &["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr"];
const DROPPED_WHOLE: &[&str] = &["script", "style", "iframe", "object", "embed", "noscript", "template", "svg", "math", "textarea", "title", "head", "frameset", "frame", "noembed", "xmp", "plaintext"];
const RAW_TEXT: &[&str] = &["script", "style", "textarea", "title", "xmp", "noembed", "plaintext"];
const URL_ATTRS: &[&str] = &["href", "src"];
pub const ALLOWED_ATTRS: &[(&str, &str)] = &[
("*", "title"),
("a", "href"),
("code", "class"),
("img", "alt"),
("img", "height"),
("img", "src"),
("img", "width"),
("ol", "start"),
("pre", "class"),
("td", "colspan"),
("td", "rowspan"),
("th", "colspan"),
("th", "rowspan"),
];
pub fn allowed_attr(tag: &str, attr: &str) -> bool {
if house_tag(tag) {
return house_attr(attr);
}
ALLOWED_TAGS.contains(&tag) && ALLOWED_ATTRS.iter().any(|(t, a)| (*t == "*" || *t == tag) && *a == attr)
}
pub fn house_tag(tag: &str) -> bool {
tag.len() > 2 && tag.starts_with("m-") && tag[2..].bytes().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')
}
pub fn custom_tag(tag: &str) -> bool {
tag.contains('-') && tag.as_bytes().first().is_some_and(|c| c.is_ascii_lowercase()) && tag.bytes().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')
}
pub const HOUSE_ATTRS: &[&str] = &[
"benefit", "by", "commits", "cost", "date", "description", "heading", "kicker", "kind", "label", "labels", "lang", "name", "path", "picked",
"recommended", "ref", "size", "status", "ticket", "title", "tone", "value", "why",
];
pub const PROP_PREFIX: &str = "data-prop-";
pub fn prop_attr(attr: &str) -> bool {
attr.as_bytes().first().is_some_and(u8::is_ascii_lowercase)
&& attr.bytes().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')
&& !attr.starts_with("on")
&& !attr.starts_with("data-")
&& !matches!(attr, "style" | "href" | "src" | "id")
}
const BOUND_MARK: &[&str] = &["data-slot", "data-component", "data-version", "data-layout"];
pub fn house_attr(attr: &str) -> bool {
let data = attr.len() > 5
&& attr.starts_with("data-")
&& attr[5..].bytes().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')
&& !BOUND_MARK.contains(&attr);
data || HOUSE_ATTRS.contains(&attr)
}
fn url_ok(value: &str) -> bool {
let v = value.trim().to_ascii_lowercase();
v.starts_with("https://") || v.starts_with("http://") || v.starts_with("mailto:") || v.starts_with('#')
}
fn decode(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(i) = rest.find('&') {
out.push_str(&rest[..i]);
rest = &rest[i..];
let end = rest[1..].find(|c: char| c == ';' || c == '&' || c.is_whitespace() || c == '<').map(|j| j + 1);
let decoded = match end.filter(|e| rest.as_bytes().get(*e) == Some(&b';')) {
Some(e) => {
let name = &rest[1..e];
let ch = match name {
"amp" => Some('&'),
"lt" => Some('<'),
"gt" => Some('>'),
"quot" => Some('"'),
"apos" => Some('\''),
"nbsp" => Some('\u{a0}'),
n if n.starts_with("#x") || n.starts_with("#X") => u32::from_str_radix(&n[2..], 16).ok().and_then(char::from_u32),
n if n.starts_with('#') => n[1..].parse::<u32>().ok().and_then(char::from_u32),
_ => None,
};
ch.map(|c| (c, e + 1))
}
None => None,
};
match decoded {
Some((c, len)) => {
out.push(c);
rest = &rest[len..];
}
None => {
out.push('&');
rest = &rest[1..];
}
}
}
out.push_str(rest);
out
}
struct Open {
name: String,
tag: String,
attrs: Vec<(String, String)>,
children: Vec<HNode>,
}
fn push_text(stack: &mut [Open], root: &mut Vec<HNode>, text: String) {
if text.is_empty() {
return;
}
let into = match stack.last_mut() {
Some(o) => &mut o.children,
None => root,
};
if let Some(HNode::Text(t)) = into.last_mut() {
t.push_str(&text);
} else {
into.push(HNode::Text(text));
}
}
fn close(stack: &mut Vec<Open>, root: &mut Vec<HNode>) {
if let Some(o) = stack.pop() {
let node = HNode::Element { tag: o.tag, attrs: o.attrs, children: o.children };
match stack.last_mut() {
Some(p) => p.children.push(node),
None => root.push(node),
}
}
}
fn tag_name(s: &str) -> Option<(String, usize)> {
let b = s.as_bytes();
if !b.first()?.is_ascii_alphabetic() {
return None;
}
let n = b.iter().position(|c| !(c.is_ascii_alphanumeric() || *c == b'-' || *c == b':')).unwrap_or(b.len());
Some((s[..n].to_ascii_lowercase(), n))
}
fn attributes(s: &str) -> (Vec<(String, String)>, bool, usize) {
let b = s.as_bytes();
let mut i = 0;
let mut attrs = Vec::new();
loop {
while i < b.len() && (b[i].is_ascii_whitespace() || b[i] == b'/') {
if b[i] == b'/' && b.get(i + 1) == Some(&b'>') {
return (attrs, true, i + 2);
}
i += 1;
}
if i >= b.len() {
return (attrs, false, i);
}
if b[i] == b'>' {
return (attrs, false, i + 1);
}
let start = i;
while i < b.len() && !(b[i].is_ascii_whitespace() || b[i] == b'=' || b[i] == b'>' || b[i] == b'/') {
i += 1;
}
let name = s[start..i].to_ascii_lowercase();
while i < b.len() && b[i].is_ascii_whitespace() {
i += 1;
}
let mut value = String::new();
if b.get(i) == Some(&b'=') {
i += 1;
while i < b.len() && b[i].is_ascii_whitespace() {
i += 1;
}
match b.get(i) {
Some(&q) if q == b'"' || q == b'\'' => {
let end = s[i + 1..].find(q as char).map(|e| i + 1 + e).unwrap_or(b.len());
value = decode(&s[i + 1..end]);
i = (end + 1).min(b.len());
}
_ => {
let vs = i;
while i < b.len() && !(b[i].is_ascii_whitespace() || b[i] == b'>') {
i += 1;
}
value = decode(&s[vs..i]);
}
}
}
if !name.is_empty() {
attrs.push((name, value));
}
}
}
fn skip_to_end(s: &str, tag: &str) -> usize {
let lower = s.to_ascii_lowercase();
let (open, close) = (format!("<{tag}"), format!("</{tag}"));
let raw = RAW_TEXT.contains(&tag);
let find = |from: usize, pat: &str| -> Option<usize> {
let mut at = from;
while let Some(j) = lower[at..].find(pat) {
let k = at + j;
match lower.as_bytes().get(k + pat.len()) {
Some(c) if c.is_ascii_alphanumeric() || *c == b'-' || *c == b':' => at = k + pat.len(),
_ => return Some(k),
}
}
None
};
let mut depth = 1;
let mut i = 0;
loop {
let next_close = find(i, &close);
let next_open = if raw { None } else { find(i, &open) };
match (next_open, next_close) {
(Some(o), Some(c)) if o < c => {
depth += 1;
i = o + open.len();
}
(_, Some(c)) => {
depth -= 1;
let gt = lower[c..].find('>').map(|g| c + g + 1).unwrap_or(s.len());
if depth == 0 {
return gt;
}
i = gt;
}
(_, None) => return s.len(),
}
}
}
pub fn sanitize(html: &str) -> Vec<HNode> {
let mut root = Vec::new();
let mut stack: Vec<Open> = Vec::new();
let mut text = String::new();
let mut i = 0;
let s = html;
while i < s.len() {
let rest = &s[i..];
let Some(lt) = rest.find('<') else {
text.push_str(rest);
break;
};
text.push_str(&rest[..lt]);
i += lt;
let rest = &s[i..];
if rest.starts_with("<!--") {
push_text(&mut stack, &mut root, decode(&std::mem::take(&mut text)));
i += rest.find("-->").map(|e| e + 3).unwrap_or(rest.len());
continue;
}
if rest.starts_with("<!") || rest.starts_with("<?") {
push_text(&mut stack, &mut root, decode(&std::mem::take(&mut text)));
i += rest.find('>').map(|e| e + 1).unwrap_or(rest.len());
continue;
}
if let Some((name, n)) = rest.strip_prefix("</").and_then(tag_name) {
push_text(&mut stack, &mut root, decode(&std::mem::take(&mut text)));
i += 2 + n + rest[2 + n..].find('>').map(|e| e + 1).unwrap_or(rest.len() - 2 - n);
if let Some(at) = stack.iter().rposition(|o| o.name == name) {
while stack.len() > at {
close(&mut stack, &mut root);
}
}
continue;
}
let Some((name, n)) = tag_name(&rest[1..]) else {
text.push('<');
i += 1;
continue;
};
push_text(&mut stack, &mut root, decode(&std::mem::take(&mut text)));
let (attrs, self_closed, used) = attributes(&rest[1 + n..]);
i += 1 + n + used;
if DROPPED_WHOLE.contains(&name.as_str()) {
if !VOID.contains(&name.as_str()) && !self_closed {
i += skip_to_end(&s[i..], &name);
}
continue;
}
let custom = !house_tag(&name) && !ALLOWED_TAGS.contains(&name.as_str()) && custom_tag(&name);
let (name, tag) = if custom { (name.clone(), "m-el".to_string()) } else { (name.clone(), name) };
let house = house_tag(&tag);
if !ALLOWED_TAGS.contains(&tag.as_str()) && !house {
continue;
}
let attrs: Vec<(String, String)> = if house {
let named = attrs.iter().any(|(a, _)| a == "data-id");
let mut kept: Vec<(String, String)> = Vec::new();
if custom {
kept.push(("data-tag".to_string(), name.clone()));
}
for (a, v) in attrs {
let a = if a == "id" && !named { "data-id".to_string() } else { a };
if house_attr(&a) && !kept.iter().any(|(k, _)| *k == a) && !(custom && a == "data-tag") {
kept.push((a, v));
} else if prop_attr(&a) {
let named = format!("{PROP_PREFIX}{a}");
if !kept.iter().any(|(k, _)| *k == named) {
kept.push((named, v));
}
}
}
kept
} else {
attrs.into_iter().filter(|(a, v)| allowed_attr(&name, a) && (!URL_ATTRS.contains(&a.as_str()) || url_ok(v))).collect()
};
if VOID.contains(&tag.as_str()) || self_closed {
let node = HNode::Element { tag, attrs, children: Vec::new() };
match stack.last_mut() {
Some(p) => p.children.push(node),
None => root.push(node),
}
} else {
stack.push(Open { name, tag, attrs, children: Vec::new() });
}
}
push_text(&mut stack, &mut root, decode(&text));
while !stack.is_empty() {
close(&mut stack, &mut root);
}
root
}
fn escape(text: &str, attr: bool) -> String {
let mut out = String::with_capacity(text.len());
for c in text.chars() {
match c {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' if attr => out.push_str("""),
c => out.push(c),
}
}
out
}
pub fn to_html(nodes: &[HNode]) -> String {
let mut out = String::new();
for n in nodes {
match n {
HNode::Text(t) => out.push_str(&escape(t, false)),
HNode::Element { tag, attrs, children } => {
out.push('<');
out.push_str(tag);
for (k, v) in attrs {
out.push_str(&format!(" {k}=\"{}\"", escape(v, true)));
}
out.push('>');
if !VOID.contains(&tag.as_str()) {
out.push_str(&to_html(children));
out.push_str(&format!("</{tag}>"));
}
}
}
}
out
}
pub fn markdown(text: &str) -> String {
use pulldown_cmark::{html, Options, Parser};
let mut out = String::new();
html::push_html(&mut out, Parser::new_ext(text, Options::ENABLE_TABLES | Options::ENABLE_STRIKETHROUGH));
out
}
#[cfg(test)]
mod tests;