From 26ec16a80241965f5ac9cec53539d81c6bac5a2b Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Tue, 20 Jul 2021 23:55:18 -0400 Subject: [PATCH] more robustly parse `@else if` --- src/parse/control_flow.rs | 18 +++++++++++------- tests/charset.rs | 6 ++---- tests/if.rs | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/parse/control_flow.rs b/src/parse/control_flow.rs index be74169..ea22886 100644 --- a/src/parse/control_flow.rs +++ b/src/parse/control_flow.rs @@ -69,13 +69,16 @@ impl<'a> Parser<'a> { self.whitespace_or_comment(); if let Some(tok) = self.toks.peek() { match tok.kind { - 'i' if matches!( - self.toks.peek_forward(1), - Some(Token { kind: 'f', .. }) | Some(Token { kind: 'F', .. }) - ) => - { - self.toks.next(); - self.toks.next(); + 'i' | 'I' | '\\' => { + self.span_before = tok.pos; + let mut ident = self.parse_identifier_no_interpolation(false)?; + + ident.node.make_ascii_lowercase(); + + if ident.node != "if" { + return Err(("expected \"{\".", ident.span).into()); + } + let cond = if found_true { self.throw_away_until_open_curly_brace()?; false @@ -84,6 +87,7 @@ impl<'a> Parser<'a> { self.expect_char('{')?; v }; + if cond { found_true = true; self.scopes.enter_new_scope(); diff --git a/tests/charset.rs b/tests/charset.rs index 321a7c1..bcb7d02 100644 --- a/tests/charset.rs +++ b/tests/charset.rs @@ -18,11 +18,9 @@ test!( ); error!( invalid_charset_value, - "@charset 1;", - "Error: Expected string." + "@charset 1;", "Error: Expected string." ); error!( invalid_charset_value_unquoted_string, - "@charset a;", - "Error: Expected string." + "@charset a;", "Error: Expected string." ); diff --git a/tests/if.rs b/tests/if.rs index 0d9f3a2..71d4a46 100644 --- a/tests/if.rs +++ b/tests/if.rs @@ -236,3 +236,27 @@ error!( denies_interpolated_at_rule, "@#{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" +);