From 57a704172fa040497d114ec2783b07e465e59b7e Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 30 Mar 2020 02:21:41 -0400 Subject: [PATCH] properly handle silent comments in styles --- src/lib.rs | 4 ++-- src/style.rs | 7 ++++--- src/utils.rs | 33 +++++++++++++++++++++++++++++++++ src/value/parse.rs | 4 ++-- tests/comments.rs | 4 ++-- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2638ca9..bf70fe5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,8 +96,8 @@ use crate::selector::Selector; use crate::style::Style; pub(crate) use crate::token::Token; use crate::utils::{ - devour_whitespace, eat_comment, eat_ident, eat_variable_value, parse_quoted_string, - read_until_newline, VariableDecl, eat_ident_no_interpolation + devour_whitespace, eat_comment, eat_ident, eat_ident_no_interpolation, eat_variable_value, + parse_quoted_string, read_until_newline, VariableDecl, }; use crate::value::Value; diff --git a/src/style.rs b/src/style.rs index 2d9fae5..466905b 100644 --- a/src/style.rs +++ b/src/style.rs @@ -5,7 +5,8 @@ use crate::error::SassResult; use crate::scope::Scope; use crate::selector::Selector; use crate::utils::{ - devour_whitespace, eat_ident, read_until_semicolon_or_open_or_closing_curly_brace, + devour_whitespace, devour_whitespace_or_comment, eat_ident, + read_until_semicolon_or_open_or_closing_curly_brace, }; use crate::value::Value; use crate::{Expr, Token}; @@ -192,10 +193,10 @@ impl<'a> StyleParser<'a> { ) -> SassResult { devour_whitespace(toks); let property = eat_ident(toks, &self.scope, &self.super_selector)?; - devour_whitespace(toks); + devour_whitespace_or_comment(toks)?; if toks.peek().is_some() && toks.peek().unwrap().kind == ':' { toks.next(); - devour_whitespace(toks); + devour_whitespace_or_comment(toks)?; } if super_property.is_empty() { diff --git a/src/utils.rs b/src/utils.rs index a317f8a..b6b7a7f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -129,11 +129,20 @@ pub(crate) fn read_until_open_curly_brace>( match tok.kind { '{' => n += 1, '}' => n -= 1, + '/' => { + let next = toks.next().unwrap(); + match toks.peek().unwrap().kind { + '/' => read_until_newline(toks), + _ => val.push(next), + }; + continue; + } _ => {} } if n == 1 { break; } + val.push(toks.next().unwrap()); } val @@ -162,6 +171,14 @@ pub(crate) fn read_until_closing_curly_brace>( t.push(toks.next().unwrap()); } } + '/' => { + let next = toks.next().unwrap(); + match toks.peek().unwrap().kind { + '/' => read_until_newline(toks), + _ => t.push(next), + }; + continue; + } _ => t.push(toks.next().unwrap()), } } @@ -245,6 +262,14 @@ pub(crate) fn read_until_semicolon_or_closing_curly_brace { + let next = toks.next().unwrap(); + match toks.peek().unwrap().kind { + '/' => read_until_newline(toks), + _ => t.push(next), + }; + continue; + } _ => t.push(toks.next().unwrap()), } } @@ -296,6 +321,14 @@ pub(crate) fn read_until_semicolon_or_open_or_closing_curly_brace { + let next = toks.next().unwrap(); + match toks.peek().unwrap().kind { + '/' => read_until_newline(toks), + _ => t.push(next), + }; + continue; + } _ => t.push(toks.next().unwrap()), } } diff --git a/src/value/parse.rs b/src/value/parse.rs index 3a35ba4..972ff0b 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -14,8 +14,8 @@ use crate::scope::Scope; use crate::selector::Selector; use crate::unit::Unit; use crate::utils::{ - devour_whitespace, eat_comment, eat_ident, eat_number, parse_interpolation, - parse_quoted_string, read_until_newline, eat_ident_no_interpolation + devour_whitespace, eat_comment, eat_ident, eat_ident_no_interpolation, eat_number, + parse_interpolation, parse_quoted_string, read_until_newline, }; use crate::value::Value; use crate::Token; diff --git a/tests/comments.rs b/tests/comments.rs index f09729a..b535bfa 100644 --- a/tests/comments.rs +++ b/tests/comments.rs @@ -50,6 +50,6 @@ test!( ); test!( converts_cr_in_comment, - "a {\n /* \r*/ color: red;\n}\n", - "a {\n /* \n*/\n color: red;\n}\n" + "a {\n color: 1 + // flang }\n blang }", + "a {\n color: 1blang;\n}\n" );