/
extender.dart
60 lines (48 loc) · 1.98 KB
/
extender.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2021 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 '../ast/css.dart';
import '../ast/selector.dart';
import '../exception.dart';
import '../utils.dart';
/// A selector that's extending another selector, such as `A` in `A {@extend
/// B}`.
class Extender {
/// The selector in which the `@extend` appeared.
final ComplexSelector selector;
/// The minimum specificity required for any selector generated from this
/// extender.
final int specificity;
/// Whether this extender represents a selector that was originally in the
/// document, rather than one defined with `@extend`.
final bool isOriginal;
/// The media query context to which this extension is restricted, or `null`
/// if it can apply within any context.
final List<CssMediaQuery>? mediaContext;
/// The span in which this selector was defined.
final FileSpan span;
/// Creates a new extender.
///
/// If [specificity] isn't passed, it defaults to `extender.specificity`.
Extender(this.selector, this.span,
{this.mediaContext, int? specificity, bool original = false})
: specificity = specificity ?? selector.specificity,
isOriginal = original;
/// Asserts that the [mediaContext] for a selector is compatible with the
/// query context for this extender.
void assertCompatibleMediaContext(List<CssMediaQuery>? mediaContext) {
if (this.mediaContext == null) return;
if (mediaContext != null && listEquals(this.mediaContext, mediaContext)) {
return;
}
throw SassException(
"You may not @extend selectors across media queries.", span);
}
Extender withSelector(ComplexSelector newSelector) =>
Extender(newSelector, span,
mediaContext: mediaContext,
specificity: specificity,
original: isOriginal);
String toString() => selector.toString();
}