fix: Ignore CSS colors

Fixes #462
This commit is contained in:
Ed Page 2022-04-18 09:19:42 -05:00
parent 3e802f8100
commit e63659c208

View file

@ -189,6 +189,7 @@ mod parser {
terminated(base64_literal, sep1), terminated(base64_literal, sep1),
terminated(email_literal, sep1), terminated(email_literal, sep1),
terminated(url_literal, sep1), terminated(url_literal, sep1),
terminated(css_color, sep1),
c_escape, c_escape,
printf, printf,
other, other,
@ -283,6 +284,27 @@ mod parser {
)(input) )(input)
} }
fn css_color<T>(input: T) -> IResult<T, T>
where
T: nom::InputTakeAtPosition
+ nom::InputTake
+ nom::InputIter
+ nom::InputLength
+ nom::Slice<std::ops::RangeFrom<usize>>
+ Clone
+ std::fmt::Debug,
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
<T as nom::InputIter>::Item: AsChar + Copy,
{
preceded(
char('#'),
alt((
take_while_m_n(3, 8, is_lower_hex_digit),
take_while_m_n(3, 8, is_upper_hex_digit),
)),
)(input)
}
fn uuid_literal<T>(input: T) -> IResult<T, T> fn uuid_literal<T>(input: T) -> IResult<T, T>
where where
T: nom::InputTakeAtPosition T: nom::InputTakeAtPosition
@ -620,8 +642,13 @@ mod parser {
#[inline] #[inline]
fn is_ignore_char(i: impl AsChar + Copy) -> bool { fn is_ignore_char(i: impl AsChar + Copy) -> bool {
let c = i.as_char(); let c = i.as_char();
// See c_escape and printf !unicode_xid::UnicodeXID::is_xid_continue(c) &&
!unicode_xid::UnicodeXID::is_xid_continue(c) && c != '\\' && c != '%' // See c_escape
c != '\\' &&
// See printf
c != '%' &&
// See css_color
c != '#'
} }
#[inline] #[inline]
@ -1248,6 +1275,23 @@ mod test {
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
#[test]
fn tokenize_color() {
let parser = TokenizerBuilder::new().build();
let input = "#[derive(Clone)] #aaa # #111 #AABBCC #hello #AABBCCDD World";
let expected: Vec<Identifier> = vec![
Identifier::new_unchecked("derive", Case::None, 2),
Identifier::new_unchecked("Clone", Case::None, 9),
Identifier::new_unchecked("hello", Case::None, 38),
Identifier::new_unchecked("World", Case::None, 54),
];
let actual: Vec<_> = parser.parse_bytes(input.as_bytes()).collect();
assert_eq!(expected, actual);
let actual: Vec<_> = parser.parse_str(input).collect();
assert_eq!(expected, actual);
}
#[test] #[test]
fn tokenize_template() { fn tokenize_template() {
let parser = TokenizerBuilder::new().build(); let parser = TokenizerBuilder::new().build();