Write escapes when serializing unquoted strings.

This commit is contained in:
Natalie Weizenbaum 2016-10-17 14:55:02 -07:00
parent beb5cc1794
commit d5aa9e7f82

View File

@ -476,19 +476,18 @@ class _SerializeCssVisitor
void visitString(SassString string) { void visitString(SassString string) {
if (_quote && string.hasQuotes) { if (_quote && string.hasQuotes) {
_visitString(string.text); _visitQuotedString(string.text);
} else { } else {
// TODO: write this character-by-character. _visitUnquotedString(string.text);
_buffer.write(string.text.replaceAll("\n", " "));
} }
} }
/// Writes [string] to [_buffer]. /// Writes a quoted string with [string] contents to [_buffer].
/// ///
/// By default, this detects which type of quote to use based on the contents /// By default, this detects which type of quote to use based on the contents
/// of the string. If [forceDoubleQuote] is `true`, this always uses a double /// of the string. If [forceDoubleQuote] is `true`, this always uses a double
/// quote. /// quote.
void _visitString(String string, {bool forceDoubleQuote: false}) { void _visitQuotedString(String string, {bool forceDoubleQuote: false}) {
var includesSingleQuote = false; var includesSingleQuote = false;
var includesDoubleQuote = false; var includesDoubleQuote = false;
@ -501,7 +500,7 @@ class _SerializeCssVisitor
if (forceDoubleQuote) { if (forceDoubleQuote) {
buffer.writeCharCode($single_quote); buffer.writeCharCode($single_quote);
} else if (includesDoubleQuote) { } else if (includesDoubleQuote) {
_visitString(string, forceDoubleQuote: true); _visitQuotedString(string, forceDoubleQuote: true);
return; return;
} else { } else {
includesSingleQuote = true; includesSingleQuote = true;
@ -514,7 +513,7 @@ class _SerializeCssVisitor
buffer.writeCharCode($backslash); buffer.writeCharCode($backslash);
buffer.writeCharCode($double_quote); buffer.writeCharCode($double_quote);
} else if (includesSingleQuote) { } else if (includesSingleQuote) {
_visitString(string, forceDoubleQuote: true); _visitQuotedString(string, forceDoubleQuote: true);
return; return;
} else { } else {
includesDoubleQuote = true; includesDoubleQuote = true;
@ -556,6 +555,27 @@ class _SerializeCssVisitor
} }
} }
/// Writes an unquoted string with [string] contents to [_buffer].
void _visitUnquotedString(String string) {
for (var i = 0; i < string.length; i++) {
var char = string.codeUnitAt(i);
switch (char) {
case $backslash:
_buffer.writeCharCode($backslash);
_buffer.writeCharCode($backslash);
break;
case $lf:
_buffer.writeCharCode($space);
break;
default:
_buffer.writeCharCode(char);
break;
}
}
}
// ## Selectors // ## Selectors
void visitAttributeSelector(AttributeSelector attribute) { void visitAttributeSelector(AttributeSelector attribute) {