Skip to content

Commit 71e621c

Browse files
committed
handle parenthesised arbitrary values bg-(…)
1 parent 2fb8fef commit 71e621c

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

crates/oxide/src/parser.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ enum Arbitrary {
4949
/// E.g.: `bg-[…]`
5050
/// ^
5151
Brackets(usize),
52+
53+
/// In arbitrary value mode with parens
54+
///
55+
/// E.g.: `bg-(…)`
56+
/// ^
57+
Parens(usize),
5258
}
5359

5460
pub struct Extractor<'a> {
@@ -490,9 +496,29 @@ impl<'a> Extractor<'a> {
490496
self.bracket_stack.pop();
491497
}
492498

493-
// Last bracket is different compared to what we expect, therefore we are not in a
494-
// valid arbitrary value.
495-
_ if !self.in_quotes() => return ParseAction::Skip,
499+
// This is the last bracket meaning the end of arbitrary content
500+
_ if !self.in_quotes() => {
501+
if matches!(self.cursor.next, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9') {
502+
return ParseAction::Consume;
503+
}
504+
505+
if let Arbitrary::Parens(start_idx) = self.arbitrary {
506+
trace!("Arbitrary::End\t");
507+
self.arbitrary = Arbitrary::None;
508+
509+
if self.cursor.pos - start_idx == 1 {
510+
// We have an empty arbitrary value, which is not allowed
511+
return ParseAction::Skip;
512+
}
513+
514+
// We have a valid arbitrary value
515+
return ParseAction::Consume;
516+
}
517+
518+
// Last parenthesis is different compared to what we expect, therefore we are
519+
// not in a valid arbitrary value.
520+
return ParseAction::Skip;
521+
}
496522

497523
// We're probably in quotes or nested brackets, so we keep going
498524
_ => {}
@@ -544,7 +570,9 @@ impl<'a> Extractor<'a> {
544570
b' ' if !self.opts.preserve_spaces_in_arbitrary => {
545571
trace!("Arbitrary::SkipAndEndEarly\t");
546572

547-
if let Arbitrary::Brackets(start_idx) = self.arbitrary {
573+
if let Arbitrary::Brackets(start_idx) | Arbitrary::Parens(start_idx) =
574+
self.arbitrary
575+
{
548576
// Restart the parser ahead of the arbitrary value It may pick up more
549577
// candidates
550578
return ParseAction::RestartAt(start_idx + 1);
@@ -609,8 +637,15 @@ impl<'a> Extractor<'a> {
609637
self.arbitrary = Arbitrary::Brackets(self.cursor.pos);
610638
}
611639

640+
// Enter arbitrary value mode. E.g.: `bg-(--my-color)`
641+
// ^
642+
b'(' if matches!(self.cursor.prev, b'-' | b'/') => {
643+
trace!("Arbitrary::Start\t");
644+
self.arbitrary = Arbitrary::Parens(self.cursor.pos);
645+
}
646+
612647
// Can't enter arbitrary value mode. This can't be a candidate.
613-
b'[' => {
648+
b'[' | b'(' => {
614649
trace!("Arbitrary::Skip_Start\t");
615650
return ParseAction::Skip;
616651
}

0 commit comments

Comments
 (0)