/
id.dart
46 lines (34 loc) · 1.19 KB
/
id.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
// 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 'dart:math' as math;
import 'package:meta/meta.dart';
import '../../visitor/interface/selector.dart';
import '../selector.dart';
/// An ID selector.
///
/// This selects elements whose `id` attribute exactly matches the given name.
///
/// {@category AST}
@sealed
class IDSelector extends SimpleSelector {
/// The ID name this selects for.
final String name;
int get specificity => math.pow(super.specificity, 2) as int;
IDSelector(this.name);
T accept<T>(SelectorVisitor<T> visitor) => visitor.visitIDSelector(this);
/// @nodoc
@internal
IDSelector addSuffix(String suffix) => IDSelector(name + suffix);
/// @nodoc
@internal
List<SimpleSelector>? unify(List<SimpleSelector> compound) {
// A given compound selector may only contain one ID.
if (compound.any((simple) => simple is IDSelector && simple != this)) {
return null;
}
return super.unify(compound);
}
bool operator ==(Object other) => other is IDSelector && other.name == name;
int get hashCode => name.hashCode;
}