Skip to content

Commit

Permalink
fix(compiler): Support camelCase property bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdeboer committed Sep 16, 2014
1 parent 4dee442 commit 93d6698
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ class ElementBinder {
var jsNode;
List bindAssignableProps = [];
bindAttrs.forEach((String prop, AST ast) {
prop = camelCase(prop);
if (jsNode == null) jsNode = new js.JsObject.fromBrowserObject(node);
scope.watchAST(ast, (v, _) {
jsNode[prop] = v;
Expand Down
1 change: 1 addition & 0 deletions lib/core_dom/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DirectiveSelector {
});
}


/**
* [matchElement] returns an [ElementBinder] or a [TemplateElementBinder] configured with all the
* directives triggered by the `node`.
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ relaxFnArgs(Function fn) {

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

String 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();
}

/// Returns whether or not the given identifier is a reserved word in Dart.
bool isReservedWord(String identifier) => RESERVED_WORDS.contains(identifier);
Expand Down
11 changes: 9 additions & 2 deletions test/core_dom/web_components_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,16 @@ main() {

it('should bind to a non-existent property', () {
registerElement('tests-empty', {});
compileAndUpgrade('<tests-empty bind-new-prop=27></tests-empty>');
compileAndUpgrade('<tests-empty bind-newprop=27></tests-empty>');
_.rootScope.apply();
expect(customProp('new-prop')).toEqual(27);
expect(customProp('newprop')).toEqual(27);
});

it('should bind to a camelCase property', () {
registerElement('tests-camel', {});
compileAndUpgrade('<tests-camel bind-new-prop=27></tests-camel>');
_.rootScope.apply();
expect(customProp('newProp')).toEqual(27);
});

it('should bind to both directives and properties', () {
Expand Down
14 changes: 14 additions & 0 deletions test/utils_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,19 @@ main() {
}).toThrowWith(message: 'Unknown function type, expecting 0 to 5 args.');
});
});

describe('camelCase', () {
it('should ignore non camelCase', () {
expect(camelCase('regular')).toEqual('regular');
});

it('should convert snake-case', () {
expect(camelCase('snake-case')).toEqual('snakeCase');
});

it('should lowercase strings', () {
expect(camelCase('Caps-first')).toEqual('capsFirst');
});
});
}

0 comments on commit 93d6698

Please sign in to comment.