Don't crash on a fully-interpolated CSS variable name (#177)

Closes #176
This commit is contained in:
Natalie Weizenbaum 2017-10-04 15:20:15 -07:00 committed by GitHub
parent f417ec79af
commit 35f879abde
4 changed files with 21 additions and 12 deletions

View File

@ -2,6 +2,9 @@
* Properly parse numbers with exponents.
* Don't crash when evaluating CSS variables whose names are entirely
interpolated (for example, `#{--foo}: ...`).
## 1.0.0-beta.2
* Add support for the `::slotted()` pseudo-element.

View File

@ -19,12 +19,6 @@ class CssDeclaration extends CssNode {
final FileSpan span;
/// Whether this is a custom property declaration, also known as a CSS
/// variable.
///
/// Custom property declarations always have unquoted [SassString] values.
bool get isCustomProperty => name.value.startsWith("--");
CssDeclaration(this.name, this.value, this.span);
T accept<T>(CssVisitor<T> visitor) => visitor.visitDeclaration(this);

View File

@ -230,8 +230,8 @@ class _SerializeVisitor implements CssVisitor, ValueVisitor, SelectorVisitor {
_writeIndentation();
_buffer.write(node.name.value);
_buffer.writeCharCode($colon);
if (node.isCustomProperty) {
_writeCustomPropertyValue(node);
if (_shouldReindentValue(node)) {
_writeReindentedValue(node);
} else {
_buffer.writeCharCode($space);
_visitValue(node.value);
@ -239,10 +239,22 @@ class _SerializeVisitor implements CssVisitor, ValueVisitor, SelectorVisitor {
_buffer.writeCharCode($semicolon);
}
/// Emits the value of [node] as a custom property value.
/// Returns whether [node]'s value should be re-indented when being written to
/// the stylesheet.
///
/// This re-indents [node]'s value relative to the current indentation.
void _writeCustomPropertyValue(CssDeclaration node) {
/// We only re-indent custom property values that were parsed as custom
/// properties, which we detect as unquoted strings. It's possible to have
/// false positives here, since someone could write `#{--foo}: unquoted`, but
/// that's unlikely enough that we can spare the extra time a no-op
/// reindenting will take.
bool _shouldReindentValue(CssDeclaration node) {
if (!node.name.value.startsWith("--")) return false;
var value = node.value.value;
return value is SassString && !value.hasQuotes;
}
/// Emits the value of [node], re-indented relative to the current indentation.
void _writeReindentedValue(CssDeclaration node) {
var value = (node.value.value as SassString).text;
var minimumIndentation = _minimumIndentation(value);

View File

@ -1,5 +1,5 @@
name: sass
version: 1.0.0-beta.2
version: 1.0.0-dev
description: A Sass implementation in Dart.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/sass/dart-sass