Skip to content
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
5 changes: 4 additions & 1 deletion modules/angular2/src/core/directives/ng_class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export class NgClass implements DoCheck, OnDestroy {
}

private _toggleClass(className: string, enabled): void {
this._renderer.setElementClass(this._ngEl, className, enabled);
className = className.trim();
if (className.length > 0) {
this._renderer.setElementClass(this._ngEl, className, enabled);
}
}
}
44 changes: 44 additions & 0 deletions modules/angular2/test/core/directives/ng_class_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,36 @@ export function main() {
rootTC.debugElement.componentInstance.arrExpr = ['bar'];
detectChangesAndCheck(rootTC, 'ng-binding foo bar');

async.done();
});
}));

it('should ignore empty or blank class names',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '<div class="foo" [ng-class]="arrExpr"></div>';

tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((rootTC) => {

rootTC.debugElement.componentInstance.arrExpr = ['', ' '];
detectChangesAndCheck(rootTC, 'foo ng-binding');

async.done();
});
}));

it('should trim blanks from class names',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '<div class="foo" [ng-class]="arrExpr"></div>';

tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((rootTC) => {

rootTC.debugElement.componentInstance.arrExpr = [' bar '];
detectChangesAndCheck(rootTC, 'foo ng-binding bar');

async.done();
});
}));
Expand Down Expand Up @@ -289,6 +319,20 @@ export function main() {
});
}));

it('should ignore empty and blank strings',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = `<div class="foo" [ng-class]="strExpr"></div>`;

tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((rootTC) => {
rootTC.debugElement.componentInstance.strExpr = '';
detectChangesAndCheck(rootTC, 'foo ng-binding');

async.done();
});
}));

});

describe('cooperation with other class-changing constructs', () => {
Expand Down