Skip to content

Commit

Permalink
[fix](ng-attr): remove camel-cased dom attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
technohippy authored and mhevery committed Feb 15, 2014
1 parent b723ade commit eb29527
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 39 deletions.
13 changes: 6 additions & 7 deletions lib/core_dom/directive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ class NodeAttrs {
NodeAttrs(this.element);

operator [](String attributeName) =>
element.attributes[snakecase(attributeName, '-')];
element.attributes[attributeName];

operator []=(String attributeName, String value) {
var snakeName = snakecase(attributeName, '-');
if (value == null) {
element.attributes.remove(snakeName);
element.attributes.remove(attributeName);
} else {
element.attributes[snakeName] = value;
element.attributes[attributeName] = value;
}
if (_observers != null && _observers.containsKey(attributeName)) {
_observers[attributeName].forEach((fn) => fn(value));
Expand All @@ -56,14 +55,14 @@ class NodeAttrs {
}

void forEach(void f(String k, String v)) {
element.attributes.forEach((k, v) => f(camelcase(k), v));
element.attributes.forEach(f);
}

bool containsKey(String attributeName) =>
element.attributes.containsKey(snakecase(attributeName, '-'));
element.attributes.containsKey(attributeName);

Iterable<String> get keys =>
element.attributes.keys.map((name) => camelcase(name));
element.attributes.keys;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/directive/ng_bind_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ part of angular.directive;
*/
@NgDirective(
selector: '[ng-bind-html]',
map: const {'ngBindHtml': '=>value'})
map: const {'ng-bind-html': '=>value'})
class NgBindHtmlDirective {
final dom.Element element;
final dom.NodeValidator validator;
Expand Down
7 changes: 4 additions & 3 deletions lib/directive/ng_src_boolean.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ class NgAttributeDirective implements NgAttachAware {
NgAttributeDirective(this._attrs);

void attach() {
String ngAttrPrefix = 'ng-attr-';
_attrs.forEach((key, value) {
if (key.startsWith('ngAttr')) {
var newKey = key.substring(6);
if (key.startsWith(ngAttrPrefix)) {
var newKey = key.substring(ngAttrPrefix.length);
_attrs[newKey] = value;
_attrs.observe(snakecase(key), (newValue) => _attrs[newKey] = newValue );
_attrs.observe(key, (newValue) => _attrs[newKey] = newValue );
}
});
}
Expand Down
9 changes: 3 additions & 6 deletions lib/tools/source_metadata_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class SourceMetadataExtractor {
.firstWhere((specPrefix) => mappingSpec.startsWith(specPrefix),
orElse: () => throw '$mappingSpec no matching spec');
if (spec != '@') {
dirInfo.expressionAttrs.add(snakecase(attrName));
dirInfo.expressionAttrs.add(attrName);
}
if (mappingSpec.length == 1) { // Shorthand. Remove.
// TODO(pavelgj): Figure out if short-hand LHS should be expanded
// and added to the expressions list.
if (attrName != '.') {
dirInfo.expressions.add(_maybeCamelCase(attrName));
dirInfo.expressions.add(attrName);
}
} else {
mappingSpec = mappingSpec.substring(spec.length);
Expand All @@ -60,7 +60,6 @@ class SourceMetadataExtractor {
});

meta.exportExpressionAttrs.forEach((attr) {
attr = snakecase(attr);
if (!dirInfo.expressionAttrs.contains(attr)) {
dirInfo.expressionAttrs.add(attr);
}
Expand All @@ -74,7 +73,7 @@ class SourceMetadataExtractor {


// No explicit selector specified on the directive, compute one.
var className = snakecase(meta.className);
var className = meta.className;
if (dirInfo.selector == null) {
if (meta.type == COMPONENT) {
if (className.endsWith(_COMPONENT)) {
Expand Down Expand Up @@ -118,8 +117,6 @@ class SourceMetadataExtractor {
}
}

String _maybeCamelCase(String s) => (s.indexOf('-') > -1) ? camelcase(s) : s;

class DirectiveMetadataCollectingVisitor {
List<DirectiveMetadata> metadata = <DirectiveMetadata>[];

Expand Down
14 changes: 0 additions & 14 deletions lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,4 @@ relaxFnArgs(Function fn) {
}
}

camelcase(String s) {
var part = s.split('-').map((s) => s.toLowerCase());
if (part.length <= 1) {
return part.join();
}
return part.first + part.skip(1).map(capitalize).join();
}

capitalize(String s) => s.substring(0, 1).toUpperCase() + s.substring(1);

var SNAKE_CASE_REGEXP = new RegExp("[A-Z]");

snakecase(String name, [separator = '-']) =>
name.replaceAllMapped(SNAKE_CASE_REGEXP, (Match match) =>
(match.start != 0 ? separator : '') + match.group(0).toLowerCase());
16 changes: 8 additions & 8 deletions test/core_dom/directive_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ main() {

it('should transform names to camel case', () {
expect(nodeAttrs['foo']).toEqual('bar');
expect(nodeAttrs['fooBar']).toEqual('baz');
expect(nodeAttrs['fooBarBaz']).toEqual('foo');
expect(nodeAttrs['foo-bar']).toEqual('baz');
expect(nodeAttrs['foo-bar-baz']).toEqual('foo');
});

it('should return null for unexistent attributes', () {
Expand All @@ -27,18 +27,18 @@ main() {
it('should provide a forEach function to iterate over attributes', () {
Map<String, String> attrMap = new Map();
nodeAttrs.forEach((k, v) => attrMap[k] = v);
expect(attrMap).toEqual({'foo': 'bar', 'fooBar': 'baz', 'fooBarBaz': 'foo'});
expect(attrMap).toEqual({'foo': 'bar', 'foo-bar': 'baz', 'foo-bar-baz': 'foo'});
});

it('should provide a contains method', () {
expect(nodeAttrs.containsKey('foo')).toEqual(true);
expect(nodeAttrs.containsKey('fooBar')).toEqual(true);
expect(nodeAttrs.containsKey('fooBarBaz')).toEqual(true);
expect(nodeAttrs.containsKey('barFoo')).toEqual(false);
expect(nodeAttrs.containsKey('foo-bar')).toEqual(true);
expect(nodeAttrs.containsKey('foo-bar-baz')).toEqual(true);
expect(nodeAttrs.containsKey('bar-foo')).toEqual(false);
});

it('should return the attribute names', () {
expect(nodeAttrs.keys.toList()..sort()).toEqual(['foo', 'fooBar', 'fooBarBaz']);
expect(nodeAttrs.keys.toList()..sort()).toEqual(['foo', 'foo-bar', 'foo-bar-baz']);
});
});
}
}

0 comments on commit eb29527

Please sign in to comment.