From 9f81efe81202e8adf8dc77120566489923561d1d Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 24 Feb 2020 19:49:24 -0500 Subject: [PATCH] Print quotes properly when string contains quotes --- src/utils.rs | 4 +--- src/value/mod.rs | 33 +++++++++++++++++++++++++++++++-- tests/values.rs | 5 +++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 14948fb..673628c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -172,7 +172,6 @@ pub(crate) fn parse_quoted_string>( break } TokenKind::Symbol(Symbol::DoubleQuote) if is_escaped => { - s.push('\\'); s.push('"'); is_escaped = false; continue; @@ -183,7 +182,6 @@ pub(crate) fn parse_quoted_string>( break } TokenKind::Symbol(Symbol::SingleQuote) if is_escaped => { - s.push('\\'); s.push('\''); is_escaped = false; continue; @@ -193,7 +191,7 @@ pub(crate) fn parse_quoted_string>( is_escaped = false; s.push('\\'); continue; - }, + } TokenKind::Interpolation if !is_escaped => { found_interpolation = true; s.push_str( diff --git a/src/value/mod.rs b/src/value/mod.rs index 398d513..20af9c0 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -1,5 +1,5 @@ #![allow(dead_code, unused_variables)] -use std::fmt::{self, Display}; +use std::fmt::{self, Display, Write}; use std::iter::Iterator; use crate::color::Color; @@ -42,7 +42,36 @@ impl Display for Value { Self::Color(c) => write!(f, "{}", c), Self::BinaryOp(..) => write!(f, "{}", self.clone().eval().unwrap()), Self::Paren(val) => write!(f, "{}", val), - Self::Ident(val, kind) => write!(f, "{}{}{}", kind.as_str(), val, kind.as_str()), + Self::Ident(val, kind) => { + if kind == &QuoteKind::None { + return write!(f, "{}", val); + } + let has_single_quotes = val.contains(|x| x == '\''); + let has_double_quotes = val.contains(|x| x == '"'); + if has_single_quotes && !has_double_quotes { + write!(f, "\"{}\"", val) + } else if !has_single_quotes && has_double_quotes { + write!(f, "'{}'", val) + } else { + let quote_char = match kind { + QuoteKind::Double => '"', + QuoteKind::Single => '\'', + _ => unreachable!(), + }; + f.write_char(quote_char)?; + for c in val.chars() { + match c { + '"' | '\'' if c == quote_char => { + f.write_char('\\')?; + f.write_char(quote_char)?; + } + v => f.write_char(v)?, + } + } + f.write_char(quote_char)?; + Ok(()) + } + } Self::True => write!(f, "true"), Self::False => write!(f, "false"), Self::Null => write!(f, "null"), diff --git a/tests/values.rs b/tests/values.rs index 658dbe9..6824abd 100644 --- a/tests/values.rs +++ b/tests/values.rs @@ -355,3 +355,8 @@ test!( "a {\n color: \"\\\\\";\n}\n", "a {\n color: \"\\\";\n}\n" ); +test!( + double_quotes_when_containing_single_quote, + "a {\n color: '\\\'';\n}\n", + "a {\n color: \"'\";\n}\n" +);