use std::borrow::Cow;
use super::raw_text::{PageReads, PassRead, opens_a_tag, removed_element_name, skip_opaque_region, track_svg_tag};
pub(super) fn find_subslice(haystack: &[u8], from: usize, needle: &[u8]) -> Option<usize> {
if from >= haystack.len() {
return None;
}
memchr::memmem::find(&haystack[from..], needle).map(|off| from + off)
}
pub fn find_html_comment_end(bytes: &[u8], from: usize) -> Option<usize> {
let rest = bytes.get(from..)?;
memchr::memchr_iter(b'>', rest).find_map(|offset| {
let preceding = &rest[..offset];
(preceding.ends_with(b"--") || preceding.ends_with(b"--!")).then_some(from + offset + 1)
})
}
pub fn normalize_bogus_comment_endings(input: &str) -> Cow<'_, str> {
let bytes = input.as_bytes();
let len = bytes.len();
if len < 5 || !bytes.windows(4).any(|w| w == b"<!--") {
return Cow::Borrowed(input);
}
let mut idx = 0;
let mut last = 0;
let mut output: Option<String> = None;
let mut reads = PageReads::new(|_| {});
while idx + 3 < len {
if bytes[idx] != b'<' {
idx += 1;
continue;
}
if !(bytes[idx] == b'<' && bytes[idx + 1] == b'!' && bytes[idx + 2] == b'-' && bytes[idx + 3] == b'-') {
if opens_a_tag(bytes, idx) {
let Some(tag_end) = reads.tag_end(bytes, idx) else {
break;
};
idx = skip_opaque_region(bytes, idx).unwrap_or(tag_end);
} else {
idx = skip_opaque_region(bytes, idx).unwrap_or(idx + 1);
}
continue;
}
idx += 4;
if bytes.get(idx) == Some(&b'>') {
let out = output.get_or_insert_with(|| String::with_capacity(len));
out.push_str(&input[last..idx - 4]);
out.push_str("<!---->");
idx += 1;
last = idx;
continue;
}
if bytes.get(idx) == Some(&b'-') && bytes.get(idx + 1) == Some(&b'>') {
let out = output.get_or_insert_with(|| String::with_capacity(len));
out.push_str(&input[last..idx - 4]);
out.push_str("<!---->");
idx += 2;
last = idx;
continue;
}
let Some(end) = find_html_comment_end(bytes, idx) else {
break;
};
let ends_with_bang = bytes[end - 2] == b'!';
let dash_end = end - 1 - usize::from(ends_with_bang);
let mut close_start = dash_end;
while close_start > idx && bytes[close_start - 1] == b'-' {
close_start -= 1;
}
if ends_with_bang || dash_end - close_start > 2 {
let out = output.get_or_insert_with(|| String::with_capacity(len));
out.push_str(&input[last..close_start]);
out.push_str("-->");
last = end;
}
idx = end;
}
match output {
Some(mut out) => {
if last < len {
out.push_str(&input[last..]);
}
Cow::Owned(out)
}
None => Cow::Borrowed(input),
}
}
pub fn normalize_split_closing_tags(input: &str) -> Cow<'_, str> {
let bytes = input.as_bytes();
let len = bytes.len();
if len < 4 || !bytes.contains(&b'\n') {
return Cow::Borrowed(input);
}
let mut idx = 0;
let mut last = 0;
let mut output: Option<String> = None;
while idx + 2 < len {
let Some(offset) = memchr::memchr(b'<', &bytes[idx..]) else {
break;
};
idx += offset;
if bytes.get(idx + 1) != Some(&b'/') {
idx += 1;
continue;
}
let name_start = idx + 2;
let mut name_end = name_start;
while name_end < len && (bytes[name_end].is_ascii_alphanumeric() || bytes[name_end] == b'-') {
name_end += 1;
}
if name_end == name_start {
idx += 1;
continue;
}
let ws_start = name_end;
let mut ws_end = ws_start;
let mut has_newline = false;
while ws_end < len && bytes[ws_end].is_ascii_whitespace() {
if bytes[ws_end] == b'\n' || bytes[ws_end] == b'\r' {
has_newline = true;
}
ws_end += 1;
}
if !has_newline || ws_end >= len || bytes[ws_end] != b'>' {
idx += 1;
continue;
}
let tag_name = &input[name_start..name_end];
let out = output.get_or_insert_with(|| String::with_capacity(len));
out.push_str(&input[last..idx]);
out.push_str("</");
out.push_str(tag_name);
out.push('>');
idx = ws_end + 1;
last = idx;
}
match output {
Some(mut out) => {
if last < len {
out.push_str(&input[last..]);
}
Cow::Owned(out)
}
None => Cow::Borrowed(input),
}
}
fn collapse_empty_comment(
input: &str,
bytes: &[u8],
idx: usize,
last: usize,
output: &mut Option<String>,
) -> Option<usize> {
const EMPTY_COMMENT: &[u8] = b"<!---->";
if !bytes[idx..].starts_with(EMPTY_COMMENT) {
return None;
}
let out = output.get_or_insert_with(|| String::with_capacity(input.len()));
out.push_str(&input[last..idx]);
out.push_str("<!-- -->");
Some(idx + EMPTY_COMMENT.len())
}
fn normalize_self_closing_void(
input: &str,
bytes: &[u8],
idx: usize,
last: usize,
output: &mut Option<String>,
) -> Option<usize> {
const SELF_CLOSING: [(&[u8], &str); 3] = [(b"<br/>", "<br>"), (b"<hr/>", "<hr>"), (b"<img/>", "<img>")];
for (pattern, replacement) in &SELF_CLOSING {
if bytes[idx..].starts_with(pattern) {
let out = output.get_or_insert_with(|| String::with_capacity(input.len()));
out.push_str(&input[last..idx]);
out.push_str(replacement);
return Some(idx + pattern.len());
}
}
None
}
fn normalize_void_tag_case<C: FnMut(PassRead)>(
input: &str,
idx: usize,
last: usize,
last_gt: usize,
reads: &mut PageReads<C>,
output: &mut Option<String>,
) -> Option<usize> {
let bytes = input.as_bytes();
let name_start = idx + 1;
if !bytes.get(name_start)?.is_ascii_alphabetic() {
return None;
}
let name_end = crate::converter::main_helpers::scan_tag_name_end(bytes, name_start);
let name = &bytes[name_start..name_end];
if !name.iter().any(u8::is_ascii_uppercase) || !crate::converter::main_helpers::is_html5_void_element(name) {
return None;
}
let out = output.get_or_insert_with(|| String::with_capacity(input.len()));
out.push_str(&input[last..idx]);
out.push('<');
out.push_str(&input[name_start..name_end].to_ascii_lowercase());
let whole = if idx < last_gt { reads.tag_end(bytes, idx) } else { None };
let end = whole.unwrap_or(name_end);
out.push_str(&input[name_end..end]);
Some(end)
}
fn strip_raw_text_tag_reopen<C: FnMut(PassRead)>(
input: &str,
idx: usize,
last: usize,
reads: &mut PageReads<C>,
output: &mut Option<String>,
) -> Option<usize> {
let bytes = input.as_bytes();
let (slot, open_end) = reads.raw_text_start_tag_end(bytes, idx)?;
let name = removed_element_name(slot)?;
if name == "script" && is_json_ld_script_open_tag(&input[idx..open_end]) {
return None;
}
let remove_end = reads
.raw_text_end_tag(bytes, open_end, slot)
.map_or(open_end, |(_, end)| end);
let out = output.get_or_insert_with(|| String::with_capacity(input.len()));
out.push_str(&input[last..open_end]);
out.push_str("</");
out.push_str(name);
out.push('>');
Some(remove_end)
}
fn strip_doctype(
input: &str,
bytes: &[u8],
idx: usize,
len: usize,
last: usize,
output: &mut Option<String>,
) -> Option<usize> {
const DOCTYPE: &[u8] = b"doctype";
if idx + 2 >= len || bytes[idx + 1] != b'!' {
return None;
}
let mut cursor = idx + 2;
while cursor < len && bytes[cursor].is_ascii_whitespace() {
cursor += 1;
}
if cursor + DOCTYPE.len() > len || !bytes[cursor..cursor + DOCTYPE.len()].eq_ignore_ascii_case(DOCTYPE) {
return None;
}
let end = find_tag_end(bytes, cursor + DOCTYPE.len())?;
let out = output.get_or_insert_with(|| String::with_capacity(input.len()));
out.push_str(&input[last..idx]);
Some(end)
}
fn looks_like_tag_start(bytes: &[u8], idx: usize, len: usize) -> bool {
if idx + 1 >= len {
return false;
}
match bytes[idx + 1] {
b'!' => {
idx + 2 < len
&& (bytes[idx + 2] == b'-'
|| bytes[idx + 2].is_ascii_alphabetic()
|| bytes[idx + 2].is_ascii_uppercase())
}
b'/' => idx + 2 < len && (bytes[idx + 2].is_ascii_alphabetic() || bytes[idx + 2].is_ascii_uppercase()),
b'?' => true,
c if c.is_ascii_alphabetic() || c.is_ascii_uppercase() => true,
_ => false,
}
}
pub fn preprocess_html(input: &str) -> Cow<'_, str> {
second_pass(input, |_| {})
}
pub(super) fn second_pass<C: FnMut(PassRead)>(input: &str, count: C) -> Cow<'_, str> {
let bytes = input.as_bytes();
let len = bytes.len();
if len == 0 {
return Cow::Borrowed(input);
}
let mut idx = 0;
let mut last = 0;
let mut output: Option<String> = None;
let mut svg_depth = 0usize;
let mut reads = PageReads::new(count);
let last_gt = memchr::memrchr(b'>', bytes).unwrap_or(0);
while idx < len {
if bytes[idx] != b'<' {
idx += 1;
continue;
}
if last_gt <= idx && looks_like_tag_start(bytes, idx, len) {
break;
}
let rewritten = collapse_empty_comment(input, bytes, idx, last, &mut output)
.or_else(|| normalize_self_closing_void(input, bytes, idx, last, &mut output));
if let Some(new_pos) = rewritten {
last = new_pos;
idx = new_pos;
continue;
}
if let Some(new_idx) = track_svg_tag(bytes, idx, &mut svg_depth) {
idx = new_idx;
continue;
}
let rewritten = if svg_depth == 0 {
normalize_void_tag_case(input, idx, last, last_gt, &mut reads, &mut output)
.or_else(|| strip_raw_text_tag_reopen(input, idx, last, &mut reads, &mut output))
.or_else(|| strip_doctype(input, bytes, idx, len, last, &mut output))
} else {
None
};
if let Some(new_pos) = rewritten {
last = new_pos;
idx = new_pos;
continue;
}
if !looks_like_tag_start(bytes, idx, len) {
let out = output.get_or_insert_with(|| String::with_capacity(input.len() + 4));
out.push_str(&input[last..idx]);
out.push_str("<");
idx += 1;
last = idx;
continue;
}
let whole = if svg_depth == 0 && idx < last_gt {
reads.comment_end(bytes, idx).or_else(|| reads.tag_end(bytes, idx))
} else {
None
};
idx = whole.unwrap_or(idx + 1);
}
if let Some(mut out) = output {
if last < len {
out.push_str(&input[last..]);
}
Cow::Owned(out)
} else {
Cow::Borrowed(input)
}
}
fn scan_unquoted_attr_value_end(bytes: &[u8], start: usize) -> usize {
let mut end = start;
while end < bytes.len() && !bytes[end].is_ascii_whitespace() && bytes[end] != b'>' {
end += 1;
}
end
}
pub fn is_json_ld_script_open_tag(tag: &str) -> bool {
let bytes = tag.as_bytes();
let mut idx = 0;
while idx + 4 <= bytes.len() {
if eq_ascii_case_insensitive(&bytes[idx..], b"type") {
let before_ok = idx == 0
|| bytes
.get(idx.saturating_sub(1))
.is_some_and(|b| b.is_ascii_whitespace() || *b == b'<' || *b == b'/');
let after_ok = bytes
.get(idx + 4)
.is_some_and(|b| b.is_ascii_whitespace() || *b == b'=');
if !before_ok || !after_ok {
idx += 4;
continue;
}
let mut i = idx + 4;
while bytes.get(i).is_some_and(u8::is_ascii_whitespace) {
i += 1;
}
if bytes.get(i) != Some(&b'=') {
idx += 4;
continue;
}
i += 1;
while bytes.get(i).is_some_and(u8::is_ascii_whitespace) {
i += 1;
}
if i >= bytes.len() {
return false;
}
let (value_start, value_end) = match bytes[i] {
b'"' | b'\'' => {
let quote = bytes[i];
let start = i + 1;
let mut end = start;
while end < bytes.len() && bytes[end] != quote {
end += 1;
}
(start, end)
}
_ => {
let start = i;
(start, scan_unquoted_attr_value_end(bytes, start))
}
};
let value = crate::text::decode_attribute_value_cow(&tag[value_start..value_end]);
return script_type(&value) != ScriptType::Removed;
}
idx += 1;
}
false
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ScriptType {
Removed,
KeptNotRead,
StructuredData,
}
pub fn script_type(type_value: &str) -> ScriptType {
const STRUCTURED_DATA: &[u8] = b"application/ld+json";
let media_type = type_value.split(';').next().unwrap_or(type_value).trim().as_bytes();
if media_type.eq_ignore_ascii_case(STRUCTURED_DATA) {
ScriptType::StructuredData
} else if eq_ascii_case_insensitive(media_type, STRUCTURED_DATA) {
ScriptType::KeptNotRead
} else {
ScriptType::Removed
}
}
#[inline]
pub fn eq_ascii_case_insensitive(haystack: &[u8], needle: &[u8]) -> bool {
if haystack.len() < needle.len() {
return false;
}
haystack
.iter()
.zip(needle.iter())
.all(|(a, b)| a.eq_ignore_ascii_case(b))
}
pub fn matches_tag_start(bytes: &[u8], mut start: usize, tag: &[u8]) -> bool {
if start >= bytes.len() {
return false;
}
if start + tag.len() > bytes.len() {
return false;
}
if !bytes[start..start + tag.len()].eq_ignore_ascii_case(tag) {
return false;
}
start += tag.len();
match bytes.get(start) {
Some(b'>' | b'/' | b' ' | b'\t' | b'\n' | b'\r') => true,
Some(_) => false,
None => true,
}
}
pub fn find_tag_end(bytes: &[u8], mut idx: usize) -> Option<usize> {
loop {
idx += memchr::memchr3(b'"', b'\'', b'>', bytes.get(idx..)?)?;
let delimiter = bytes[idx];
idx += 1;
if delimiter == b'>' {
return Some(idx);
}
idx += memchr::memchr(delimiter, &bytes[idx..])? + 1;
}
}
pub fn matches_end_tag_start(bytes: &[u8], start: usize, tag: &[u8]) -> bool {
if start >= bytes.len() || bytes[start] != b'/' {
return false;
}
matches_tag_start(bytes, start + 1, tag)
}
pub fn strip_html_cdata(input: &str) -> Cow<'_, str> {
if !input.contains("<![CDATA[") {
return Cow::Borrowed(input);
}
let bytes = input.as_bytes();
let mut cursor = 0;
let mut last = 0;
let mut foreign_depth = 0usize;
let mut output = None;
while let Some(offset) = memchr::memchr(b'<', &bytes[cursor..]) {
cursor += offset;
if bytes[cursor..].starts_with(b"<![CDATA[") {
let end = cdata_end(bytes, cursor, foreign_depth == 0);
if foreign_depth == 0 {
let out = output.get_or_insert_with(|| String::with_capacity(input.len()));
out.push_str(&input[last..cursor]);
last = end;
}
cursor = end;
continue;
}
if let Some(end) =
super::raw_text::skip_opaque_region(bytes, cursor).or_else(|| skip_cdata_text_element(bytes, cursor))
{
cursor = end;
continue;
}
let name_start = cursor + 1 + usize::from(bytes.get(cursor + 1) == Some(&b'/'));
if !bytes.get(name_start).is_some_and(u8::is_ascii_alphabetic) {
cursor += 1;
continue;
}
let Some(end) = find_tag_end(bytes, cursor + 1) else {
break;
};
update_cdata_foreign_depth(bytes, cursor, end, &mut foreign_depth);
cursor = end;
}
match output {
Some(mut out) => {
out.push_str(&input[last..]);
Cow::Owned(out)
}
None => Cow::Borrowed(input),
}
}
fn skip_cdata_text_element(bytes: &[u8], cursor: usize) -> Option<usize> {
for tag in [
b"textarea".as_slice(),
b"title",
b"xmp",
b"iframe",
b"noembed",
b"noframes",
b"noscript",
] {
if matches_tag_start(bytes, cursor + 1, tag) {
return text_element_end(bytes, cursor, tag);
}
}
matches_tag_start(bytes, cursor + 1, b"plaintext").then_some(bytes.len())
}
fn text_element_end(bytes: &[u8], cursor: usize, tag: &[u8]) -> Option<usize> {
let open_end = find_tag_end(bytes, cursor + 1 + tag.len())?;
Some(super::raw_text::find_closing_tag_bytes(bytes, open_end, tag).unwrap_or(bytes.len()))
}
const WRITTEN_TEXT_ELEMENTS: [&[u8]; 3] = [b"textarea", b"noembed", b"noframes"];
pub(super) fn skip_written_text_element(bytes: &[u8], cursor: usize) -> Option<usize> {
if matches_tag_start(bytes, cursor + 1, b"plaintext") {
return Some(bytes.len());
}
WRITTEN_TEXT_ELEMENTS
.iter()
.find(|name| matches_tag_start(bytes, cursor + 1, name))
.and_then(|name| text_element_end(bytes, cursor, name))
}
fn update_cdata_foreign_depth(bytes: &[u8], cursor: usize, end: usize, depth: &mut usize) {
for tag in [b"svg".as_slice(), b"math".as_slice()] {
if matches_tag_start(bytes, cursor + 1, tag) && !bytes[cursor..end].ends_with(b"/>") {
*depth += 1;
} else if matches_end_tag_start(bytes, cursor + 1, tag) {
*depth = depth.saturating_sub(1);
}
}
}
fn cdata_end(bytes: &[u8], cursor: usize, in_html: bool) -> usize {
if in_html {
memchr::memchr(b'>', &bytes[cursor + 2..]).map_or(bytes.len(), |offset| cursor + 2 + offset + 1)
} else {
super::raw_text::skip_opaque_region(bytes, cursor).unwrap_or(bytes.len())
}
}