more accurately parse strange hex colors

This commit is contained in:
ConnorSkees 2020-06-18 00:42:43 -04:00
parent 76290a4159
commit 5d5e7adb98
2 changed files with 28 additions and 10 deletions

View File

@ -528,15 +528,16 @@ impl<'a> Parser<'a> {
fn parse_hex(&mut self) -> SassResult<Spanned<Value>> { fn parse_hex(&mut self) -> SassResult<Spanned<Value>> {
let mut s = String::with_capacity(7); let mut s = String::with_capacity(7);
s.push('#'); s.push('#');
if self let first_char = self
.toks .toks
.peek() .peek()
.ok_or(("Expected identifier.", self.span_before))? .ok_or(("Expected identifier.", self.span_before))?
.kind .kind;
.is_ascii_hexdigit() let first_is_digit = first_char.is_ascii_digit();
{ let first_is_hexdigit = first_char.is_ascii_hexdigit();
if first_is_digit {
while let Some(c) = self.toks.peek() { while let Some(c) = self.toks.peek() {
if !c.kind.is_ascii_hexdigit() { if !c.kind.is_ascii_hexdigit() || s.len() == 9 {
break; break;
} }
let tok = self.toks.next().unwrap(); let tok = self.toks.next().unwrap();
@ -548,10 +549,17 @@ impl<'a> Parser<'a> {
// that is, `#ooobar`. // that is, `#ooobar`.
} else { } else {
let ident = self.parse_identifier()?; let ident = self.parse_identifier()?;
return Ok(Spanned { if first_is_hexdigit
node: Value::String(format!("#{}", ident.node), QuoteKind::None), && ident.node.chars().all(|c| c.is_ascii_hexdigit())
span: ident.span, && matches!(ident.node.len(), 3 | 4 | 6 | 8)
}); {
s.push_str(&ident.node);
} else {
return Ok(Spanned {
node: Value::String(format!("#{}", ident.node), QuoteKind::None),
span: ident.span,
});
}
} }
let v = match u32::from_str_radix(&s[1..], 16) { let v = match u32::from_str_radix(&s[1..], 16) {
Ok(a) => a, Ok(a) => a,

View File

@ -593,7 +593,17 @@ test!(
"a {\n color: #ooobar;\n}\n" "a {\n color: #ooobar;\n}\n"
); );
test!( test!(
more_than_8_hex_chars_after_hash, more_than_8_hex_chars_after_hash_starts_with_letter,
"a {\n color: #ffffffffff;\n}\n", "a {\n color: #ffffffffff;\n}\n",
"a {\n color: #ffffffffff;\n}\n" "a {\n color: #ffffffffff;\n}\n"
); );
test!(
more_than_8_hex_chars_after_hash_starts_with_number,
"a {\n color: #0000000000;\n}\n",
"a {\n color: #00000000 0;\n}\n"
);
test!(
more_than_8_hex_chars_after_hash_starts_with_number_contains_hex_char,
"a {\n color: #00000000f00;\n}\n",
"a {\n color: #00000000 f00;\n}\n"
);