2022-12-18 12:05:39 -05:00
|
|
|
|
use crate::CowStr;
|
|
|
|
|
use crate::Span;
|
2023-03-06 06:18:58 -05:00
|
|
|
|
use std::fmt;
|
2022-12-18 12:05:39 -05:00
|
|
|
|
|
|
|
|
|
use State::*;
|
|
|
|
|
|
2023-02-19 07:09:22 -05:00
|
|
|
|
pub(crate) fn parse(src: &str) -> Attributes {
|
2023-01-15 09:47:28 -05:00
|
|
|
|
let mut a = Attributes::new();
|
2023-02-19 07:09:22 -05:00
|
|
|
|
a.parse(src);
|
2023-01-15 09:47:28 -05:00
|
|
|
|
a
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 11:24:27 -05:00
|
|
|
|
pub fn valid<I: Iterator<Item = char>>(chars: I) -> (usize, bool) {
|
|
|
|
|
let mut has_attr = false;
|
2022-12-22 14:39:11 -05:00
|
|
|
|
let mut p = Parser::new(chars);
|
2023-01-16 11:24:27 -05:00
|
|
|
|
for e in &mut p {
|
|
|
|
|
match e {
|
|
|
|
|
Element::Class(..) | Element::Identifier(..) | Element::Attribute(..) => {
|
2023-01-31 15:13:39 -05:00
|
|
|
|
has_attr = true;
|
2023-01-16 11:24:27 -05:00
|
|
|
|
}
|
|
|
|
|
Element::Invalid => return (0, false),
|
|
|
|
|
}
|
2022-12-22 14:39:11 -05:00
|
|
|
|
}
|
2023-01-16 11:24:27 -05:00
|
|
|
|
(p.pos, has_attr)
|
2022-12-18 12:05:39 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 06:18:58 -05:00
|
|
|
|
/// Stores an attribute value that supports backslash escapes of ASCII punctuation upon displaying,
|
|
|
|
|
/// without allocating.
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
pub struct AttributeValue<'s> {
|
|
|
|
|
raw: CowStr<'s>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'s> AttributeValue<'s> {
|
|
|
|
|
/// Processes the attribute value escapes and returns an iterator of the parts of the value
|
|
|
|
|
/// that should be displayed.
|
|
|
|
|
pub fn parts(&'s self) -> AttributeValueParts<'s> {
|
|
|
|
|
AttributeValueParts { ahead: &self.raw }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'s> From<&'s str> for AttributeValue<'s> {
|
|
|
|
|
fn from(value: &'s str) -> Self {
|
|
|
|
|
Self { raw: value.into() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'s> From<CowStr<'s>> for AttributeValue<'s> {
|
|
|
|
|
fn from(value: CowStr<'s>) -> Self {
|
|
|
|
|
Self { raw: value }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'s> From<String> for AttributeValue<'s> {
|
|
|
|
|
fn from(value: String) -> Self {
|
|
|
|
|
Self { raw: value.into() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'s> fmt::Display for AttributeValue<'s> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
self.parts().try_for_each(|part| f.write_str(part))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An iterator over the parts of an [`AttributeValue`] that should be displayed.
|
|
|
|
|
pub struct AttributeValueParts<'s> {
|
|
|
|
|
ahead: &'s str,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'s> Iterator for AttributeValueParts<'s> {
|
|
|
|
|
type Item = &'s str;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
for (i, _) in self.ahead.match_indices('\\') {
|
|
|
|
|
match self.ahead.as_bytes().get(i + 1) {
|
|
|
|
|
Some(b'\\') => {
|
|
|
|
|
let next = &self.ahead[..i + 1];
|
|
|
|
|
self.ahead = &self.ahead[i + 2..];
|
|
|
|
|
return Some(next);
|
|
|
|
|
}
|
|
|
|
|
Some(c) if c.is_ascii_punctuation() => {
|
|
|
|
|
let next = &self.ahead[..i];
|
|
|
|
|
self.ahead = &self.ahead[i + 1..];
|
|
|
|
|
return Some(next);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(!self.ahead.is_empty()).then(|| std::mem::take(&mut self.ahead))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:55:51 -05:00
|
|
|
|
/// A collection of attributes, i.e. a key-value map.
|
2022-12-18 12:05:39 -05:00
|
|
|
|
// Attributes are relatively rare, we choose to pay 8 bytes always and sometimes an extra
|
|
|
|
|
// indirection instead of always 24 bytes.
|
2023-02-11 15:21:48 -05:00
|
|
|
|
#[allow(clippy::box_vec)]
|
2023-03-26 11:05:50 -04:00
|
|
|
|
#[derive(Clone, PartialEq, Eq, Default)]
|
2023-03-06 06:18:58 -05:00
|
|
|
|
pub struct Attributes<'s>(Option<Box<Vec<(&'s str, AttributeValue<'s>)>>>);
|
2022-12-18 12:05:39 -05:00
|
|
|
|
|
|
|
|
|
impl<'s> Attributes<'s> {
|
2023-02-01 15:55:51 -05:00
|
|
|
|
/// Create an empty collection.
|
2022-12-18 12:05:39 -05:00
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self::default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2023-02-01 15:55:51 -05:00
|
|
|
|
pub(crate) fn take(&mut self) -> Self {
|
2022-12-18 12:05:39 -05:00
|
|
|
|
Self(self.0.take())
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 07:09:22 -05:00
|
|
|
|
pub(crate) fn parse(&mut self, input: &'s str) -> bool {
|
2022-12-18 12:05:39 -05:00
|
|
|
|
for elem in Parser::new(input.chars()) {
|
|
|
|
|
match elem {
|
2023-02-19 07:09:22 -05:00
|
|
|
|
Element::Class(c) => self.insert("class", c.of(input).into()),
|
|
|
|
|
Element::Identifier(i) => self.insert("id", i.of(input).into()),
|
|
|
|
|
Element::Attribute(a, v) => self.insert(a.of(input), v.of(input).into()),
|
2022-12-18 12:05:39 -05:00
|
|
|
|
Element::Invalid => return false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 10:03:01 -05:00
|
|
|
|
/// Combine all attributes from both objects, prioritizing self on conflicts.
|
2023-02-01 15:55:51 -05:00
|
|
|
|
pub(crate) fn union(&mut self, other: Self) {
|
2023-01-28 10:03:01 -05:00
|
|
|
|
if let Some(attrs0) = &mut self.0 {
|
|
|
|
|
if let Some(mut attrs1) = other.0 {
|
2023-02-05 06:38:57 -05:00
|
|
|
|
for (key, val) in attrs1.drain(..) {
|
|
|
|
|
if !attrs0.iter().any(|(k, _)| *k == key) {
|
|
|
|
|
attrs0.push((key, val));
|
2023-01-28 10:03:01 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
self.0 = other.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:55:51 -05:00
|
|
|
|
/// Insert an attribute. If the attribute already exists, the previous value will be
|
|
|
|
|
/// overwritten, unless it is a "class" attribute. In that case the provided value will be
|
|
|
|
|
/// appended to the existing value.
|
2023-03-06 06:18:58 -05:00
|
|
|
|
pub fn insert(&mut self, key: &'s str, val: AttributeValue<'s>) {
|
2022-12-18 12:05:39 -05:00
|
|
|
|
if self.0.is_none() {
|
|
|
|
|
self.0 = Some(Vec::new().into());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let attrs = self.0.as_mut().unwrap();
|
2023-02-01 15:55:51 -05:00
|
|
|
|
if let Some(i) = attrs.iter().position(|(k, _)| *k == key) {
|
2023-01-15 14:03:22 -05:00
|
|
|
|
let prev = &mut attrs[i].1;
|
2023-02-01 15:55:51 -05:00
|
|
|
|
if key == "class" {
|
2023-01-15 14:03:22 -05:00
|
|
|
|
*prev = format!("{} {}", prev, val).into();
|
|
|
|
|
} else {
|
|
|
|
|
*prev = val;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-02-01 15:55:51 -05:00
|
|
|
|
attrs.push((key, val));
|
2023-01-15 14:03:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:55:51 -05:00
|
|
|
|
/// Returns true if the collection contains no attributes.
|
2023-01-15 14:03:22 -05:00
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2023-02-01 15:55:51 -05:00
|
|
|
|
self.0.as_ref().map_or(true, |v| v.is_empty())
|
2022-12-18 12:05:39 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:55:51 -05:00
|
|
|
|
/// Returns a reference to the value corresponding to the attribute key.
|
2023-01-29 09:10:01 -05:00
|
|
|
|
#[must_use]
|
2023-03-06 06:18:58 -05:00
|
|
|
|
pub fn get(&self, key: &str) -> Option<&AttributeValue<'s>> {
|
2023-01-29 09:10:01 -05:00
|
|
|
|
self.iter().find(|(k, _)| *k == key).map(|(_, v)| v)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 15:55:51 -05:00
|
|
|
|
/// Returns an iterator over the attributes in undefined order.
|
2023-03-06 06:18:58 -05:00
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&'s str, &AttributeValue<'s>)> + '_ {
|
|
|
|
|
self.0.iter().flat_map(|v| v.iter().map(|(a, b)| (*a, b)))
|
2022-12-18 12:05:39 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
impl<'s> FromIterator<(&'s str, &'s str)> for Attributes<'s> {
|
|
|
|
|
fn from_iter<I: IntoIterator<Item = (&'s str, &'s str)>>(iter: I) -> Self {
|
|
|
|
|
let attrs = iter
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(a, v)| (a, v.into()))
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
if attrs.is_empty() {
|
|
|
|
|
Attributes::new()
|
|
|
|
|
} else {
|
|
|
|
|
Attributes(Some(attrs.into()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 11:05:50 -04:00
|
|
|
|
impl<'s> std::fmt::Debug for Attributes<'s> {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
write!(f, "{{")?;
|
|
|
|
|
let mut first = true;
|
|
|
|
|
for (k, v) in self.iter() {
|
|
|
|
|
if !first {
|
|
|
|
|
write!(f, ", ")?;
|
|
|
|
|
}
|
|
|
|
|
first = false;
|
|
|
|
|
write!(f, "{}=\"{}\"", k, v.raw)?;
|
|
|
|
|
}
|
|
|
|
|
write!(f, "}}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 12:05:39 -05:00
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
enum State {
|
|
|
|
|
Start,
|
|
|
|
|
Whitespace,
|
|
|
|
|
Comment,
|
|
|
|
|
ClassFirst,
|
|
|
|
|
Class,
|
|
|
|
|
IdentifierFirst,
|
|
|
|
|
Identifier,
|
2023-02-17 17:28:07 -05:00
|
|
|
|
Key,
|
2022-12-18 12:05:39 -05:00
|
|
|
|
ValueFirst,
|
|
|
|
|
Value,
|
|
|
|
|
ValueQuoted,
|
|
|
|
|
Done,
|
|
|
|
|
Invalid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Parser<I> {
|
|
|
|
|
chars: I,
|
|
|
|
|
pos: usize,
|
2023-02-01 15:55:51 -05:00
|
|
|
|
pos_prev: usize,
|
2022-12-18 12:05:39 -05:00
|
|
|
|
state: State,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<I: Iterator<Item = char>> Parser<I> {
|
|
|
|
|
fn new(chars: I) -> Self {
|
|
|
|
|
Parser {
|
|
|
|
|
chars,
|
|
|
|
|
pos: 0,
|
2023-02-01 15:55:51 -05:00
|
|
|
|
pos_prev: 0,
|
2022-12-18 12:05:39 -05:00
|
|
|
|
state: Start,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn step_char(&mut self) -> Option<State> {
|
|
|
|
|
self.chars.next().map(|c| {
|
2023-02-01 15:55:51 -05:00
|
|
|
|
self.pos_prev = self.pos;
|
2022-12-18 12:05:39 -05:00
|
|
|
|
self.pos += c.len_utf8();
|
|
|
|
|
match self.state {
|
|
|
|
|
Start => match c {
|
|
|
|
|
'{' => Whitespace,
|
|
|
|
|
_ => Invalid,
|
|
|
|
|
},
|
|
|
|
|
Whitespace => match c {
|
|
|
|
|
'}' => Done,
|
|
|
|
|
'.' => ClassFirst,
|
|
|
|
|
'#' => IdentifierFirst,
|
|
|
|
|
'%' => Comment,
|
2023-02-17 17:28:07 -05:00
|
|
|
|
c if c.is_ascii_alphanumeric() || matches!(c, '_' | ':' | '-') => Key,
|
2022-12-18 12:05:39 -05:00
|
|
|
|
c if c.is_whitespace() => Whitespace,
|
|
|
|
|
_ => Invalid,
|
|
|
|
|
},
|
|
|
|
|
Comment => {
|
|
|
|
|
if c == '%' {
|
|
|
|
|
Whitespace
|
|
|
|
|
} else {
|
|
|
|
|
Comment
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s @ (ClassFirst | IdentifierFirst) => {
|
2023-03-12 12:13:14 -04:00
|
|
|
|
if is_name(c) {
|
2022-12-18 12:05:39 -05:00
|
|
|
|
match s {
|
|
|
|
|
ClassFirst => Class,
|
|
|
|
|
IdentifierFirst => Identifier,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Invalid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s @ (Class | Identifier | Value) => {
|
|
|
|
|
if is_name(c) {
|
|
|
|
|
s
|
|
|
|
|
} else if c.is_whitespace() {
|
|
|
|
|
Whitespace
|
|
|
|
|
} else if c == '}' {
|
|
|
|
|
Done
|
|
|
|
|
} else {
|
|
|
|
|
Invalid
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-17 17:28:07 -05:00
|
|
|
|
Key => {
|
2022-12-18 12:05:39 -05:00
|
|
|
|
if is_name(c) {
|
2023-02-17 17:28:07 -05:00
|
|
|
|
Key
|
2022-12-18 12:05:39 -05:00
|
|
|
|
} else if c == '=' {
|
|
|
|
|
ValueFirst
|
|
|
|
|
} else {
|
|
|
|
|
Invalid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ValueFirst => {
|
|
|
|
|
if is_name(c) {
|
|
|
|
|
Value
|
|
|
|
|
} else if c == '"' {
|
|
|
|
|
ValueQuoted
|
|
|
|
|
} else {
|
|
|
|
|
Invalid
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 06:18:58 -05:00
|
|
|
|
ValueQuoted => match c {
|
|
|
|
|
'\\' => {
|
|
|
|
|
if let Some(c) = self.chars.next() {
|
|
|
|
|
self.pos_prev = self.pos;
|
|
|
|
|
self.pos += c.len_utf8();
|
|
|
|
|
}
|
2022-12-18 12:05:39 -05:00
|
|
|
|
ValueQuoted
|
|
|
|
|
}
|
2023-03-06 06:18:58 -05:00
|
|
|
|
'"' => Whitespace,
|
|
|
|
|
_ => ValueQuoted,
|
|
|
|
|
},
|
2023-01-12 11:26:53 -05:00
|
|
|
|
Invalid | Done => panic!("{:?}", self.state),
|
2022-12-18 12:05:39 -05:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn step(&mut self) -> (State, Span) {
|
2023-02-01 15:55:51 -05:00
|
|
|
|
let start = self.pos_prev;
|
2022-12-18 12:05:39 -05:00
|
|
|
|
|
2022-12-22 14:39:11 -05:00
|
|
|
|
if self.state == Done {
|
|
|
|
|
return (Done, Span::empty_at(start));
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 11:26:53 -05:00
|
|
|
|
if self.state == Invalid {
|
|
|
|
|
return (Invalid, Span::empty_at(start));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 12:05:39 -05:00
|
|
|
|
while let Some(state_next) = self.step_char() {
|
|
|
|
|
if self.state != state_next {
|
|
|
|
|
return (
|
|
|
|
|
std::mem::replace(&mut self.state, state_next),
|
2023-02-01 15:55:51 -05:00
|
|
|
|
Span::new(start, self.pos_prev),
|
2022-12-18 12:05:39 -05:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
if self.state == Done { Done } else { Invalid },
|
2023-02-01 15:55:51 -05:00
|
|
|
|
Span::new(start, self.pos_prev),
|
2022-12-18 12:05:39 -05:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 15:23:50 -05:00
|
|
|
|
pub fn is_name(c: char) -> bool {
|
2023-03-12 12:13:14 -04:00
|
|
|
|
c.is_ascii_alphanumeric() || matches!(c, ':' | '_' | '-')
|
2022-12-18 12:05:39 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum Element {
|
|
|
|
|
Class(Span),
|
|
|
|
|
Identifier(Span),
|
|
|
|
|
Attribute(Span, Span),
|
|
|
|
|
Invalid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<I: Iterator<Item = char>> Iterator for Parser<I> {
|
|
|
|
|
type Item = Element;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
loop {
|
|
|
|
|
let (st, span0) = self.step();
|
|
|
|
|
return match st {
|
|
|
|
|
ClassFirst | IdentifierFirst => {
|
|
|
|
|
let (st, span1) = self.step();
|
|
|
|
|
Some(match st {
|
|
|
|
|
Class => Element::Class(span1),
|
|
|
|
|
Identifier => Element::Identifier(span1),
|
|
|
|
|
_ => return Some(Element::Invalid),
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-02-17 17:28:07 -05:00
|
|
|
|
Key => {
|
2022-12-18 12:05:39 -05:00
|
|
|
|
let (st, _span1) = self.step();
|
|
|
|
|
match st {
|
|
|
|
|
ValueFirst => {
|
|
|
|
|
let (st, span2) = self.step();
|
|
|
|
|
match st {
|
|
|
|
|
Value => Some(Element::Attribute(span0, span2)),
|
|
|
|
|
ValueQuoted => Some(Element::Attribute(span0, span2.skip(1))),
|
|
|
|
|
Invalid => Some(Element::Invalid),
|
|
|
|
|
_ => panic!("{:?}", st),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Invalid => Some(Element::Invalid),
|
|
|
|
|
_ => panic!("{:?}", st),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Comment | Start | Whitespace => continue,
|
|
|
|
|
Done => None,
|
|
|
|
|
Invalid => Some(Element::Invalid),
|
|
|
|
|
_ => panic!("{:?}", st),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
macro_rules! test_attr {
|
|
|
|
|
($src:expr $(,$($av:expr),* $(,)?)?) => {
|
|
|
|
|
#[allow(unused)]
|
2023-03-06 06:18:58 -05:00
|
|
|
|
let mut attr = super::Attributes::new();
|
2023-01-15 09:47:28 -05:00
|
|
|
|
attr.parse($src);
|
2022-12-18 12:05:39 -05:00
|
|
|
|
let actual = attr.iter().collect::<Vec<_>>();
|
|
|
|
|
let expected = &[$($($av),*,)?];
|
2023-03-06 06:18:58 -05:00
|
|
|
|
for i in 0..actual.len() {
|
|
|
|
|
let actual_val = format!("{}", actual[i].1);
|
|
|
|
|
assert_eq!((actual[i].0, actual_val.as_str()), expected[i], "\n\n{}\n\n", $src);
|
|
|
|
|
}
|
2022-12-18 12:05:39 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn empty() {
|
|
|
|
|
test_attr!("{}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn class_id() {
|
|
|
|
|
test_attr!(
|
|
|
|
|
"{.some_class #some_id}",
|
|
|
|
|
("class", "some_class"),
|
|
|
|
|
("id", "some_id"),
|
|
|
|
|
);
|
2023-02-01 15:55:51 -05:00
|
|
|
|
test_attr!("{.a .b}", ("class", "a b"));
|
|
|
|
|
test_attr!("{#a #b}", ("id", "b"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unicode_whitespace() {
|
|
|
|
|
test_attr!("{.a .b}", ("class", "a b"));
|
2022-12-18 12:05:39 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn value_unquoted() {
|
|
|
|
|
test_attr!(
|
|
|
|
|
"{attr0=val0 attr1=val1}",
|
|
|
|
|
("attr0", "val0"),
|
|
|
|
|
("attr1", "val1"),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn value_quoted() {
|
|
|
|
|
test_attr!(
|
|
|
|
|
r#"{attr0="val0" attr1="val1"}"#,
|
|
|
|
|
("attr0", "val0"),
|
|
|
|
|
("attr1", "val1"),
|
|
|
|
|
);
|
|
|
|
|
test_attr!(
|
|
|
|
|
r#"{#id .class style="color:red"}"#,
|
|
|
|
|
("id", "id"),
|
|
|
|
|
("class", "class"),
|
|
|
|
|
("style", "color:red")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn comment() {
|
|
|
|
|
test_attr!("{%%}");
|
|
|
|
|
test_attr!("{ % abc % }");
|
|
|
|
|
test_attr!(
|
|
|
|
|
"{ .some_class % abc % #some_id}",
|
|
|
|
|
("class", "some_class"),
|
|
|
|
|
("id", "some_id"),
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-12-22 14:39:11 -05:00
|
|
|
|
|
2023-03-06 06:18:58 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn escape() {
|
|
|
|
|
test_attr!(
|
|
|
|
|
r#"{attr="with escaped \~ char"}"#,
|
|
|
|
|
("attr", "with escaped ~ char")
|
|
|
|
|
);
|
|
|
|
|
test_attr!(
|
|
|
|
|
r#"{key="quotes \" should be escaped"}"#,
|
|
|
|
|
("key", r#"quotes " should be escaped"#)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn escape_backslash() {
|
|
|
|
|
test_attr!(r#"{attr="with\\backslash"}"#, ("attr", r"with\backslash"));
|
|
|
|
|
test_attr!(
|
|
|
|
|
r#"{attr="with many backslashes\\\\"}"#,
|
|
|
|
|
("attr", r"with many backslashes\\")
|
|
|
|
|
);
|
|
|
|
|
test_attr!(
|
|
|
|
|
r#"{attr="\\escaped backslash at start"}"#,
|
|
|
|
|
("attr", r"\escaped backslash at start")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn only_escape_punctuation() {
|
|
|
|
|
test_attr!(r#"{attr="do not \escape"}"#, ("attr", r"do not \escape"));
|
|
|
|
|
test_attr!(
|
|
|
|
|
r#"{attr="\backslash at the beginning"}"#,
|
|
|
|
|
("attr", r"\backslash at the beginning")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-22 14:39:11 -05:00
|
|
|
|
#[test]
|
2023-01-12 11:26:53 -05:00
|
|
|
|
fn valid_full() {
|
|
|
|
|
let src = "{.class %comment%}";
|
2023-01-16 11:24:27 -05:00
|
|
|
|
assert_eq!(super::valid(src.chars()), (src.len(), true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn valid_empty() {
|
|
|
|
|
let src = "{}";
|
|
|
|
|
assert_eq!(super::valid(src.chars()), (src.len(), false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn valid_whitespace() {
|
|
|
|
|
let src = "{ \n }";
|
|
|
|
|
assert_eq!(super::valid(src.chars()), (src.len(), false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn valid_comment() {
|
|
|
|
|
let src = "{%comment%}";
|
|
|
|
|
assert_eq!(super::valid(src.chars()), (src.len(), false));
|
2023-01-12 11:26:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn valid_trailing() {
|
|
|
|
|
let src = "{.class}";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
super::valid(src.chars().chain("{.ignore}".chars())),
|
2023-01-16 11:24:27 -05:00
|
|
|
|
(src.len(), true),
|
2023-01-12 11:26:53 -05:00
|
|
|
|
);
|
|
|
|
|
}
|
2022-12-22 14:39:11 -05:00
|
|
|
|
|
2023-01-12 11:26:53 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn valid_invalid() {
|
2023-01-16 11:24:27 -05:00
|
|
|
|
assert_eq!(super::valid(" {.valid}".chars()), (0, false));
|
|
|
|
|
assert_eq!(super::valid("{.class invalid}".chars()), (0, false));
|
|
|
|
|
assert_eq!(super::valid("abc".chars()), (0, false));
|
|
|
|
|
assert_eq!(super::valid("{.abc.}".chars()), (0, false));
|
2022-12-22 14:39:11 -05:00
|
|
|
|
}
|
2022-12-18 12:05:39 -05:00
|
|
|
|
}
|