use crate::converter::dom_context::{DomContext, TagInfo};
use crate::converter::main_helpers::{is_ascii_whitespace_only, is_inline_element};
use crate::converter::utility::attributes::{attribute_matches_any, element_has_navigation_hint};
use crate::options::ConversionOptions;
pub fn inline_ancestor_allows_block(tag_name: &str) -> bool {
matches!(tag_name, "a" | "ins" | "del")
}
#[derive(Clone, Copy)]
struct MisnestState {
inside_anchor: bool,
inside_preformatted: bool,
blocked_by_inline_ancestor: bool,
p_ancestor_state: bool,
list_item_ancestor_state: bool,
cell_ancestor_state: bool,
}
impl MisnestState {
const ROOT: Self = Self {
inside_anchor: false,
inside_preformatted: false,
blocked_by_inline_ancestor: false,
p_ancestor_state: false,
list_item_ancestor_state: false,
cell_ancestor_state: false,
};
}
fn node_is_misnested(info: &TagInfo, state: MisnestState, self_inside_preformatted: bool) -> bool {
(info.name == "a" && state.inside_anchor && !state.inside_preformatted)
|| (matches!(info.name.as_str(), "td" | "tr" | "th") && state.p_ancestor_state)
|| (info.is_block && !self_inside_preformatted && state.blocked_by_inline_ancestor)
|| (matches!(info.name.as_str(), "li" | "dt" | "dd") && state.list_item_ancestor_state)
|| (info.name == "tr" && state.cell_ancestor_state)
}
fn child_misnest_state(info: &TagInfo, state: MisnestState, self_inside_preformatted: bool) -> MisnestState {
MisnestState {
inside_anchor: state.inside_anchor || info.name == "a",
inside_preformatted: self_inside_preformatted,
blocked_by_inline_ancestor: state.blocked_by_inline_ancestor
|| (is_inline_element(&info.name) && !inline_ancestor_allows_block(&info.name)),
p_ancestor_state: p_ancestor_state_for(&info.name, state.p_ancestor_state),
list_item_ancestor_state: list_item_ancestor_state_for(&info.name, state.list_item_ancestor_state),
cell_ancestor_state: cell_ancestor_state_for(&info.name, state.cell_ancestor_state),
}
}
pub fn has_inline_block_misnest(dom_ctx: &DomContext, parser: &tl::Parser) -> bool {
let mut stack: Vec<(tl::NodeHandle, MisnestState)> = dom_ctx
.root_children
.iter()
.map(|handle| (*handle, MisnestState::ROOT))
.collect();
while let Some((handle, state)) = stack.pop() {
let node_id = handle.get_inner();
if !matches!(handle.get(parser), Some(tl::Node::Tag(_))) {
continue;
}
let Some(info) = dom_ctx.tag_info(node_id, parser) else {
continue;
};
let self_inside_preformatted = state.inside_preformatted || matches!(info.name.as_str(), "pre" | "code");
if node_is_misnested(info, state, self_inside_preformatted) {
return true;
}
if let Some(children) = dom_ctx.children_of(node_id) {
if info.name == "table"
&& children
.iter()
.any(|child| is_foster_parenting_candidate(*child, parser, dom_ctx))
{
return true;
}
if is_row_context_with_misplaced_element(info, children, parser, dom_ctx) {
return true;
}
let child_state = child_misnest_state(info, state, self_inside_preformatted);
stack.extend(children.iter().map(|child| (*child, child_state)));
}
}
false
}
fn is_foster_parenting_candidate(child: tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
match child.get(parser) {
Some(tl::Node::Raw(bytes)) => {
let raw = bytes.as_utf8_str();
let decoded = crate::text::decode_html_entities_cow(raw.as_ref());
!is_ascii_whitespace_only(&decoded)
}
Some(tl::Node::Tag(_)) => dom_ctx
.tag_name_for(child, parser)
.is_some_and(|name| !is_table_structural_child(name.as_ref())),
_ => false,
}
}
fn is_table_structural_child(tag_name: &str) -> bool {
matches!(
tag_name,
"caption" | "colgroup" | "col" | "thead" | "tbody" | "tfoot" | "tr" | "row" | "style" | "script" | "template"
)
}
fn is_row_context_with_misplaced_element(
info: &TagInfo,
children: &[tl::NodeHandle],
parser: &tl::Parser,
dom_ctx: &DomContext,
) -> bool {
let valid: fn(&str) -> bool = match info.name.as_str() {
"tr" | "row" => is_row_structural_child,
"thead" | "tbody" | "tfoot" => is_section_structural_child,
_ => return false,
};
children.iter().any(|child| {
matches!(child.get(parser), Some(tl::Node::Tag(_)))
&& dom_ctx
.tag_name_for(*child, parser)
.is_some_and(|name| !valid(name.as_ref()))
})
}
fn is_row_structural_child(tag_name: &str) -> bool {
matches!(tag_name, "td" | "th" | "cell" | "script" | "style" | "template")
}
fn is_section_structural_child(tag_name: &str) -> bool {
matches!(tag_name, "tr" | "row" | "script" | "style" | "template")
}
fn p_ancestor_state_for(tag_name: &str, inherited: bool) -> bool {
if tag_name == "p" {
true
} else if matches!(tag_name, "table" | "body" | "html") {
false
} else {
inherited
}
}
fn list_item_ancestor_state_for(tag_name: &str, inherited: bool) -> bool {
if matches!(tag_name, "li" | "dt" | "dd") {
true
} else if matches!(tag_name, "ul" | "ol" | "dl" | "table" | "body" | "html") {
false
} else {
inherited
}
}
fn cell_ancestor_state_for(tag_name: &str, inherited: bool) -> bool {
if matches!(tag_name, "td" | "th") {
true
} else if matches!(tag_name, "table" | "thead" | "tbody" | "tfoot" | "tr") {
false
} else {
inherited
}
}
pub fn should_drop_for_preprocessing(
tag_name: &str,
tag: &tl::HTMLTag,
options: &ConversionOptions,
is_page_header: bool,
) -> bool {
use crate::options::PreprocessingPreset;
if !options.preprocessing.enabled {
return false;
}
let preset = options.preprocessing.preset;
if preset == PreprocessingPreset::Minimal {
return false;
}
if options.preprocessing.remove_forms && tag_name == "form" {
return true;
}
let is_aggressive = preset == PreprocessingPreset::Aggressive;
if is_aggressive && tag_name == "noscript" {
return true;
}
if !options.preprocessing.remove_navigation {
return false;
}
let has_nav_hint = element_has_navigation_hint(tag);
if tag_name == "nav" {
return true;
}
if tag_name == "header" {
return is_page_header || has_nav_hint;
}
if tag_name == "footer" || tag_name == "aside" {
return is_aggressive || has_nav_hint;
}
if is_aggressive && has_nav_hint {
return true;
}
if is_aggressive {
if element_has_noise_hint(tag) {
return true;
}
}
false
}
pub fn is_page_header(node_handle: &tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
let mut ancestor = dom_ctx.parent_of(node_handle.get_inner());
let mut in_document_body = false;
while let Some(node_id) = ancestor {
if let Some(info) = dom_ctx.tag_info(node_id, parser) {
if matches!(info.name.as_str(), "article" | "section" | "main") {
return false;
}
in_document_body |= info.name == "body";
}
ancestor = dom_ctx.parent_of(node_id);
}
in_document_body
}
fn element_has_noise_hint(tag: &tl::HTMLTag) -> bool {
const NOISE_KEYWORDS: &[&str] = &[
"cookie",
"consent",
"gdpr",
"banner",
"advertisement",
"ad-container",
"advert",
"social-share",
"share-buttons",
"popup",
"modal-overlay",
"newsletter-signup",
];
attribute_matches_any(tag, "class", NOISE_KEYWORDS) || attribute_matches_any(tag, "id", NOISE_KEYWORDS)
}