dart-sass/lib/src/ast/sass/interpolation.dart
2016-09-30 18:42:41 -07:00

52 lines
1.7 KiB
Dart

// 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.
import 'package:source_span/source_span.dart';
import 'expression.dart';
import 'node.dart';
/// Plain text interpolated with Sass expressions.
class Interpolation implements SassNode {
/// The contents of this interpolation.
///
/// This contains [String]s and [Expression]s. It never contains two adjacent
/// [String]s.
final List contents;
final FileSpan span;
/// If this contains no interpolated expressions, returns its text contents.
///
/// Otherwise, returns `null`.
String get asPlain {
if (contents.isEmpty) return '';
if (contents.length == 1 && contents.first is String) return contents.first;
return null;
}
/// Returns the plain text before the interpolation, or the empty string.
String get initialPlain => contents.first is String ? contents.first : '';
Interpolation(Iterable /*(String|Expression)*/ contents, this.span)
: contents = new List.unmodifiable(contents) {
for (var i = 0; i < this.contents.length; i++) {
if (this.contents[i] is! String && this.contents[i] is! Expression) {
throw new ArgumentError.value(this.contents, "contents",
"May only contains Strings or Expressions.");
}
if (i != 0 &&
this.contents[i - 1] is String &&
this.contents[i] is String) {
throw new ArgumentError.value(
this.contents, "contents", "May not contain adjacent Strings.");
}
}
}
String toString() =>
contents.map((value) => value is String ? value : "#{$value}").join();
}