Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(expression_extractor): implemented support for wildcard attr sele…
Browse files Browse the repository at this point in the history
…ctor

Fixes #447
  • Loading branch information
pavelgj committed Jan 27, 2014
1 parent 522ba49 commit 1e40344
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/tools/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ContainsSelector {
}
}

RegExp _SELECTOR_REGEXP = new RegExp(r'^(?:([\w\-]+)|(?:\.([\w\-]+))|(?:\[([\w\-]+)(?:=([^\]]*))?\]))');
RegExp _SELECTOR_REGEXP = new RegExp(r'^(?:([\w\-]+)|(?:\.([\w\-]+))|(?:\[([\w\-\*]+)(?:=([^\]]*))?\]))');
RegExp _COMMENT_COMPONENT_REGEXP = new RegExp(r'^\[([\w\-]+)(?:\=(.*))?\]$');
RegExp _CONTAINS_REGEXP = new RegExp(r'^:contains\(\/(.+)\/\)$'); //
RegExp _ATTR_CONTAINS_REGEXP = new RegExp(r'^\[\*=\/(.+)\/\]$'); //
Expand Down Expand Up @@ -101,9 +101,10 @@ bool matchesNode(Node node, String selector) {
stillGood = false;
}
} else if (part.attrName != null) {
if (part.attrValue == '' ?
node.attributes[part.attrName] == null :
node.attributes[part.attrName] != part.attrValue) {
String matchingKey = _matchingKey(node.attributes.keys, part.attrName);
if (matchingKey == null || part.attrValue == '' ?
node.attributes[matchingKey] == null :
node.attributes[matchingKey] != part.attrValue) {
stillGood = false;
}
}
Expand All @@ -122,3 +123,8 @@ bool matchesNode(Node node, String selector) {
}
return false;
}

String _matchingKey(Iterable keys, String attrName) =>
keys.firstWhere(
(key) => new RegExp('^${attrName.replaceAll('*', r'[\w\-]+')}\$').hasMatch(key.toString()),
orElse: () => null);
10 changes: 10 additions & 0 deletions test/tools/selector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ main() => describe('selector', () {
expect(matchesNode(node, '[*=/xyzz/]'), isFalse);
});

it('should match whildcard attributes', () {
var node = new Element.html('<div attr-foo="blah"></div>');
expect(matchesNode(node, '[attr-*]'), isTrue);
expect(matchesNode(node, '[attr-*=blah]'), isTrue);
expect(matchesNode(node, '[attr-*=halb]'), isFalse);
expect(matchesNode(node, '[foo-*]'), isFalse);
expect(matchesNode(node, '[foo-*=blah]'), isFalse);
expect(matchesNode(node, '[foo-*=halb]'), isFalse);
});

it('should match text', () {
var node = new Text('before-abc-after');
expect(matchesNode(node, ':contains(/abc/)'), isTrue);
Expand Down

0 comments on commit 1e40344

Please sign in to comment.