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

Commit 7caf0a1

Browse files
committed
feat(compiler): Mapped attributes are snake-cased correctly.
1 parent b68964f commit 7caf0a1

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

lib/angular.dart

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ part 'node_cursor.dart';
3737
part 'parser.dart';
3838
part 'scope.dart';
3939
part 'selector.dart';
40+
part 'string_utilities.dart';
4041

4142
ASSERT(condition) {
4243
if (!condition) {

lib/block.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class ComponentFactory {
318318

319319
createAttributeMapping(Scope parentScope, Scope shadowScope, Parser parser) {
320320
directive.$map.forEach((attrName, mapping) {
321-
var attrValue = element.attributes[attrName];
321+
var attrValue = element.attributes[snakeCase(attrName, '-')];
322322
if (attrValue == null) attrValue = '';
323323
if (mapping == '@') {
324324
shadowScope[attrName] = attrValue;

lib/string_utilities.dart

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
part of angular;
2+
3+
var SNAKE_CASE_REGEXP = new RegExp("[A-Z]");
4+
5+
snakeCase(String name, [separator = '_']) =>
6+
name.replaceAllMapped(SNAKE_CASE_REGEXP, (Match match) =>
7+
(match.start != 0 ? separator : '') + match.group(0).toLowerCase());

test/compiler_spec.dart

+17-1
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ main() {
391391
describe('components', () {
392392
beforeEach(module((AngularModule module) {
393393
module.directive(SimpleComponent);
394+
module.directive(CamelCaseMapComponent);
394395
module.directive(IoComponent);
395396
module.directive(ParentExpressionComponent);
396397
module.directive(PublishMeComponent);
@@ -412,7 +413,7 @@ main() {
412413
it('should create a component that can access parent scope', inject(() {
413414
$rootScope.fromParent = "should not be used";
414415
$rootScope.val = "poof";
415-
var element = $('<parent-expression fromParent=val></parent-expression>');
416+
var element = $('<parent-expression from-parent=val></parent-expression>');
416417

417418
$compile(element)(injector, element);
418419

@@ -455,6 +456,14 @@ main() {
455456
expect($rootScope.done).toEqual(true);
456457
}));
457458

459+
it('should expose mapped attributes as camel case', inject(() {
460+
var element = $('<camel-case-map camel-case=6></camel-case-map>');
461+
$compile(element)(injector, element);
462+
$rootScope.$apply();
463+
var componentScope = $rootScope.camelCase;
464+
expect(componentScope.camelCase).toEqual('6');
465+
}));
466+
458467
it('should throw an exception if required directive is missing', inject((Compiler $compile, Scope $rootScope, Injector injector) {
459468
expect(() {
460469
var element = $('<tab local><pane></pane><pane local></pane></tab>');
@@ -510,6 +519,13 @@ class IoComponent {
510519
}
511520
}
512521

522+
class CamelCaseMapComponent {
523+
static Map $map = {"camelCase": "@"};
524+
CamelCaseMapComponent(Scope scope) {
525+
scope.$root.camelCase = scope;
526+
}
527+
}
528+
513529
class ParentExpressionComponent {
514530
static String $template = '<div>inside {{fromParent()}}</div>';
515531
static Map $map = {"fromParent": "&"};

test/string_utilities_spec.dart

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import "_specs.dart";
2+
3+
main() {
4+
describe('snakeCase', (){
5+
it('should convert to snake_case', () {
6+
expect(snakeCase('ABC')).toEqual('a_b_c');
7+
expect(snakeCase('alanBobCharles')).toEqual('alan_bob_charles');
8+
});
9+
10+
11+
it('should allow seperator to be overridden', () {
12+
expect(snakeCase('ABC', '&')).toEqual('a&b&c');
13+
expect(snakeCase('alanBobCharles', '&')).toEqual('alan&bob&charles');
14+
});
15+
});
16+
}

0 commit comments

Comments
 (0)