use scraper::node::Element;
/// What kind of markdown wrapper an element produces.
pub(crate) enum ElementAction {
/// Skip this element and all its children entirely.
Skip,
/// Render children only, no wrapper (transparent element).
Transparent,
/// Block element with specific rendering.
Block(BlockKind),
/// Inline element with specific rendering.
Inline(InlineKind),
}
#[derive(Clone, Copy)]
pub(crate) enum BlockKind {
Paragraph,
Heading(u8),
Blockquote,
UnorderedList,
OrderedList,
ListItem,
PreFormatted,
HorizontalRule,
Table,
Div,
}
#[derive(Clone, Copy)]
pub(crate) enum InlineKind {
Bold,
Italic,
Strikethrough,
Code,
Link,
Image,
LineBreak,
Superscript,
Subscript,
}
/// Classify an HTML element into the action pter should take.
pub(crate) fn classify(el: &Element) -> ElementAction {
match el.name() {
// Skip entirely
"script" | "style" | "head" | "meta" | "link" | "title" | "noscript" => ElementAction::Skip,
// Block elements
"p" => ElementAction::Block(BlockKind::Paragraph),
"h1" => ElementAction::Block(BlockKind::Heading(1)),
"h2" => ElementAction::Block(BlockKind::Heading(2)),
"h3" => ElementAction::Block(BlockKind::Heading(3)),
"h4" => ElementAction::Block(BlockKind::Heading(4)),
"h5" => ElementAction::Block(BlockKind::Heading(5)),
"h6" => ElementAction::Block(BlockKind::Heading(6)),
"blockquote" => ElementAction::Block(BlockKind::Blockquote),
"ul" | "menu" => ElementAction::Block(BlockKind::UnorderedList),
"ol" => ElementAction::Block(BlockKind::OrderedList),
"li" => ElementAction::Block(BlockKind::ListItem),
"pre" => ElementAction::Block(BlockKind::PreFormatted),
"hr" => ElementAction::Block(BlockKind::HorizontalRule),
"table" => ElementAction::Block(BlockKind::Table),
// Table sub-elements are handled by the Table block handler, not individually
"thead" | "tbody" | "tfoot" | "tr" | "td" | "th" | "caption" | "colgroup" | "col" => {
ElementAction::Transparent
}
"div" | "section" | "article" | "main" | "header" | "footer" | "nav" | "aside"
| "figure" | "figcaption" | "details" | "summary" => ElementAction::Block(BlockKind::Div),
// Inline elements
"strong" | "b" => ElementAction::Inline(InlineKind::Bold),
"em" | "i" => ElementAction::Inline(InlineKind::Italic),
"del" | "s" | "strike" => ElementAction::Inline(InlineKind::Strikethrough),
"code" | "tt" => ElementAction::Inline(InlineKind::Code),
"a" => ElementAction::Inline(InlineKind::Link),
"img" => ElementAction::Inline(InlineKind::Image),
"br" => ElementAction::Inline(InlineKind::LineBreak),
"sup" => ElementAction::Inline(InlineKind::Superscript),
"sub" => ElementAction::Inline(InlineKind::Subscript),
// Everything else: transparent (render children)
_ => ElementAction::Transparent,
}
}
/// Check if an element is a tracking pixel.
/// Returns true if it should be skipped.
pub(crate) fn is_tracking_pixel(el: &Element) -> bool {
let width = el.attr("width");
let height = el.attr("height");
// 1x1 or 0x0 images
if matches!(width, Some("1" | "0")) || matches!(height, Some("1" | "0")) {
return true;
}
// No src attribute
let Some(src) = el.attr("src") else {
return true;
};
// Empty or data:image/gif (common transparent pixel)
if src.is_empty() {
return true;
}
if src.starts_with("data:image/gif;base64,R0lGOD") {
return true;
}
// Check inline style for tiny dimensions
if let Some(style) = el.attr("style") {
let style_lower = style.to_lowercase();
if style_lower.contains("width:1px")
|| style_lower.contains("width: 1px")
|| style_lower.contains("width:0")
|| style_lower.contains("height:1px")
|| style_lower.contains("height: 1px")
|| style_lower.contains("height:0")
|| style_lower.contains("display:none")
|| style_lower.contains("display: none")
{
return true;
}
}
false
}
/// Check if an element is hidden via inline style.
///
/// Catches display:none, visibility:hidden, and spacer tricks
/// like font-size:0 or line-height:0 (commonly used in email templates).
pub(crate) fn is_hidden(el: &Element) -> bool {
if let Some(style) = el.attr("style") {
let s = style.to_lowercase();
if s.contains("display:none")
|| s.contains("display: none")
|| s.contains("visibility:hidden")
|| s.contains("visibility: hidden")
|| s.contains("font-size:0")
|| s.contains("font-size: 0")
|| s.contains("line-height:0")
|| s.contains("line-height: 0")
|| (s.contains("height:0") && s.contains("overflow:hidden"))
|| (s.contains("height: 0") && s.contains("overflow: hidden"))
|| s.contains("max-height:0")
|| s.contains("max-height: 0")
{
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
use scraper::{Html, Selector};
fn classify_tag(tag: &str) -> ElementAction {
let html = format!("<{tag}>{tag}>");
let doc = Html::parse_fragment(&html);
let sel = Selector::parse(tag).unwrap();
let el = doc.select(&sel).next().unwrap();
classify(el.value())
}
fn img_is_pixel(attrs: &str) -> bool {
let html = format!("