use vize_l0::{Allocator, Span, Vec};
use super::super::BlockAttr;
mod interpolation;
use interpolation::skip_interpolation;
pub(super) struct Open<'a> {
pub(super) name: &'a str,
pub(super) end: usize,
pub(super) attrs: Vec<'a, BlockAttr<'a>>,
pub(super) self_closing: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct MatchedClose {
start: usize,
end: usize,
name: Span,
}
impl MatchedClose {
pub(super) fn start(self) -> usize {
self.start
}
pub(super) fn end(self) -> usize {
self.end
}
pub(super) fn name(self) -> Span {
self.name
}
}
pub(super) fn read_open<'a>(
allocator: &'a Allocator,
source: &'a str,
at: usize,
) -> Result<Option<Open<'a>>, ()> {
let mut attrs = Vec::new_in(&allocator);
let boundary = read_open_boundary(source, at, |attr| attrs.push(attr))?;
Ok(boundary.map(|boundary| Open {
name: boundary.name,
end: boundary.end,
attrs,
self_closing: boundary.self_closing,
}))
}
pub(super) struct OpenBoundary<'a> {
pub(super) name: &'a str,
pub(super) end: usize,
pub(super) self_closing: bool,
}
pub(super) fn read_open_boundary<'a>(
source: &'a str,
at: usize,
mut attribute: impl FnMut(BlockAttr<'a>),
) -> Result<Option<OpenBoundary<'a>>, ()> {
let bytes = source.as_bytes();
if bytes.get(at) != Some(&b'<') || !bytes.get(at + 1).is_some_and(u8::is_ascii_alphabetic) {
return Ok(None);
}
let mut pos = at + 1;
while bytes.get(pos).is_some_and(|byte| is_name(*byte)) {
pos += 1;
}
let name = source.get(at + 1..pos).ok_or(())?;
loop {
while bytes
.get(pos)
.is_some_and(|byte| byte.is_ascii_whitespace())
{
pos += 1;
}
match bytes.get(pos) {
Some(b'>') => {
return Ok(Some(OpenBoundary {
name,
end: pos + 1,
self_closing: false,
}));
}
Some(b'/') if bytes.get(pos + 1) == Some(&b'>') => {
return Ok(Some(OpenBoundary {
name,
end: pos + 2,
self_closing: true,
}));
}
None => return Err(()),
_ => {}
}
let start = pos;
while bytes
.get(pos)
.is_some_and(|byte| !byte.is_ascii_whitespace() && !matches!(byte, b'=' | b'>' | b'/'))
{
pos += 1;
}
if pos == start {
return Err(());
}
let name = source.get(start..pos).ok_or(())?;
while bytes
.get(pos)
.is_some_and(|byte| byte.is_ascii_whitespace())
{
pos += 1;
}
let value = if bytes.get(pos) == Some(&b'=') {
pos += 1;
while bytes
.get(pos)
.is_some_and(|byte| byte.is_ascii_whitespace())
{
pos += 1;
}
if let Some(quote @ (b'\'' | b'"')) = bytes.get(pos).copied() {
pos += 1;
let value_start = pos;
while bytes.get(pos).is_some_and(|byte| *byte != quote) {
pos += 1;
}
if bytes.get(pos).is_none() {
return Err(());
}
let value = source.get(value_start..pos).ok_or(())?;
pos += 1;
Some(value)
} else {
let value_start = pos;
while bytes
.get(pos)
.is_some_and(|byte| !byte.is_ascii_whitespace() && !matches!(byte, b'>' | b'/'))
{
pos += 1;
}
Some(source.get(value_start..pos).ok_or(())?)
}
} else {
None
};
attribute(BlockAttr {
name,
value,
span: Span::new(start as u32, pos as u32),
});
}
}
pub(super) fn find_template_close(
source: &str,
from: usize,
) -> (usize, Option<MatchedClose>, bool) {
let bytes = source.as_bytes();
let (mut pos, mut depth) = (from, 1usize);
let mut uncertain = false;
while let Some(offset) = bytes
.get(pos..)
.and_then(|rest| memchr::memchr2(b'<', b'{', rest))
{
pos += offset;
if bytes
.get(pos..)
.is_some_and(|rest| rest.starts_with(b"<!--"))
{
pos = find_bytes(bytes, pos + 4, b"-->").map_or(bytes.len(), |end| end + 3);
continue;
}
if bytes.get(pos..).is_some_and(|rest| rest.starts_with(b"{{")) {
if let Some(end) = skip_interpolation(bytes, pos + 2) {
pos = end;
} else {
uncertain = true;
pos = bytes.len();
}
continue;
}
if bytes.get(pos) != Some(&b'<') {
pos += 1;
continue;
}
if let Some(close) = close_at(bytes, pos, "template") {
depth -= 1;
if depth == 0 {
return (pos, Some(close), uncertain);
}
pos = close.end();
continue;
}
if let Ok(Some(open)) = read_open_boundary(source, pos, |_| {}) {
if open.name.eq_ignore_ascii_case("template") && !open.self_closing {
depth += 1;
}
if ["script", "style", "textarea", "title"]
.iter()
.any(|name| open.name.eq_ignore_ascii_case(name))
&& !open.self_closing
&& let Some(close) = find_close(bytes, open.end, open.name)
{
pos = close.end();
continue;
}
pos = open.end;
continue;
}
pos += 1;
}
(bytes.len(), None, uncertain)
}
pub(super) fn find_close(bytes: &[u8], from: usize, name: &str) -> Option<MatchedClose> {
let mut pos = from;
while let Some(offset) = bytes.get(pos..).and_then(|rest| memchr::memchr(b'<', rest)) {
pos += offset;
if bytes.get(pos) == Some(&b'<')
&& let Some(close) = close_at(bytes, pos, name)
{
return Some(close);
}
pos += 1;
}
None
}
fn close_at(bytes: &[u8], at: usize, name: &str) -> Option<MatchedClose> {
if bytes.get(at..at + 2) != Some(b"</") {
return None;
}
let end_name = at + 2 + name.len();
if !bytes
.get(at + 2..end_name)
.is_some_and(|candidate| candidate.eq_ignore_ascii_case(name.as_bytes()))
{
return None;
}
let mut pos = end_name;
while bytes
.get(pos)
.is_some_and(|byte| byte.is_ascii_whitespace())
{
pos += 1;
}
if bytes.get(pos) != Some(&b'>') {
return None;
}
Some(MatchedClose {
start: at,
end: pos + 1,
name: Span::new(u32::try_from(at + 2).ok()?, u32::try_from(end_name).ok()?),
})
}
pub(super) fn find_bytes(bytes: &[u8], from: usize, needle: &[u8]) -> Option<usize> {
memchr::memmem::find(bytes.get(from..)?, needle).map(|offset| from + offset)
}
fn is_name(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_')
}
#[cfg(test)]
mod tests;