Properly remove quotes when stringifying.

This commit is contained in:
Natalie Weizenbaum 2016-10-16 19:13:13 -07:00
parent 7f6c70f821
commit beb5cc1794
3 changed files with 24 additions and 12 deletions

View File

@ -286,7 +286,9 @@ abstract class Value {
/// Throws a [SassScriptException] if [this] can't be represented in plain
/// CSS. Use [toString] instead to get a string representation even if this
/// isn't valid CSS.
String toCssString() => valueToCss(this);
///
/// If [quote] is `false`, quoted strings are emitted without quotes.
String toCssString({bool quote: true}) => valueToCss(this, quote: quote);
/// Returns a string representation of [this].
///

View File

@ -596,7 +596,7 @@ class _PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
} else if (condition is SupportsNegation) {
return "not ${_parenthesize(condition.condition)}";
} else if (condition is SupportsInterpolation) {
return condition.expression.accept(this).toCssString();
return condition.expression.accept(this).toCssString(quote: false);
} else if (condition is SupportsDeclaration) {
return "(${condition.name.accept(this).toCssString()}: "
"${condition.value.accept(this).toCssString()})";
@ -627,10 +627,12 @@ class _PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
}
void visitWarnRule(WarnRule node) {
_addExceptionSpan(
node.span,
() => stderr
.writeln("WARNING: ${node.expression.accept(this).toCssString()}"));
_addExceptionSpan(node.span, () {
var value = node.expression.accept(this);
var string = value is SassString ? value.text : value.toCssString();
stderr.writeln("WARNING: $string");
});
for (var line in _stackTrace(node.span).toString().split("\n")) {
stderr.writeln(" $line");
}
@ -1120,7 +1122,7 @@ class _PerformVisitor implements StatementVisitor, ExpressionVisitor<Value> {
return interpolation.contents.map((value) {
if (value is String) return value;
var result = (value as Expression).accept(this);
return result is SassString ? result.text : result;
return result.toCssString(quote: false);
}).join();
}

View File

@ -46,8 +46,10 @@ String toCss(CssNode node, {OutputStyle style, bool inspect: false}) {
/// source structure. Note however that, although this will be valid SCSS, it
/// may not be valid CSS. If [inspect] is `false` and [value] can't be
/// represented in plain CSS, throws a [SassScriptException].
String valueToCss(Value value, {bool inspect: false}) {
var visitor = new _SerializeCssVisitor(inspect: inspect);
///
/// If [quote] is `false`, quoted strings are emitted without quotes.
String valueToCss(Value value, {bool inspect: false, bool quote: true}) {
var visitor = new _SerializeCssVisitor(inspect: inspect, quote: quote);
value.accept(visitor);
return visitor._buffer.toString();
}
@ -77,8 +79,13 @@ class _SerializeCssVisitor
/// structure, as opposed to valid CSS.
final bool _inspect;
_SerializeCssVisitor({OutputStyle style, bool inspect: false})
: _inspect = inspect;
/// Whether quoted strings should be emitted with quotes.
final bool _quote;
_SerializeCssVisitor(
{OutputStyle style, bool inspect: false, bool quote: true})
: _inspect = inspect,
_quote = quote;
void visitStylesheet(CssStylesheet node) {
for (var child in node.children) {
@ -468,9 +475,10 @@ class _SerializeCssVisitor
}
void visitString(SassString string) {
if (string.hasQuotes) {
if (_quote && string.hasQuotes) {
_visitString(string.text);
} else {
// TODO: write this character-by-character.
_buffer.write(string.text.replaceAll("\n", " "));
}
}