use super::{Mode, argument_end, echo_delimiters, echo_end, tag_quote_end, word};
pub(super) fn attributes(mut source: &[u8], mode: Mode) -> bool {
while !source.is_empty() {
if !source[0].is_ascii_whitespace() {
return false;
}
source = trim_start(source);
if source.is_empty() {
return true;
}
if let Some((_, open, close)) = echo_delimiters(source) {
let Some(end) = echo_end(source, open, close, mode == Mode::Lossless) else {
return false;
};
source = &source[end + close.len()..];
continue;
}
if let Some(name) = source.strip_prefix(b":$") {
let length = name.iter().take_while(|&&byte| word(byte)).count();
if length == 0 {
return false;
}
source = &name[length..];
continue;
}
let length = source
.iter()
.take_while(|&&byte| word(byte) || matches!(byte, b'-' | b':' | b'.' | b'@' | b'%'))
.count();
if length == 0 {
return false;
}
let (name, rest) = source.split_at(length);
let tail = trim_start(rest);
if (name.eq_ignore_ascii_case(b"@class") || name.eq_ignore_ascii_case(b"@style"))
&& tail.starts_with(b"(")
{
let Some(end) = argument_end(tail, 0, mode) else {
return false;
};
source = &tail[end + 1..];
} else if let Some(value) = tail.strip_prefix(b"=") {
let value = trim_start(value);
let length = match value.first() {
Some(b'\'' | b'"') => {
let Some(end) = tag_quote_end(value, 0, mode) else {
return false;
};
end + 1
}
_ => value
.iter()
.take_while(|&&byte| {
!byte.is_ascii_whitespace()
&& !matches!(byte, b'\'' | b'"' | b'=' | b'<' | b'>')
})
.count(),
};
if length == 0 {
return false;
}
source = &value[length..];
} else {
source = rest;
}
}
true
}
fn trim_start(source: &[u8]) -> &[u8] {
&source[source
.iter()
.take_while(|byte| byte.is_ascii_whitespace())
.count()..]
}