Skip to content
This repository was archived by the owner on Sep 16, 2022. It is now read-only.

Commit 49e733d

Browse files
MichaelRFairhurstalorenzen
authored andcommitted
Roll attribute selector
Closes #1771 PiperOrigin-RevId: 242039436
1 parent c04514c commit 49e733d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:angular_analyzer_plugin/src/selector/attribute_selector_base.dart';
2+
import 'package:angular_analyzer_plugin/src/selector/element_view.dart';
3+
import 'package:angular_analyzer_plugin/src/selector/html_tag_for_selector.dart';
4+
import 'package:angular_analyzer_plugin/src/selector/selector.dart';
5+
import 'package:angular_analyzer_plugin/src/selector/match.dart';
6+
import 'package:angular_analyzer_plugin/src/selector/name.dart';
7+
8+
/// The [Selector] that matches elements that have an attribute with the
9+
/// given name, and (optionally) with the given value;
10+
class AttributeSelector extends AttributeSelectorBase {
11+
@override
12+
final SelectorName nameElement;
13+
final String value;
14+
15+
AttributeSelector(this.nameElement, this.value);
16+
17+
@override
18+
List<SelectorName> getAttributes(ElementView element) =>
19+
match(element, null) == SelectorMatch.NonTagMatch ? [] : [nameElement];
20+
21+
@override
22+
bool matchValue(String attributeValue) =>
23+
value == null || attributeValue == value;
24+
25+
@override
26+
List<HtmlTagForSelector> refineTagSuggestions(
27+
List<HtmlTagForSelector> context) {
28+
for (final tag in context) {
29+
tag.setAttribute(nameElement.string, value: value);
30+
}
31+
return context;
32+
}
33+
34+
@override
35+
String toString() {
36+
final name = nameElement.string;
37+
if (value != null) {
38+
return '[$name=$value]';
39+
}
40+
return '[$name]';
41+
}
42+
}

0 commit comments

Comments
 (0)