more robustly parse @else if

This commit is contained in:
Connor Skees 2021-07-20 23:55:18 -04:00
parent b4c346f51f
commit 26ec16a802
3 changed files with 37 additions and 11 deletions

View File

@ -69,13 +69,16 @@ impl<'a> Parser<'a> {
self.whitespace_or_comment(); self.whitespace_or_comment();
if let Some(tok) = self.toks.peek() { if let Some(tok) = self.toks.peek() {
match tok.kind { match tok.kind {
'i' if matches!( 'i' | 'I' | '\\' => {
self.toks.peek_forward(1), self.span_before = tok.pos;
Some(Token { kind: 'f', .. }) | Some(Token { kind: 'F', .. }) let mut ident = self.parse_identifier_no_interpolation(false)?;
) =>
{ ident.node.make_ascii_lowercase();
self.toks.next();
self.toks.next(); if ident.node != "if" {
return Err(("expected \"{\".", ident.span).into());
}
let cond = if found_true { let cond = if found_true {
self.throw_away_until_open_curly_brace()?; self.throw_away_until_open_curly_brace()?;
false false
@ -84,6 +87,7 @@ impl<'a> Parser<'a> {
self.expect_char('{')?; self.expect_char('{')?;
v v
}; };
if cond { if cond {
found_true = true; found_true = true;
self.scopes.enter_new_scope(); self.scopes.enter_new_scope();

View File

@ -18,11 +18,9 @@ test!(
); );
error!( error!(
invalid_charset_value, invalid_charset_value,
"@charset 1;", "@charset 1;", "Error: Expected string."
"Error: Expected string."
); );
error!( error!(
invalid_charset_value_unquoted_string, invalid_charset_value_unquoted_string,
"@charset a;", "@charset a;", "Error: Expected string."
"Error: Expected string."
); );

View File

@ -236,3 +236,27 @@ error!(
denies_interpolated_at_rule, denies_interpolated_at_rule,
"@#{if} true { a { color: red; } }", "Error: expected \"(\"." "@#{if} true { a { color: red; } }", "Error: expected \"(\"."
); );
test!(
else_if_escaped_lower_i,
r"@if false {
} @else \69 f true {
/**/
}",
"/**/\n"
);
test!(
else_if_escaped_lower_both,
r"@if false {
} @else \69 \66 true {
/**/
}",
"/**/\n"
);
test!(
else_if_uppercase,
r"@if false {
} @else IF true {
/**/
}",
"/**/\n"
);