From fba6f2eb73dab395836b4032f840ce869467f351 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 24 Feb 2020 18:58:09 -0500 Subject: [PATCH] Properly emit quotes after interpolation --- src/lib.rs | 7 +++++++ src/style.rs | 22 +++++++++++++++------- tests/interpolation.rs | 5 +++++ tests/values.rs | 2 ++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7a3f06a..8f5a5b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,6 +107,13 @@ impl Token { pos: Pos::new(), } } + + pub fn from_symbol(s: Symbol) -> Self { + Token { + kind: TokenKind::Symbol(s), + pos: Pos::new(), + } + } } impl IsWhitespace for Token { diff --git a/src/style.rs b/src/style.rs index 58dc245..16787ce 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,4 +1,4 @@ -use crate::common::{Pos, Scope, Symbol}; +use crate::common::{Pos, QuoteKind, Scope, Symbol}; use crate::error::SassResult; use crate::selector::Selector; use crate::utils::{devour_whitespace, parse_interpolation, parse_quoted_string}; @@ -87,12 +87,20 @@ impl<'a> StyleParser<'a> { ref q @ TokenKind::Symbol(Symbol::DoubleQuote) | ref q @ TokenKind::Symbol(Symbol::SingleQuote) => { let q = q.clone(); - let tok = toks.next().unwrap(); - style.push(tok.clone()); - style.push(Token::from_string( - parse_quoted_string(toks, scope, q)?.unquote().to_string(), - )); - style.push(tok); + toks.next(); + let (s, q) = if let Value::Ident(s, q) = parse_quoted_string(toks, scope, q)? { + (s, q) + } else { + unreachable!() + }; + let quote_kind = Token::from_symbol(match q { + QuoteKind::Single => Symbol::SingleQuote, + QuoteKind::Double => Symbol::DoubleQuote, + _ => unreachable!(), + }); + style.push(quote_kind.clone()); + style.push(Token::from_string(s)); + style.push(quote_kind); continue; } TokenKind::Symbol(Symbol::OpenCurlyBrace) diff --git a/tests/interpolation.rs b/tests/interpolation.rs index 93a5afc..318db4f 100644 --- a/tests/interpolation.rs +++ b/tests/interpolation.rs @@ -28,3 +28,8 @@ test!( "a {\n color: #{\"''\"};\n}\n", "a {\n color: '';\n}\n" ); +test!( + single_quotes_converted_to_double_when_interpolated, + "a {\n color: '#{foo}';\n}\n", + "a {\n color: \"foo\";\n}\n" +); diff --git a/tests/values.rs b/tests/values.rs index 35dc0a3..db76bd9 100644 --- a/tests/values.rs +++ b/tests/values.rs @@ -3,6 +3,8 @@ #[macro_use] mod macros; +test!(single_quote, "a {\n color: 'foo';\n}\n"); +test!(double_quote, "a {\n color: \"foo\";\n}\n"); test!(comma_list_ident, "a {\n color: foo, bar, baz;\n}\n"); test!(space_list_ident, "a {\n color: foo bar baz;\n}\n"); test!(comma_list_number, "a {\n color: 1, 2, 3;\n}\n");