dart-sass/lib/src/ast/sass/interpolation.dart

52 lines
1.7 KiB
Dart
Raw Normal View History

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