use std::borrow::Cow;
use crate::converter::DomContext;
use crate::converter::utility::content::normalized_tag_name;
pub fn decoded_attribute<'a>(tag: &'a crate::dom::HTMLTag<'a>, name: &'a str) -> Option<Cow<'a, str>> {
let raw = tag.attributes().get(name).flatten()?.as_utf8_str();
Some(match raw {
Cow::Borrowed(borrowed) => crate::text::decode_attribute_value_cow(borrowed),
Cow::Owned(owned) => Cow::Owned(crate::text::decode_attribute_value_cow(&owned).into_owned()),
})
}
fn style_sets_display_or_white_space(style: &[u8]) -> bool {
style.split(|&byte| byte == b';').any(|declaration| {
declaration.iter().position(|&byte| byte == b':').is_some_and(|colon| {
let name = declaration[..colon].trim_ascii();
name.eq_ignore_ascii_case(b"display") || name.eq_ignore_ascii_case(b"white-space")
})
})
}
pub fn style_attribute_sets_display_or_white_space(source_value: &[u8]) -> bool {
if !source_value.contains(&b'&') {
return style_sets_display_or_white_space(source_value);
}
let text = String::from_utf8_lossy(source_value);
style_sets_display_or_white_space(crate::text::decode_attribute_value_cow(&text).as_bytes())
}
pub fn tag_has_main_semantics(tag: &crate::dom::HTMLTag) -> bool {
if let Some(Some(role)) = tag.attributes().get("role") {
let lowered = role.as_utf8_str().to_ascii_lowercase();
if matches!(lowered.as_str(), "main" | "article" | "document" | "region") {
return true;
}
}
if let Some(Some(class_bytes)) = tag.attributes().get("class") {
let class_value = class_bytes.as_utf8_str().to_ascii_lowercase();
const MAIN_CLASS_HINTS: &[&str] = &[
"mw-body",
"mw-parser-output",
"content-body",
"content-container",
"article-body",
"article-content",
"main-content",
"page-content",
"entry-content",
"post-content",
"document-body",
];
if MAIN_CLASS_HINTS.iter().any(|hint| class_value.contains(hint)) {
return true;
}
}
false
}
pub const NAV_KEYWORDS: &[&str] = &[
"nav",
"navigation",
"navbar",
"breadcrumbs",
"breadcrumb",
"toc",
"sidebar",
"sidenav",
"menu",
"menubar",
"mainmenu",
"subnav",
"tabs",
"tablist",
"toolbar",
"pager",
"pagination",
"skipnav",
"skip-link",
"skiplinks",
"site-nav",
"site-menu",
"site-header",
"site-footer",
"topbar",
"bottombar",
"masthead",
"vector-nav",
"vector-header",
"vector-footer",
];
pub fn element_has_navigation_hint(tag: &crate::dom::HTMLTag) -> bool {
if attribute_matches_any(tag, "role", &["navigation", "menubar", "tablist", "toolbar"]) {
return true;
}
if attribute_contains_any(
tag,
"aria-label",
&["navigation", "menu", "contents", "table of contents", "toc"],
) {
return true;
}
attribute_matches_any(tag, "class", NAV_KEYWORDS) || attribute_matches_any(tag, "id", NAV_KEYWORDS)
}
pub fn attribute_matches_any(tag: &crate::dom::HTMLTag, attr: &str, keywords: &[&str]) -> bool {
let Some(attr_value) = tag.attributes().get(attr) else {
return false;
};
let Some(value) = attr_value else {
return false;
};
let raw = value.as_utf8_str();
raw.split_whitespace()
.map(|token| {
token
.chars()
.map(|c| match c {
'_' | ':' | '.' | '/' => '-',
_ => c,
})
.collect::<String>()
.to_ascii_lowercase()
})
.filter(|token| !token.is_empty())
.any(|token| keywords.iter().any(|kw| token == *kw))
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn attribute_contains_any(tag: &crate::dom::HTMLTag, attr: &str, keywords: &[&str]) -> bool {
let Some(attr_value) = tag.attributes().get(attr) else {
return false;
};
let Some(value) = attr_value else {
return false;
};
let lower = value.as_utf8_str().to_ascii_lowercase();
keywords.iter().any(|kw| lower.contains(*kw))
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn has_semantic_content_ancestor(
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
dom_ctx: &DomContext,
) -> bool {
let mut current_id = node_handle.get_inner();
while let Some(parent_id) = dom_ctx.parent_of(current_id) {
if let Some(parent_info) = dom_ctx.tag_info(parent_id, parser) {
if matches!(parent_info.name.as_str(), "main" | "article" | "section") {
return true;
}
}
if let Some(parent_handle) = dom_ctx.node_handle(parent_id) {
if let Some(crate::dom::Node::Tag(parent_tag)) = parent_handle.get(parser) {
let parent_name = normalized_tag_name(parent_tag.name().as_utf8_str());
if matches!(parent_name.as_ref(), "main" | "article" | "section") {
return true;
}
if tag_has_main_semantics(parent_tag) {
return true;
}
}
}
current_id = parent_id;
}
false
}
#[cfg(test)]
mod tests {
use super::style_sets_display_or_white_space;
#[test]
fn a_style_with_a_display_or_a_white_space_declaration_sets_one() {
for style in [
"display:inline-block",
"display:block",
"white-space:pre",
"white-space: pre-wrap",
"DISPLAY:none",
"White-Space:PRE",
"display : inline",
" display\t:\tinline",
"\n white-space \n : pre",
"color:red;display:block",
"color:red; white-space:pre;",
";;display:block",
"display:",
] {
assert!(style_sets_display_or_white_space(style.as_bytes()), "{style:?}");
}
}
#[test]
fn a_style_with_no_display_and_no_white_space_declaration_sets_none() {
for style in [
"",
" ",
";",
"color:red",
"color:red;",
"display",
"display;color:red",
"--display:block",
"text-white-space-x:pre",
"white-space-collapse:preserve",
"displays:block",
"white space:pre",
"color:display",
"content:\"display:block\"",
":display",
] {
assert!(!style_sets_display_or_white_space(style.as_bytes()), "{style:?}");
}
}
#[test]
fn a_semicolon_in_a_quoted_value_starts_a_declaration() {
assert!(style_sets_display_or_white_space(b"content:\"a;display:x\""));
}
#[test]
fn a_source_value_is_decoded_before_it_is_read() {
use super::style_attribute_sets_display_or_white_space;
for value in [
"white-space:pre",
"white-space:pre",
"display:inline-block",
"display:inline-block",
"display:inline-block",
"display:inline-block",
"DISPLAY:BLOCK",
"color:red;display:block",
" display	:block",
"a&b:c;display:block",
] {
assert!(
style_attribute_sets_display_or_white_space(value.as_bytes()),
"{value:?}"
);
}
for value in [
"color:red",
"color:red",
"display-x:1",
"white-space&#58;pre",
"display&colon;block",
"&display:block",
"<!-- display:block -->:",
] {
assert!(
!style_attribute_sets_display_or_white_space(value.as_bytes()),
"{value:?}"
);
}
}
}