dart-sass/lib/src/value/list.dart

69 lines
1.9 KiB
Dart
Raw Normal View History

2016-05-24 20:56:53 +02:00
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
2016-08-15 00:49:50 +02:00
import 'package:charcode/charcode.dart';
2016-08-15 07:32:00 +02:00
import '../utils.dart';
2016-08-15 08:51:29 +02:00
import '../visitor/interface/value.dart';
2016-05-24 20:56:53 +02:00
import '../value.dart';
class SassList extends Value {
final List<Value> contents;
final ListSeparator separator;
2016-08-15 00:49:50 +02:00
final bool isBracketed;
2016-06-03 22:36:20 +02:00
bool get isBlank => contents.every((element) => element.isBlank);
2016-08-27 06:29:53 +02:00
SassList(Iterable<Value> contents, this.separator, {bool bracketed: false})
2016-08-15 00:49:50 +02:00
: contents = new List.unmodifiable(contents),
isBracketed = bracketed;
2016-05-24 20:56:53 +02:00
2016-05-28 01:09:03 +02:00
/*=T*/ accept/*<T>*/(ValueVisitor/*<T>*/ visitor) =>
visitor.visitList(this);
2016-08-15 07:32:00 +02:00
bool operator ==(other) =>
other is SassList &&
other.separator == separator &&
other.isBracketed == isBracketed &&
listEquals(other.contents, contents);
int get hashCode => listHash(contents);
2016-08-15 00:49:50 +02:00
String toString() {
var buffer = new StringBuffer();
if (isBracketed) buffer.writeCharCode($lbracket);
2016-08-15 08:19:18 +02:00
buffer.write(contents
.map((element) =>
_elementNeedsParens(element) ? "($element)" : element.toString())
.join(separator == ListSeparator.comma ? ", " : " "));
2016-08-15 00:49:50 +02:00
if (isBracketed) buffer.writeCharCode($rbracket);
return buffer.toString();
}
2016-08-15 08:19:18 +02:00
bool _elementNeedsParens(Value value) {
if (value is SassList) {
2016-08-15 08:47:06 +02:00
if (value.contents.length < 2) return false;
if (value.isBracketed) return false;
2016-08-15 08:19:18 +02:00
return separator == ListSeparator.comma
? separator == ListSeparator.comma
2016-08-15 08:47:06 +02:00
: separator != ListSeparator.undecided;
2016-08-15 08:19:18 +02:00
}
return false;
}
2016-05-24 20:56:53 +02:00
}
class ListSeparator {
static const space = const ListSeparator._("space");
static const comma = const ListSeparator._("comma");
2016-06-03 22:36:20 +02:00
static const undecided = const ListSeparator._("undecided");
2016-05-24 20:56:53 +02:00
final String name;
const ListSeparator._(this.name);
String toString() => name;
}