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

Commit

Permalink
fix(ng-class): array syntax should not insert nulls
Browse files Browse the repository at this point in the history
Closes #513
  • Loading branch information
chirayuk committed Feb 6, 2014
1 parent b5aa130 commit b982e32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/directive/ng_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ abstract class _NgClassBase {

static List<String> _flatten(classes) {
if (classes == null) return [];
if (classes is List) return classes;
if (classes is List) {
return classes.where((String e) => e != null && e.isNotEmpty)
.toList(growable: false);
}
if (classes is Map) {
return classes.keys.where((key) => toBool(classes[key])).toList();
}
Expand Down
18 changes: 15 additions & 3 deletions test/directive/ng_class_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ main() {


it('should support adding multiple classes via an array', () {
var element = _.compile('<div class="existing" ng-class="[\'A\', \'B\']"></div>');
_.rootScope.a = 'a';
_.rootScope.b = '';
_.rootScope.c = null;
var element = _.compile('<div class="existing" ng-class="[\'literalA\', a, b, c]"></div>');
_.rootScope.$digest();
expect(element.classes.contains('existing')).toBeTruthy();
expect(element.classes.contains('A')).toBeTruthy();
expect(element.classes.contains('B')).toBeTruthy();
expect(element.classes.contains('a')).toBeTruthy();
expect(element.classes.contains('b')).toBeFalsy();
expect(element.classes.contains('c')).toBeFalsy();
expect(element.classes.contains('null')).toBeFalsy();
_.rootScope.a = null;
_.rootScope.b = 'b';
_.rootScope.c = 'c';
_.rootScope.$digest();
expect(element.classes.contains('a')).toBeFalsy();
expect(element.classes.contains('b')).toBeTruthy();
expect(element.classes.contains('c')).toBeTruthy();
});


Expand Down

0 comments on commit b982e32

Please sign in to comment.