Merge pull request #463 from epage/color

fix: Ignore CSS colors
This commit is contained in:
Ed Page 2022-04-18 09:36:28 -05:00 committed by GitHub
commit e4b0f4df00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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();