use super::markup::matches_tag_start;
use super::raw_text::{is_self_closing_tag, opens_a_tag, skip_opaque_region};
use super::visibility::TagEndScan;
use crate::converter::main_helpers::{end_tag_starts_body, is_ignorable_before_head, starts_body};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(super) struct HeadScan {
place: Place,
inside: Inside,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Place {
Before { in_document: bool },
In,
AfterEndTag,
Body,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Inside {
Nothing,
Title,
Noscript,
Template(usize),
}
impl HeadScan {
pub(super) const fn new() -> Self {
Self {
place: Place::Before { in_document: false },
inside: Inside::Nothing,
}
}
pub(super) const fn in_body(self) -> bool {
matches!(self.place, Place::Body)
}
pub(super) fn after_tag(self, bytes: &[u8], name_start: usize, is_end_tag: bool, closes_itself: bool) -> Self {
if self.in_body() {
return self;
}
if !is_end_tag && matches_tag_start(bytes, name_start, b"!doctype") {
return self.in_document();
}
let name = lower_case_name(bytes, name_start);
if name.is_empty() {
return self;
}
match self.inside {
Inside::Nothing if is_end_tag => self.after_end_tag(&name),
Inside::Nothing => self.after_start_tag(&name, closes_itself),
inside => Self {
inside: inside.after_tag(&name, is_end_tag, closes_itself),
..self
},
}
}
pub(super) fn after_text(self, text: &str, next_tag_is_html: bool) -> Self {
if self.in_body() || self.inside != Inside::Nothing {
return self;
}
let before_the_head_tag = matches!(self.place, Place::Before { .. });
if is_ignorable_before_head(text, before_the_head_tag && next_tag_is_html) {
self
} else {
self.body()
}
}
fn after_start_tag(self, name: &[u8], closes_itself: bool) -> Self {
let holds_fragment = matches!(name, b"template" | b"noscript");
match (self.place, name) {
(Place::Before { .. }, b"head") => self.at(Place::In),
(_, b"head") => self,
(_, b"html") => self.in_document(),
(_, name) if starts_body(name) => self.body(),
(Place::Before { in_document: false }, _) | (Place::AfterEndTag, b"noscript") if holds_fragment => {
self.body()
}
_ => Self {
inside: Inside::opened_by(name, closes_itself),
..self.in_document()
},
}
}
fn after_end_tag(self, name: &[u8]) -> Self {
match name {
b"head" => self.at(Place::AfterEndTag),
name if end_tag_starts_body(name) => self.body(),
_ => self,
}
}
const fn in_document(self) -> Self {
match self.place {
Place::Before { .. } => self.at(Place::Before { in_document: true }),
_ => self,
}
}
const fn body(self) -> Self {
self.at(Place::Body)
}
const fn at(self, place: Place) -> Self {
Self { place, ..self }
}
}
impl Inside {
fn opened_by(name: &[u8], closes_itself: bool) -> Self {
match name {
_ if closes_itself => Self::Nothing,
b"title" => Self::Title,
b"noscript" => Self::Noscript,
b"template" => Self::Template(1),
_ => Self::Nothing,
}
}
fn after_tag(self, name: &[u8], is_end_tag: bool, closes_itself: bool) -> Self {
match (self, name) {
(Self::Title, b"title") | (Self::Noscript, b"noscript") | (Self::Template(1), b"template")
if is_end_tag =>
{
Self::Nothing
}
(Self::Template(open), b"template") if is_end_tag => Self::Template(open - 1),
(Self::Template(open), b"template") if !closes_itself => Self::Template(open + 1),
(inside, _) => inside,
}
}
}
pub(super) fn body_start(input: &str, tag_ends: &mut TagEndScan<'_>) -> Option<usize> {
let bytes = input.as_bytes();
let mut head = HeadScan::new();
let mut text_start = 0;
let mut idx = 0;
while let Some(offset) = memchr::memchr(b'<', &bytes[idx..]) {
idx += offset;
let is_end_tag = bytes.get(idx + 1) == Some(&b'/');
let name_start = idx + 1 + usize::from(is_end_tag);
let next_tag_is_html = !is_end_tag && matches_tag_start(bytes, name_start, b"html");
head = head.after_text(input.get(text_start..idx).unwrap_or_default(), next_tag_is_html);
if head.in_body() {
return Some(text_start);
}
let is_tag = opens_a_tag(bytes, idx);
let tag_end = if is_tag { tag_ends.tag_end(idx + 1) } else { None };
let closes_itself = tag_end.is_some_and(|end| is_self_closing_tag(&bytes[idx..end], b"template"));
head = head.after_tag(bytes, name_start, is_end_tag, closes_itself);
if head.in_body() {
return Some(idx);
}
let unit_end = if is_tag && tag_end.is_none() {
None
} else {
skip_opaque_region(bytes, idx).or(tag_end)
};
text_start = unit_end.unwrap_or(if is_tag { bytes.len() } else { idx });
idx = unit_end.unwrap_or(idx + 1);
}
None
}
fn lower_case_name(bytes: &[u8], name_start: usize) -> Vec<u8> {
let rest = bytes.get(name_start..).unwrap_or_default();
if !rest.first().is_some_and(u8::is_ascii_alphabetic) {
return Vec::new();
}
let end = rest
.iter()
.position(|byte| byte.is_ascii_whitespace() || matches!(byte, b'/' | b'>'))
.unwrap_or(rest.len());
rest[..end].to_ascii_lowercase()
}
#[cfg(test)]
mod tests {
use super::HeadScan;
fn scan(markup: &str) -> HeadScan {
let bytes = markup.as_bytes();
let mut state = HeadScan::new();
let mut text_start = 0;
while let Some(start) = markup[text_start..].find('<').map(|offset| text_start + offset) {
let end = markup[start..]
.find('>')
.map_or(markup.len(), |offset| start + offset + 1);
let is_end_tag = markup[start..].starts_with("</");
let next_tag_is_html = markup[start..].to_ascii_lowercase().starts_with("<html");
state = state.after_text(&markup[text_start..start], next_tag_is_html);
state = state.after_tag(
bytes,
start + 1 + usize::from(is_end_tag),
is_end_tag,
markup[start..end].ends_with("/>"),
);
text_start = end;
}
state.after_text(&markup[text_start..], false)
}
fn assert_in_body(expected: bool, pages: &[&str]) {
for page in pages {
assert_eq!(scan(page).in_body(), expected, "{page:?}");
}
}
#[test]
fn should_end_the_head_at_a_tag_that_starts_the_body() {
assert_in_body(
true,
&[
"<head><p>",
"<head></head><body>",
"<HEAD><P>",
"<head><metadata>",
"<head><titles>",
"<head><templates>",
"<head></body>",
"<head></html>",
"<head></br>",
"<head></BODY>",
"<html><meta></br>",
"</head><p>",
"<head></head><noscript>",
],
);
assert_in_body(
false,
&[
"<head><head><html><meta><link><base>",
"<head></p></div></bodys></brx>",
"<head></head>",
"<head></head><meta><template>",
"<head></head><template><p></template>",
"<html><title>",
"<head><",
"<head><3>",
],
);
}
#[test]
fn should_read_the_content_of_an_element_of_the_head_as_its_own() {
assert_in_body(
false,
&[
"<head><title><p>a</body>",
"<head><title><p>a</title>",
"<head><noscript><img>a</body></noscript>",
"<head><template><p>a<template><p></template><p></body>",
"<head><template><p>a<template><p></template><p></template>",
"<head><template><template/><p></template>",
"<html><title><p>a</title><noscript><img></noscript><template><p></template>",
"<!DOCTYPE html><noscript><img></noscript>",
"<meta><template><p></template>",
],
);
assert_in_body(
true,
&[
"<head><title><p>a</title><p>",
"<head><title/><p>",
"<head><noscript/><p>",
"<head><template/><p>",
"<head><template><template></template></template><p>",
"<head><noscript></noscript>a",
],
);
}
#[test]
fn should_read_the_first_template_of_a_fragment_as_content() {
assert_in_body(true, &["<template>", "<noscript>", " \n<TEMPLATE>", "</p><template>"]);
assert_in_body(
false,
&[
"<html><template>",
"<!doctype html><template>",
"<title></title><noscript>",
"<link><template>",
],
);
}
#[test]
fn should_end_the_head_at_text_that_the_search_for_the_head_does_not_ignore() {
for start in ["", "<html>", "<head>", "<head></head>", "<head><meta>"] {
let in_body = |text: &str| scan(&format!("{start}{text}")).in_body();
assert!(in_body("a"), "{start}");
assert!(in_body(" \n a"), "{start}");
assert!(in_body("&"), "{start}");
assert!(in_body(" &zzz; "), "{start}");
assert!(in_body("&Tab"), "{start}");
assert!(in_body("&"), "{start}");
assert!(in_body("�"), "{start}");
assert!(!in_body(" \t\n\x0c\r"), "{start}");
assert!(!in_body(""), "{start}");
assert!(
!in_body("     		
  \n"),
"{start}"
);
assert!(!in_body("  \u{a0}"), "{start}");
}
}
#[test]
fn should_ignore_text_before_the_html_tag_and_not_after_the_head_tag() {
assert_in_body(false, &["a<html>", "a<HTML lang=en><head>", "<meta>a<html>"]);
assert_in_body(true, &["a<htmls>", "a<head>", "<head>a<html>", "</head>a<html>"]);
}
}