use std::borrow::Cow;
use super::raw_text::track_svg_tag;
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;
while idx + 3 < len {
if !(bytes[idx] == b'<' && bytes[idx + 1] == b'!' && bytes[idx + 2] == b'-' && bytes[idx + 3] == b'-') {
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 mut consecutive_dashes: usize = 0;
while idx < len {
let b = bytes[idx];
if b == b'-' {
consecutive_dashes += 1;
idx += 1;
} else if b == b'>' && consecutive_dashes >= 2 {
if consecutive_dashes > 2 {
let out = output.get_or_insert_with(|| String::with_capacity(len));
let close_start = idx - consecutive_dashes;
out.push_str(&input[last..close_start]);
out.push_str("-->");
idx += 1;
last = idx;
} else {
idx += 1;
}
break;
} else {
consecutive_dashes = 0;
idx += 1;
}
}
}
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(
input: &str,
bytes: &[u8],
idx: usize,
last: usize,
output: &mut Option<String>,
) -> Option<usize> {
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());
Some(name_end)
}
fn strip_raw_text_tag_reopen(
input: &str,
bytes: &[u8],
idx: usize,
last: usize,
output: &mut Option<String>,
) -> Option<usize> {
const TAGS: [&[u8]; 2] = [b"script", b"style"];
for tag in TAGS {
if matches_tag_start(bytes, idx + 1, tag) {
if let Some(open_end) = find_tag_end(bytes, idx + 1 + tag.len()) {
if tag == b"script" && is_json_ld_script_open_tag(&input[idx..open_end]) {
continue;
}
let remove_end = find_closing_tag(bytes, open_end, tag).unwrap_or(open_end);
let out = output.get_or_insert_with(|| String::with_capacity(input.len()));
out.push_str(&input[last..idx]);
out.push_str(&input[idx..open_end]);
out.push_str("</");
if let Ok(tag_str) = str::from_utf8(tag) {
out.push_str(tag_str);
}
out.push('>');
return Some(remove_end);
}
}
}
None
}
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> {
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;
while idx < len {
if bytes[idx] == b'<' {
if let Some(new_pos) = collapse_empty_comment(input, bytes, idx, last, &mut output) {
last = new_pos;
idx = new_pos;
continue;
}
if let Some(new_pos) = normalize_self_closing_void(input, bytes, idx, last, &mut output) {
last = new_pos;
idx = new_pos;
continue;
}
if let Some(new_idx) = track_svg_tag(bytes, idx, &mut svg_depth) {
idx = new_idx;
continue;
}
if svg_depth == 0 {
if let Some(new_pos) = normalize_void_tag_case(input, bytes, idx, last, &mut output) {
last = new_pos;
idx = new_pos;
continue;
}
if let Some(new_pos) = strip_raw_text_tag_reopen(input, bytes, idx, last, &mut output) {
last = new_pos;
idx = new_pos;
continue;
}
if let Some(new_pos) = strip_doctype(input, bytes, idx, len, last, &mut output) {
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;
}
}
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 = &tag[value_start..value_end];
let media_type = value.split(';').next().unwrap_or(value).trim();
return eq_ascii_case_insensitive(media_type.as_bytes(), b"application/ld+json");
}
idx += 1;
}
false
}
#[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 find_closing_tag(bytes: &[u8], mut idx: usize, tag: &[u8]) -> Option<usize> {
let len = bytes.len();
let mut depth = 1usize;
while idx < len {
if let Some(next) = matching_open_end(bytes, idx, tag) {
depth += 1;
idx = next;
continue;
}
if let Some(close) = matching_close_end(bytes, idx, tag) {
depth -= 1;
if depth == 0 {
return Some(close);
}
idx = close;
continue;
}
idx += 1;
}
None
}
fn matching_open_end(bytes: &[u8], idx: usize, tag: &[u8]) -> Option<usize> {
(bytes.get(idx) == Some(&b'<') && matches_tag_start(bytes, idx + 1, tag))
.then(|| find_tag_end(bytes, idx + 1 + tag.len()))
.flatten()
}
fn matching_close_end(bytes: &[u8], idx: usize, tag: &[u8]) -> Option<usize> {
(bytes.get(idx) == Some(&b'<') && matches_end_tag_start(bytes, idx + 1, tag))
.then(|| find_tag_end(bytes, idx + 2 + tag.len()))
.flatten()
}
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)
}