Print quotes properly when string contains quotes
This commit is contained in:
parent
6c5cf4b405
commit
9f81efe812
@ -172,7 +172,6 @@ pub(crate) fn parse_quoted_string<I: Iterator<Item = Token>>(
|
||||
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<I: Iterator<Item = Token>>(
|
||||
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<I: Iterator<Item = Token>>(
|
||||
is_escaped = false;
|
||||
s.push('\\');
|
||||
continue;
|
||||
},
|
||||
}
|
||||
TokenKind::Interpolation if !is_escaped => {
|
||||
found_interpolation = true;
|
||||
s.push_str(
|
||||
|
@ -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"),
|
||||
|
@ -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"
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user