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

feat(compiler): Mapped attributes are snake-cased correctly. #42

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
1 change: 1 addition & 0 deletions lib/angular.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ part 'node_cursor.dart';
part 'parser.dart';
part 'scope.dart';
part 'selector.dart';
part 'string_utilities.dart';

ASSERT(condition) {
if (!condition) {
Expand Down
2 changes: 1 addition & 1 deletion lib/block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class ComponentFactory {

createAttributeMapping(Scope parentScope, Scope shadowScope, Parser parser) {
directive.$map.forEach((attrName, mapping) {
var attrValue = element.attributes[attrName];
var attrValue = element.attributes[snake_case(attrName, '-')];
if (attrValue == null) attrValue = '';
if (mapping == '@') {
shadowScope[attrName] = attrValue;
Expand Down
7 changes: 7 additions & 0 deletions lib/string_utilities.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
part of angular;

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

snake_case(String name, [separator = '_']) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind... snake_case is fine. :)

name.replaceAllMapped(SNAKE_CASE_REGEXP, (Match match) =>
(match.start != 0 ? separator : '') + match.group(0).toLowerCase());
18 changes: 17 additions & 1 deletion test/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ main() {
describe('components', () {
beforeEach(module((AngularModule module) {
module.directive(SimpleComponent);
module.directive(CamelCaseMapComponent);
module.directive(IoComponent);
module.directive(ParentExpressionComponent);
module.directive(PublishMeComponent);
Expand All @@ -412,7 +413,7 @@ main() {
it('should create a component that can access parent scope', inject(() {
$rootScope.fromParent = "should not be used";
$rootScope.val = "poof";
var element = $('<parent-expression fromParent=val></parent-expression>');
var element = $('<parent-expression from-parent=val></parent-expression>');

$compile(element)(injector, element);

Expand Down Expand Up @@ -455,6 +456,14 @@ main() {
expect($rootScope.done).toEqual(true);
}));

it('should expose mapped attributes as camel case', inject(() {
var element = $('<camel-case-map camel-case=6></camel-case-map>');
$compile(element)(injector, element);
$rootScope.$apply();
var componentScope = $rootScope.camelCase;
expect(componentScope.camelCase).toEqual('6');
}));

it('should throw an exception if required directive is missing', inject((Compiler $compile, Scope $rootScope, Injector injector) {
expect(() {
var element = $('<tab local><pane></pane><pane local></pane></tab>');
Expand Down Expand Up @@ -510,6 +519,13 @@ class IoComponent {
}
}

class CamelCaseMapComponent {
static Map $map = {"camelCase": "@"};
CamelCaseMapComponent(Scope scope) {
scope.$root.camelCase = scope;
}
}

class ParentExpressionComponent {
static String $template = '<div>inside {{fromParent()}}</div>';
static Map $map = {"fromParent": "&"};
Expand Down
10 changes: 10 additions & 0 deletions test/string_utilities_spec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "_specs.dart";

main() {
describe('snake_case', (){
it('should convert to snake_case', () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe another test for different separator?

expect(snake_case('ABC')).toEqual('a_b_c');
expect(snake_case('alanBobCharles')).toEqual('alan_bob_charles');
});
});
}