From a7ca888942205e7492808b2c46688c4734e9d8dd Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 20 Jan 2020 18:09:25 -0500 Subject: [PATCH] Parse styles not ending in semicolons --- src/lib.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8abc369..2f0fee7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -244,7 +244,7 @@ impl StyleSheet { /// Used mainly in debugging, but can at times be useful #[inline] #[allow(dead_code)] - fn pretty_print(&self, buf: W) -> SassResult<()> { + pub fn pretty_print(&self, buf: W) -> SassResult<()> { PrettyPrinter::new(buf).pretty_print(self) } @@ -409,10 +409,10 @@ impl<'a> StyleSheetParser<'a> { fn eat_rules(&mut self, super_selector: &Selector, scope: &mut Scope) -> Vec { let mut stmts = Vec::new(); - while let Some(tok) = eat_expr(&mut self.lexer, scope, super_selector) + while let Some(expr) = eat_expr(&mut self.lexer, scope, super_selector) .unwrap_or_else(|error| self.error(error.0, &error.1)) { - match tok { + match expr { Expr::Style(s) => stmts.push(Stmt::Style(s)), Expr::MixinDecl(name, mixin) => { scope.mixins.insert(name, mixin); @@ -499,7 +499,7 @@ pub(crate) fn eat_expr>( let mut values = Vec::with_capacity(5); while let Some(tok) = toks.peek() { match &tok.kind { - TokenKind::Symbol(Symbol::SemiColon) | TokenKind::Symbol(Symbol::CloseCurlyBrace) => { + TokenKind::Symbol(Symbol::SemiColon) => { toks.next(); devour_whitespace(toks); return Ok(Some(Expr::Style(match Style::from_tokens(values, scope) { @@ -507,6 +507,17 @@ pub(crate) fn eat_expr>( Err(_) => return Ok(None), }))); } + TokenKind::Symbol(Symbol::CloseCurlyBrace) => { + if values.is_empty() { + toks.next(); + devour_whitespace(toks); + return Ok(None); + } + return Ok(Some(Expr::Style(match Style::from_tokens(values, scope) { + Ok(x) => x, + Err(_) => return Ok(None), + }))); + } TokenKind::Symbol(Symbol::OpenCurlyBrace) => { toks.next(); devour_whitespace(toks); @@ -972,6 +983,11 @@ mod test_styles { two_rulesets, "a {\n color: red;\n}\nc {\n color: white;\n}\n" ); + test!( + two_rulesets_first_no_semicolon, + "a {\n color: red\n}\nc {\n color: white;\n}\n", + "a {\n color: red;\n}\nc {\n color: white;\n}\n" + ); test!( two_inner_outer_rulesets, "a {\n b {\n color: red;\n}\n c {\n color: white;\n}\n}\na {\n b {\n color: red;\n}\n c {\n color: white;\n}\n}\n",