use std::borrow::Cow;
use std::str;
use super::markup::{find_tag_end, is_json_ld_script_open_tag, matches_end_tag_start, matches_tag_start};
pub(super) fn track_svg_tag(bytes: &[u8], idx: usize, svg_depth: &mut usize) -> Option<usize> {
if matches_tag_start(bytes, idx + 1, b"svg") {
if let Some(open_end) = find_tag_end(bytes, idx + 1 + b"svg".len()) {
*svg_depth += 1;
return Some(open_end);
}
} else if matches_end_tag_start(bytes, idx + 1, b"svg") {
if let Some(close_end) = find_tag_end(bytes, idx + 2 + b"svg".len()) {
if *svg_depth > 0 {
*svg_depth = svg_depth.saturating_sub(1);
}
return Some(close_end);
}
}
None
}
struct RawTextElement<'a> {
input: &'a str,
bytes: &'a [u8],
idx: usize,
len: usize,
last: usize,
open_tag_pattern: &'a [u8],
close_tag_name: &'a [u8],
json_ld_exempt: bool,
}
impl<'a> RawTextElement<'a> {
const fn new(
input: &'a str,
idx: usize,
last: usize,
open_tag_pattern: &'a [u8],
close_tag_name: &'a [u8],
json_ld_exempt: bool,
) -> Self {
Self {
input,
bytes: input.as_bytes(),
idx,
len: input.len(),
last,
open_tag_pattern,
close_tag_name,
json_ld_exempt,
}
}
}
fn strip_raw_text_element(element: RawTextElement<'_>, output: &mut Option<String>) -> Option<usize> {
let prefix_len = element.open_tag_pattern.len();
if element.idx + prefix_len >= element.len
|| !eq_ascii_insensitive(
&element.bytes[element.idx..element.idx + prefix_len],
element.open_tag_pattern,
)
{
return None;
}
let after_tag = element.bytes[element.idx + prefix_len];
if !(after_tag == b'>' || after_tag == b' ' || after_tag == b'\t' || after_tag == b'\n' || after_tag == b'\r') {
return None;
}
let mut tag_end = element.idx + prefix_len;
while tag_end < element.len && element.bytes[tag_end] != b'>' {
tag_end += 1;
}
if tag_end >= element.len {
return None;
}
tag_end += 1;
if element.json_ld_exempt {
let tag_content = &element.input[element.idx..tag_end];
if is_json_ld_script_open_tag(tag_content) {
return None;
}
}
let close_idx = find_closing_tag_bytes(element.bytes, tag_end, element.close_tag_name)?;
let out = output.get_or_insert_with(|| String::with_capacity(element.len));
out.push_str(&element.input[element.last..element.idx]);
if element.idx > 0
&& close_idx < element.len
&& !element.bytes[element.idx - 1].is_ascii_whitespace()
&& !element.bytes[close_idx].is_ascii_whitespace()
{
out.push(' ');
}
Some(close_idx)
}
pub fn strip_script_and_style_tags(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 {
let Some(offset) = memchr::memchr(b'<', &bytes[idx..]) else {
break;
};
idx += offset;
if idx + 1 < len {
if let Some(new_idx) = track_svg_tag(bytes, idx, &mut svg_depth) {
idx = new_idx;
continue;
}
if svg_depth > 0 {
idx += 1;
continue;
}
if bytes[idx + 1] == b'/' && idx + 2 < len {
if idx + 9 <= len && eq_ascii_insensitive(&bytes[idx..idx + 9], b"</script>") {
idx += 9;
continue;
}
if idx + 8 <= len && eq_ascii_insensitive(&bytes[idx..idx + 8], b"</style>") {
idx += 8;
continue;
}
}
if let Some(new_pos) = strip_raw_text_element(
RawTextElement::new(input, idx, last, b"<script", b"script", true),
&mut output,
) {
last = new_pos;
idx = new_pos;
continue;
}
if let Some(new_pos) = strip_raw_text_element(
RawTextElement::new(input, idx, last, b"<style", b"style", false),
&mut output,
) {
last = new_pos;
idx = new_pos;
continue;
}
}
idx += 1;
}
if let Some(mut out) = output {
if last < len {
out.push_str(&input[last..]);
}
Cow::Owned(out)
} else {
Cow::Borrowed(input)
}
}
const MAX_CLOSING_TAG_SCAN: usize = 100_000_000;
fn match_closing_tag_at(bytes: &[u8], idx: usize, len: usize, tag: &[u8]) -> Option<usize> {
let tag_len = tag.len();
if idx + 2 >= len || bytes[idx + 1] != b'/' {
return None;
}
if idx + 2 + tag_len > len || !eq_ascii_insensitive(&bytes[idx + 2..idx + 2 + tag_len], tag) {
return None;
}
let after_tag = idx + 2 + tag_len;
if after_tag >= len || !(bytes[after_tag] == b'>' || bytes[after_tag].is_ascii_whitespace()) {
return None;
}
let mut close_idx = after_tag;
while close_idx < len && bytes[close_idx] != b'>' {
close_idx += 1;
}
if close_idx < len { Some(close_idx + 1) } else { None }
}
#[inline]
pub fn find_closing_tag_bytes(bytes: &[u8], start: usize, tag: &[u8]) -> Option<usize> {
let len = bytes.len();
let mut idx = start;
while idx < len && (idx - start) < MAX_CLOSING_TAG_SCAN {
if bytes[idx] != b'<' {
if let Some(pos) = memchr::memchr(b'<', &bytes[idx..]) {
idx += pos;
} else {
break;
}
}
if let Some(close_pos) = match_closing_tag_at(bytes, idx, len, tag) {
return Some(close_pos);
}
idx += 1;
}
None
}
const VOID_ELEMENT_NAMES: [&[u8]; 4] = [b"br", b"hr", b"img", b"input"];
#[inline]
pub(super) fn is_self_closing_tag(tag_slice: &[u8], tag_name: &[u8]) -> bool {
tag_slice.ends_with(b"/>")
|| VOID_ELEMENT_NAMES
.iter()
.any(|void_name| tag_name.eq_ignore_ascii_case(void_name))
}
const COMMENT_OPEN: &[u8] = b"<!--";
const COMMENT_CLOSE: &[u8] = b"-->";
const CDATA_OPEN: &[u8] = b"<![CDATA[";
const CDATA_CLOSE: &[u8] = b"]]>";
const RAW_TEXT_ELEMENT_NAMES: [&[u8]; 2] = [b"script", b"style"];
#[inline]
fn find_sequence_end(bytes: &[u8], from: usize, terminator: &[u8]) -> usize {
let len = bytes.len();
if from >= len {
return len;
}
match bytes[from..].windows(terminator.len()).position(|w| w == terminator) {
Some(offset) => from + offset + terminator.len(),
None => len,
}
}
#[inline]
pub fn skip_opaque_region(bytes: &[u8], idx: usize) -> Option<usize> {
let len = bytes.len();
if idx >= len || bytes[idx] != b'<' {
return None;
}
if bytes[idx..].starts_with(COMMENT_OPEN) {
return Some(find_sequence_end(bytes, idx + COMMENT_OPEN.len(), COMMENT_CLOSE));
}
if bytes[idx..].starts_with(CDATA_OPEN) {
return Some(find_sequence_end(bytes, idx + CDATA_OPEN.len(), CDATA_CLOSE));
}
for raw_text_name in RAW_TEXT_ELEMENT_NAMES {
if matches_tag_start(bytes, idx + 1, raw_text_name) {
let open_end = find_tag_end(bytes, idx + 1 + raw_text_name.len())?;
return Some(find_closing_tag_bytes(bytes, open_end, raw_text_name).unwrap_or(len));
}
}
None
}
#[inline]
pub(super) fn opens_a_tag(bytes: &[u8], idx: usize) -> bool {
match bytes.get(idx + 1) {
Some(b'/' | b'!') => bytes.get(idx + 2).is_some_and(u8::is_ascii_alphabetic),
Some(byte) => byte.is_ascii_alphabetic(),
None => false,
}
}
#[inline]
pub fn find_closing_tag_bytes_nested(bytes: &[u8], start: usize, tag: &[u8]) -> Option<usize> {
let len = bytes.len();
if tag.is_empty() {
return None;
}
let mut idx = start;
let mut depth = 1usize;
while idx < len && (idx - start) < MAX_CLOSING_TAG_SCAN {
if bytes[idx] != b'<' {
match memchr::memchr(b'<', &bytes[idx..]) {
Some(pos) => idx += pos,
None => break,
}
}
if let Some(region_end) = skip_opaque_region(bytes, idx) {
idx = region_end;
continue;
}
if matches_end_tag_start(bytes, idx + 1, tag) {
if let Some(close_end) = find_tag_end(bytes, idx + 2 + tag.len()) {
depth -= 1;
if depth == 0 {
return Some(close_end);
}
idx = close_end;
continue;
}
} else if matches_tag_start(bytes, idx + 1, tag) {
if let Some(open_end) = find_tag_end(bytes, idx + 1 + tag.len()) {
if !is_self_closing_tag(&bytes[idx..open_end], tag) {
depth += 1;
}
idx = open_end;
continue;
}
} else if opens_a_tag(bytes, idx) {
if let Some(unrelated_tag_end) = find_tag_end(bytes, idx + 1) {
idx = unrelated_tag_end;
continue;
}
}
idx += 1;
}
None
}
#[inline]
pub fn eq_ascii_insensitive(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
a.iter().zip(b.iter()).all(|(x, y)| x.eq_ignore_ascii_case(y))
}