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

Commit

Permalink
Roll attribute selector
Browse files Browse the repository at this point in the history
Closes #1771

PiperOrigin-RevId: 242039436
  • Loading branch information
MichaelRFairhurst authored and alorenzen committed Apr 5, 2019
1 parent c04514c commit 49e733d
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions angular_analyzer_plugin/lib/src/selector/attribute_selector.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:angular_analyzer_plugin/src/selector/attribute_selector_base.dart';
import 'package:angular_analyzer_plugin/src/selector/element_view.dart';
import 'package:angular_analyzer_plugin/src/selector/html_tag_for_selector.dart';
import 'package:angular_analyzer_plugin/src/selector/selector.dart';
import 'package:angular_analyzer_plugin/src/selector/match.dart';
import 'package:angular_analyzer_plugin/src/selector/name.dart';

/// The [Selector] that matches elements that have an attribute with the
/// given name, and (optionally) with the given value;
class AttributeSelector extends AttributeSelectorBase {
@override
final SelectorName nameElement;
final String value;

AttributeSelector(this.nameElement, this.value);

@override
List<SelectorName> getAttributes(ElementView element) =>
match(element, null) == SelectorMatch.NonTagMatch ? [] : [nameElement];

@override
bool matchValue(String attributeValue) =>
value == null || attributeValue == value;

@override
List<HtmlTagForSelector> refineTagSuggestions(
List<HtmlTagForSelector> context) {
for (final tag in context) {
tag.setAttribute(nameElement.string, value: value);
}
return context;
}

@override
String toString() {
final name = nameElement.string;
if (value != null) {
return '[$name=$value]';
}
return '[$name]';
}
}

0 comments on commit 49e733d

Please sign in to comment.