use std::borrow::Cow;
use crate::tokenizer::{Span, Token, Tokenizer};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Bracket {
Paren,
Square,
Curly,
}
impl Bracket {
pub fn open(self) -> char {
match self {
Bracket::Paren => '(',
Bracket::Square => '[',
Bracket::Curly => '{',
}
}
pub fn close(self) -> char {
match self {
Bracket::Paren => ')',
Bracket::Square => ']',
Bracket::Curly => '}',
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum ComponentValue<'a> {
Token(Token<'a>, Span),
Function(Function<'a>),
Block(SimpleBlock<'a>),
}
impl<'a> ComponentValue<'a> {
pub fn span(&self) -> Span {
match self {
ComponentValue::Token(_, span) => *span,
ComponentValue::Function(f) => f.span,
ComponentValue::Block(b) => b.span,
}
}
pub fn token(&self) -> Option<&Token<'a>> {
match self {
ComponentValue::Token(t, _) => Some(t),
_ => None,
}
}
pub fn is_whitespace(&self) -> bool {
matches!(self, ComponentValue::Token(Token::Whitespace, _))
}
fn is(&self, token: &Token<'_>) -> bool {
match (self, token) {
(ComponentValue::Token(Token::Delim(a), _), Token::Delim(b)) => a == b,
(ComponentValue::Token(t, _), _) => {
std::mem::discriminant(t) == std::mem::discriminant(token)
}
_ => false,
}
}
fn is_curly(&self) -> bool {
matches!(self, ComponentValue::Block(b) if b.bracket == Bracket::Curly)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Function<'a> {
pub name: Cow<'a, str>,
pub arguments: Vec<ComponentValue<'a>>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SimpleBlock<'a> {
pub bracket: Bracket,
pub contents: Vec<ComponentValue<'a>>,
pub span: Span,
}
fn drop_flat(children: &mut Vec<ComponentValue<'_>>) {
if children
.iter()
.all(|c| matches!(c, ComponentValue::Token(..)))
{
return;
}
let mut stack = std::mem::take(children);
while let Some(child) = stack.pop() {
match child {
ComponentValue::Function(mut f) => stack.append(&mut f.arguments),
ComponentValue::Block(mut b) => stack.append(&mut b.contents),
ComponentValue::Token(..) => {}
}
}
}
impl Drop for Function<'_> {
fn drop(&mut self) {
drop_flat(&mut self.arguments);
}
}
impl Drop for SimpleBlock<'_> {
fn drop(&mut self) {
drop_flat(&mut self.contents);
}
}
impl<'a> SimpleBlock<'a> {
pub fn contents(mut self, src: &'a str) -> BlockContents<'a> {
let values = std::mem::take(&mut self.contents);
let mut p = Parser::new(src, values.into_iter());
let items = p.block_contents();
BlockContents {
items,
errors: p.errors,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct AtRule<'a, B = SimpleBlock<'a>> {
pub name: Cow<'a, str>,
pub prelude: Vec<ComponentValue<'a>>,
pub block: Option<B>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct QualifiedRule<'a, B = SimpleBlock<'a>> {
pub prelude: Vec<ComponentValue<'a>>,
pub block: B,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Rule<'a, B = SimpleBlock<'a>> {
At(AtRule<'a, B>),
Qualified(QualifiedRule<'a, B>),
}
impl<B> Rule<'_, B> {
pub fn span(&self) -> Span {
match self {
Rule::At(r) => r.span,
Rule::Qualified(r) => r.span,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Declaration<'a> {
pub name: Cow<'a, str>,
pub value: Vec<ComponentValue<'a>>,
pub important: bool,
pub span: Span,
}
impl Declaration<'_> {
pub fn original_text<'s>(&self, src: &'s str) -> &'s str {
match (self.value.first(), self.value.last()) {
(Some(first), Some(last)) => &src[first.span().start..last.span().end],
_ => "",
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Item<'a, B = SimpleBlock<'a>> {
Declaration(Declaration<'a>),
Rule(Rule<'a, B>),
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Block<'a> {
pub items: Vec<Item<'a, Block<'a>>>,
pub span: Span,
}
impl Drop for Block<'_> {
fn drop(&mut self) {
if self.items.iter().all(|i| matches!(i, Item::Declaration(_))) {
return;
}
let mut stack = std::mem::take(&mut self.items);
while let Some(item) = stack.pop() {
match item {
Item::Rule(Rule::Qualified(mut r)) => stack.append(&mut r.block.items),
Item::Rule(Rule::At(AtRule {
block: Some(mut b), ..
})) => stack.append(&mut b.items),
_ => {}
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ParseError {
pub kind: ErrorKind,
pub span: Span,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ErrorKind {
Empty,
ExtraInput,
InvalidDeclaration,
InvalidRule,
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let what = match self.kind {
ErrorKind::Empty => "nothing to parse",
ErrorKind::ExtraInput => "unexpected input after the end",
ErrorKind::InvalidDeclaration => "invalid declaration",
ErrorKind::InvalidRule => "invalid rule",
};
write!(f, "{what} at bytes {}..{}", self.span.start, self.span.end)
}
}
impl std::error::Error for ParseError {}
#[derive(Clone, Debug, PartialEq)]
pub struct Stylesheet<'a, B = SimpleBlock<'a>> {
pub rules: Vec<Rule<'a, B>>,
pub errors: Vec<ParseError>,
}
impl<B> Default for Stylesheet<'_, B> {
fn default() -> Self {
Stylesheet {
rules: Vec::new(),
errors: Vec::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct BlockContents<'a> {
pub items: Vec<Item<'a>>,
pub errors: Vec<ParseError>,
}
pub(crate) struct Values<'a> {
pub(crate) tokens: Tokenizer<'a>,
open: Vec<Open<'a>>,
buffer: Vec<ComponentValue<'a>>,
}
impl<'a> Values<'a> {
pub(crate) fn new(tokens: Tokenizer<'a>) -> Values<'a> {
Values {
tokens,
open: Vec::new(),
buffer: Vec::new(),
}
}
}
struct Open<'a> {
bracket: Option<Bracket>,
name: Cow<'a, str>,
start: usize,
at: usize,
}
pub(crate) fn take_tail<T>(buf: &mut Vec<T>, at: usize) -> Vec<T> {
if at > 0 {
return buf.split_off(at);
}
let mut tail = Vec::with_capacity(buf.len());
tail.append(buf);
tail
}
#[inline]
fn opens<'a>(token: Token<'a>, start: usize, at: usize) -> Result<Open<'a>, Token<'a>> {
let (bracket, name) = match token {
Token::Function(name) => (None, name),
Token::LeftParen => (Some(Bracket::Paren), Cow::Borrowed("")),
Token::LeftBracket => (Some(Bracket::Square), Cow::Borrowed("")),
Token::LeftBrace => (Some(Bracket::Curly), Cow::Borrowed("")),
other => return Err(other),
};
Ok(Open {
bracket,
name,
start,
at,
})
}
impl<'a> Open<'a> {
fn closes(&self, token: &Token<'_>) -> bool {
matches!(
(self.bracket, token),
(None | Some(Bracket::Paren), Token::RightParen)
| (Some(Bracket::Square), Token::RightBracket)
| (Some(Bracket::Curly), Token::RightBrace)
)
}
fn finish(self, values: Vec<ComponentValue<'a>>, end: usize) -> ComponentValue<'a> {
let span = Span::new(self.start, end);
match self.bracket {
None => ComponentValue::Function(Function {
name: self.name,
arguments: values,
span,
}),
Some(bracket) => ComponentValue::Block(SimpleBlock {
bracket,
contents: values,
span,
}),
}
}
}
impl<'a> Iterator for Values<'a> {
type Item = ComponentValue<'a>;
fn next(&mut self) -> Option<ComponentValue<'a>> {
let (token, span) = self.tokens.next_token()?;
Some(self.whole(token, span))
}
}
impl<'a> Values<'a> {
#[inline]
pub(crate) fn whole(&mut self, token: Token<'a>, span: Span) -> ComponentValue<'a> {
match opens(token, span.start, 0) {
Ok(open) => self.open.push(open),
Err(token) => return ComponentValue::Token(token, span),
}
self.rest_of_open()
}
fn rest_of_open(&mut self) -> ComponentValue<'a> {
loop {
let Some((token, span)) = self.tokens.next_token() else {
let end = self.tokens.source().len();
loop {
let open = self.open.pop().unwrap();
let values = take_tail(&mut self.buffer, open.at);
let done = open.finish(values, end);
if self.open.is_empty() {
return done;
}
self.buffer.push(done);
}
};
if self.open.last().unwrap().closes(&token) {
let open = self.open.pop().unwrap();
let values = take_tail(&mut self.buffer, open.at);
let done = open.finish(values, span.end);
if self.open.is_empty() {
return done;
}
self.buffer.push(done);
continue;
}
if !matches!(
token,
Token::Function(_) | Token::LeftParen | Token::LeftBracket | Token::LeftBrace
) {
self.buffer.push(ComponentValue::Token(token, span));
continue;
}
match opens(token, span.start, self.buffer.len()) {
Ok(open) => self.open.push(open),
Err(token) => self.buffer.push(ComponentValue::Token(token, span)),
}
}
}
}
trait Source<'a>: Iterator<Item = ComponentValue<'a>> {
fn rest(&self) -> Option<&[ComponentValue<'a>]> {
None
}
}
impl<'a> Source<'a> for Values<'a> {}
impl<'a> Source<'a> for std::vec::IntoIter<ComponentValue<'a>> {
fn rest(&self) -> Option<&[ComponentValue<'a>]> {
Some(self.as_slice())
}
}
fn before_semicolon(values: &[ComponentValue<'_>], nested: bool) -> usize {
values
.iter()
.position(|v| v.is(&Token::Semicolon) || (nested && v.is(&Token::RightBrace)))
.unwrap_or(values.len())
}
struct Parser<'a, I> {
src: &'a str,
input: I,
back: Vec<ComponentValue<'a>>,
scratch: Vec<ComponentValue<'a>>,
errors: Vec<ParseError>,
}
fn text(src: &str) -> Parser<'_, Values<'_>> {
Parser::new(src, Values::new(Tokenizer::new(src)))
}
impl<'a, I: Source<'a>> Parser<'a, I> {
fn new(src: &'a str, input: I) -> Parser<'a, I> {
Parser {
src,
input,
back: Vec::new(),
scratch: Vec::new(),
errors: Vec::new(),
}
}
fn peek(&mut self) -> Option<&ComponentValue<'a>> {
if self.back.is_empty() {
if self.input.rest().is_some() {
return self.input.rest().and_then(<[_]>::first);
}
let next = self.input.next()?;
self.back.push(next);
}
self.back.last()
}
fn next(&mut self) -> Option<ComponentValue<'a>> {
self.back.pop().or_else(|| self.input.next())
}
fn put_back(&mut self, values: Vec<ComponentValue<'a>>) {
self.back.extend(values.into_iter().rev());
}
fn skip_whitespace(&mut self) {
while self.peek().is_some_and(ComponentValue::is_whitespace) {
self.next();
}
}
fn error(&mut self, kind: ErrorKind, start: usize, end: usize) {
self.errors.push(ParseError {
kind,
span: Span::new(start, end.max(start)),
});
}
fn rules(&mut self, cdo_cdc: bool) -> Vec<Rule<'a>> {
let mut rules = Vec::new();
while let Some(next) = self.peek() {
match next {
ComponentValue::Token(Token::Whitespace, _) => {
self.next();
}
ComponentValue::Token(Token::Cdo | Token::Cdc, _) if cdo_cdc => {
self.next();
}
ComponentValue::Token(Token::AtKeyword(_), _) => {
let rule = self.at_rule(false);
rules.push(Rule::At(rule));
}
_ => {
if let Some(rule) = self.qualified_rule(false, false) {
rules.push(Rule::Qualified(rule));
}
}
}
}
rules
}
fn at_rule(&mut self, nested: bool) -> AtRule<'a> {
let Some(ComponentValue::Token(Token::AtKeyword(name), span)) = self.next() else {
unreachable!("at_rule is called on an at-keyword");
};
let mut rule = AtRule {
name,
prelude: Vec::new(),
block: None,
span,
};
while let Some(next) = self.next() {
match next {
ComponentValue::Token(Token::Semicolon, _) => break,
ComponentValue::Token(Token::RightBrace, _) if nested => {
self.back.push(next);
break;
}
ComponentValue::Block(block) if block.bracket == Bracket::Curly => {
rule.span.end = block.span.end;
rule.block = Some(block);
break;
}
other => {
rule.span.end = other.span().end;
self.scratch.push(other);
}
}
}
rule.prelude = take_tail(&mut self.scratch, 0);
rule
}
fn qualified_rule(&mut self, stop: bool, nested: bool) -> Option<QualifiedRule<'a>> {
let end_of_input = self.src.len();
let start = self.peek().map_or(end_of_input, |v| v.span().start);
self.scratch.clear();
let mut end = start;
loop {
let Some(next) = self.next() else {
self.scratch.clear();
self.error(ErrorKind::InvalidRule, start, end);
return None;
};
match next {
ComponentValue::Token(Token::Semicolon, _) if stop => {
self.back.push(next);
self.scratch.clear();
self.error(ErrorKind::InvalidRule, start, end);
return None;
}
ComponentValue::Token(Token::RightBrace, _) if nested => {
self.back.push(next);
self.scratch.clear();
self.error(ErrorKind::InvalidRule, start, end);
return None;
}
ComponentValue::Block(block) if block.bracket == Bracket::Curly => {
let end = block.span.end;
let prelude = take_tail(&mut self.scratch, 0);
if looks_like_custom_property(&prelude) {
if nested {
self.bad_declaration_remnants(true);
}
self.error(ErrorKind::InvalidRule, start, end);
return None;
}
return Some(QualifiedRule {
prelude,
block,
span: Span::new(start, end),
});
}
other => {
end = other.span().end;
self.scratch.push(other);
}
}
}
}
fn bad_declaration_remnants(&mut self, nested: bool) {
while let Some(next) = self.next() {
match next {
ComponentValue::Token(Token::Semicolon, _) => return,
ComponentValue::Token(Token::RightBrace, _) if nested => {
self.back.push(next);
return;
}
_ => {}
}
}
}
fn up_to_semicolon(&mut self, nested: bool) -> Vec<ComponentValue<'a>> {
let mut values = Vec::new();
while let Some(next) = self.peek() {
if next.is(&Token::Semicolon) || (nested && next.is(&Token::RightBrace)) {
break;
}
values.push(self.next().unwrap());
}
values
}
fn declaration(&mut self, nested: bool) -> Result<Declaration<'a>, Vec<ComponentValue<'a>>> {
if self.back.is_empty()
&& let Some(rest) = self.input.rest()
{
let n = before_semicolon(rest, nested);
if let Some(plan) = plan(&rest[..n]) {
let name = self.input.next().unwrap();
for _ in 1..plan.value_start {
self.input.next();
}
let value = self
.input
.by_ref()
.take(plan.value_end - plan.value_start)
.collect();
for _ in plan.value_end.max(plan.value_start)..n {
self.input.next();
}
return Ok(build(name, value, &plan, self.src));
}
return Err(self.input.by_ref().take(n).collect());
}
let values = self.up_to_semicolon(nested);
declaration_from(values, self.src)
}
fn block_contents(&mut self) -> Vec<Item<'a>> {
let mut items = Vec::new();
while let Some(next) = self.peek() {
match next {
ComponentValue::Token(Token::Whitespace | Token::Semicolon, _) => {
self.next();
}
ComponentValue::Token(Token::RightBrace, span) => {
let span = *span;
self.next();
self.error(ErrorKind::InvalidRule, span.start, span.end);
}
ComponentValue::Token(Token::AtKeyword(_), _) => {
let rule = self.at_rule(true);
items.push(Item::Rule(Rule::At(rule)));
}
ComponentValue::Token(Token::Ident(_), _) => match self.declaration(true) {
Ok(decl) => items.push(Item::Declaration(decl)),
Err(values) => {
self.put_back(values);
if let Some(rule) = self.qualified_rule(true, true) {
items.push(Item::Rule(Rule::Qualified(rule)));
}
}
},
_ => {
if let Some(rule) = self.qualified_rule(true, true) {
items.push(Item::Rule(Rule::Qualified(rule)));
}
}
}
}
items
}
fn declaration_list(&mut self) -> Vec<Item<'a>> {
let mut items = Vec::new();
while let Some(next) = self.peek() {
match next {
ComponentValue::Token(Token::Whitespace | Token::Semicolon, _) => {
self.next();
}
ComponentValue::Token(Token::AtKeyword(_), _) => {
let rule = self.at_rule(false);
items.push(Item::Rule(Rule::At(rule)));
}
_ => match self.declaration(false) {
Ok(decl) => items.push(Item::Declaration(decl)),
Err(values) => {
let start = values.first().map_or(0, |v| v.span().start);
let end = values.last().map_or(start, |v| v.span().end);
self.error(ErrorKind::InvalidDeclaration, start, end);
}
},
}
}
items
}
fn end<T>(&mut self, value: T) -> Result<T, ParseError> {
self.skip_whitespace();
match self.peek() {
None => Ok(value),
Some(v) => {
let start = v.span().start;
Err(ParseError {
kind: ErrorKind::ExtraInput,
span: Span::new(start, self.src.len()),
})
}
}
}
fn start(&mut self) -> Result<(), ParseError> {
self.skip_whitespace();
match self.peek() {
Some(_) => Ok(()),
None => Err(ParseError {
kind: ErrorKind::Empty,
span: Span::new(0, self.src.len()),
}),
}
}
}
pub(crate) fn looks_like_custom_property(prelude: &[ComponentValue<'_>]) -> bool {
let mut it = prelude.iter().filter(|v| !v.is_whitespace());
matches!(it.next(), Some(ComponentValue::Token(Token::Ident(n), _)) if n.starts_with("--"))
&& it.next().is_some_and(|v| v.is(&Token::Colon))
}
struct Plan {
value_start: usize,
value_end: usize,
important: bool,
unicode_range: bool,
span: Span,
}
fn plan(values: &[ComponentValue<'_>]) -> Option<Plan> {
let Some(ComponentValue::Token(Token::Ident(name), first)) = values.first() else {
return None;
};
let mut colon = 1;
while values.get(colon).is_some_and(ComponentValue::is_whitespace) {
colon += 1;
}
let colon_end = match values.get(colon) {
Some(ComponentValue::Token(Token::Colon, span)) => span.end,
_ => return None,
};
let after = colon + 1;
let v = value_plan(name, &values[after..])?;
Some(Plan {
value_start: after + v.start,
value_end: after + v.end,
important: v.important,
unicode_range: name.eq_ignore_ascii_case("unicode-range"),
span: Span::new(first.start, v.last_end.unwrap_or(colon_end)),
})
}
pub(crate) struct ValuePlan {
pub(crate) start: usize,
pub(crate) end: usize,
pub(crate) important: bool,
pub(crate) last_end: Option<usize>,
}
pub(crate) fn value_plan(name: &str, values: &[ComponentValue<'_>]) -> Option<ValuePlan> {
let mut start = 0;
while values.get(start).is_some_and(ComponentValue::is_whitespace) {
start += 1;
}
let mut solid = (start..values.len())
.rev()
.filter(|&i| !values[i].is_whitespace());
let last = solid.next();
let before = solid.next();
let important = match (before, last) {
(Some(bang), Some(word))
if values[bang].is(&Token::Delim('!'))
&& matches!(&values[word], ComponentValue::Token(Token::Ident(w), _)
if w.eq_ignore_ascii_case("important")) =>
{
Some(bang)
}
_ => None,
};
let mut end = important.unwrap_or(values.len());
while end > start && values[end - 1].is_whitespace() {
end -= 1;
}
if !name.starts_with("--") {
let value = &values[start..end];
if value.len() > 1 && value.iter().any(ComponentValue::is_curly) {
return None;
}
}
Some(ValuePlan {
start,
end,
important: important.is_some(),
last_end: last.map(|i| values[i].span().end),
})
}
fn build<'a>(
name: ComponentValue<'a>,
mut value: Vec<ComponentValue<'a>>,
plan: &Plan,
src: &'a str,
) -> Declaration<'a> {
let ComponentValue::Token(Token::Ident(name), _) = name else {
unreachable!("a plan starts with an ident")
};
if plan.unicode_range && !value.is_empty() {
let from = value[0].span().start;
let to = value[value.len() - 1].span().end;
value = Values::new(unicode_range_tokens(src, from, to)).collect();
}
Declaration {
name,
value,
important: plan.important,
span: plan.span,
}
}
fn declaration_from<'a>(
mut values: Vec<ComponentValue<'a>>,
src: &'a str,
) -> Result<Declaration<'a>, Vec<ComponentValue<'a>>> {
let Some(plan) = plan(&values) else {
return Err(values);
};
let blank = ComponentValue::Token(Token::Whitespace, Span::default());
let name = std::mem::replace(&mut values[0], blank);
values.truncate(plan.value_end);
values.drain(..plan.value_start);
Ok(build(name, values, &plan, src))
}
pub(crate) fn unicode_range_tokens(src: &str, from: usize, to: usize) -> Tokenizer<'_> {
let mut t = Tokenizer::with_unicode_ranges(&src[..to]);
t.reset(from);
t
}
pub fn parse_stylesheet(css: &str) -> Stylesheet<'_> {
let mut p = text(css);
let rules = p.rules(true);
Stylesheet {
rules,
errors: p.errors,
}
}
pub fn parse_rule_list(css: &str) -> Stylesheet<'_> {
let mut p = text(css);
let rules = p.rules(false);
Stylesheet {
rules,
errors: p.errors,
}
}
pub fn parse_block_contents(css: &str) -> BlockContents<'_> {
let mut p = text(css);
let items = p.block_contents();
BlockContents {
items,
errors: p.errors,
}
}
pub fn parse_declaration_list(css: &str) -> BlockContents<'_> {
let mut p = text(css);
let items = p.declaration_list();
BlockContents {
items,
errors: p.errors,
}
}
pub fn parse_rule(css: &str) -> Result<Rule<'_>, ParseError> {
let mut p = text(css);
p.start()?;
let rule = if matches!(
p.peek(),
Some(ComponentValue::Token(Token::AtKeyword(_), _))
) {
Rule::At(p.at_rule(false))
} else {
match p.qualified_rule(false, false) {
Some(rule) => Rule::Qualified(rule),
None => return Err(p.errors.pop().unwrap()),
}
};
p.end(rule)
}
pub fn parse_declaration(css: &str) -> Result<Declaration<'_>, ParseError> {
let mut p = text(css);
p.start()?;
match p.declaration(false) {
Ok(decl) => p.end(decl),
Err(values) => {
let start = values.first().map_or(0, |v| v.span().start);
Err(ParseError {
kind: ErrorKind::InvalidDeclaration,
span: Span::new(start, css.len()),
})
}
}
}
pub fn parse_component_value(css: &str) -> Result<ComponentValue<'_>, ParseError> {
let mut p = text(css);
p.start()?;
let value = p.next().unwrap();
p.end(value)
}
pub fn parse_component_values(css: &str) -> Vec<ComponentValue<'_>> {
Values::new(Tokenizer::new(css)).collect()
}
pub fn parse_comma_separated(css: &str) -> Vec<Vec<ComponentValue<'_>>> {
let mut values = Values::new(Tokenizer::new(css)).peekable();
let mut groups = Vec::new();
while values.peek().is_some() {
let mut group = Vec::new();
for v in values.by_ref() {
if v.is(&Token::Comma) {
break;
}
group.push(v);
}
groups.push(group);
}
groups
}
pub fn parse_unicode_range_value(css: &str) -> Vec<ComponentValue<'_>> {
Values::new(Tokenizer::with_unicode_ranges(css)).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deep_nesting_parses_and_drops() {
let css = "(".repeat(1_000_000);
let values = parse_component_values(&css);
assert_eq!(values.len(), 1);
drop(values);
let css = format!("a{{b:{}}}", "[".repeat(200_000));
let sheet = parse_stylesheet(&css);
let Rule::Qualified(rule) = sheet.rules.into_iter().next().unwrap() else {
panic!()
};
assert_eq!(rule.block.contents(&css).items.len(), 1);
}
#[test]
fn spans_cover_rules_and_declarations() {
let css = "a { color: red !important; } @x y;";
let sheet = parse_stylesheet(css);
assert_eq!(
sheet.rules[0].span().text(css),
"a { color: red !important; }"
);
assert_eq!(
sheet.rules[1].span().text(css),
"@x y;".trim_end_matches(';')
);
let Rule::Qualified(rule) = sheet.rules.into_iter().next().unwrap() else {
panic!()
};
let Item::Declaration(d) = &rule.block.contents(css).items[0] else {
panic!()
};
assert_eq!(d.span.text(css), "color: red !important");
assert_eq!(d.original_text(css), "red");
assert!(d.important);
}
#[test]
fn block_contents_parsed_later_match_parsing_at_once() {
let css = "color: red; &:hover { b: c } --x: {a} b; d:e{f}; @media x { g: h }";
let at_once = parse_block_contents(css);
let wrapped = format!("{{{css}}}");
let Ok(ComponentValue::Block(block)) = parse_component_value(&wrapped) else {
panic!()
};
let later = block.contents(&wrapped);
assert_eq!(at_once.items.len(), later.items.len());
assert_eq!(at_once.errors.len(), later.errors.len());
}
#[test]
fn unicode_range_descriptor_gets_range_tokens() {
let d = parse_declaration("unicode-range: U+0-7F, u+4??").unwrap();
let ranges: Vec<_> = d
.value
.iter()
.filter_map(|v| match v.token() {
Some(Token::UnicodeRange { start, end }) => Some((*start, *end)),
_ => None,
})
.collect();
assert_eq!(ranges, [(0, 0x7f), (0x400, 0x4ff)]);
}
}