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';
|
|
|
|
|
2016-08-27 07:43:10 +02:00
|
|
|
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.
|
2016-08-27 07:43:10 +02:00
|
|
|
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++) {
|
2016-07-09 00:50:57 +02:00
|
|
|
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
|
|
|
}
|