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

fix(html_extractor): correct handling of camelCased attributes #1301

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions lib/tools/common.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
library angular.tools.common;

import 'dart:collection';

class DirectiveInfo {
String selector;
String template;
List<String> expressionAttrs = <String>[];
List<String> expressions = <String>[];
DirectiveInfo([this.selector, this.expressionAttrs, this.expressions]) {
if (expressionAttrs == null) {
expressionAttrs = <String>[];
}
if (expressions == null) {
expressions = <String>[];
List<String> expressions;
_AttributeList _expressionAttrs;

DirectiveInfo([this.selector, List<String> expAttrs, this.expressions]) {
expressionAttrs = expAttrs;

if (expressions == null) expressions = <String>[];
}

List<String> get expressionAttrs => _expressionAttrs;

void set expressionAttrs(value) {
if (value is _AttributeList) {
_expressionAttrs = value;
} else if (value == null) {
_expressionAttrs = new _AttributeList();
} else {
assert(value is Iterable);
_expressionAttrs = new _AttributeList()..addAll(value);
}
}
}
Expand Down Expand Up @@ -42,3 +55,29 @@ class DirectiveMetadata {
}
}

/**
* Extends list to always returned lowercase attribute name:
*
* var l = new _AttributeList();
* l.add('fooBar');
* print(l[0]); // "foobar"
*
* It helps working with html5lib `Node` class which also lowercase attribute names.
*
* Note: attribute names are case-insensitive in HTML.
*/
class _AttributeList extends ListBase<String> {
final List<String> _attributes = [];

void set length(int len) {
_attributes.length = len;
}

int get length => _attributes.length;

String operator [](int index) => _attributes[index].toLowerCase();

void operator []=(int index, String value) {
_attributes[index] = value;
}
}
6 changes: 2 additions & 4 deletions lib/tools/html_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ class HtmlExpressionExtractor {
});
}
if (matchesNode(node, r'[ng-repeat]')) {
var expr = _NG_REPEAT_SYNTAX.
firstMatch(node.attributes['ng-repeat']).group(2);
var expr = _NG_REPEAT_SYNTAX.firstMatch(node.attributes['ng-repeat']).group(2);
expressions.add(expr);
}

for (DirectiveInfo directiveInfo in directiveInfos) {
if (directiveInfo.selector != null &&
matchesNode(node, directiveInfo.selector)) {
if (directiveInfo.selector != null && matchesNode(node, directiveInfo.selector)) {
directiveInfo.expressionAttrs.forEach((attr) {
if (node.attributes[attr] != null && attr != 'ng-repeat') {
expressions.add(node.attributes[attr]);
Expand Down
19 changes: 12 additions & 7 deletions test/tools/html_extractor_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,23 @@ void main() {
});

it('should extract expressions from expression attributes', () {
var ioService = new MockIoService({
'foo.html': r'''
<foo bar="ctrl.baz"></foo>
'''
});
var ioService = new MockIoService({'foo.html': r'<foo bar="ctrl.baz"></foo>'});

var extractor = new HtmlExpressionExtractor([
new DirectiveInfo('foo', ['bar'])
]);
extractor.crawl('/', ioService);
expect(extractor.expressions.toList()..sort(),
equals(['ctrl.baz']));
expect(extractor.expressions.toList()).toEqual(['ctrl.baz']);
});

it('should extract expressions from expression attributes for camelCased attributes', () {
var ioService = new MockIoService({'foo.html': r'<foo fooBar="ctrl.baz"></foo>'});

var extractor = new HtmlExpressionExtractor([
new DirectiveInfo('foo', ['fooBar'])
]);
extractor.crawl('/', ioService);
expect(extractor.expressions.toList()).toEqual(['ctrl.baz']);
});

it('should ignore ng-repeat while extracting attribute expressions', () {
Expand Down