X Tutup
Skip to content

Commit 73351ac

Browse files
fix(NgClass): ignore empty and blank class names
Fixes #4033 Closes #4173
1 parent 5bab607 commit 73351ac

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

modules/angular2/src/core/directives/ng_class.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ export class NgClass implements DoCheck, OnDestroy {
126126
}
127127

128128
private _toggleClass(className: string, enabled): void {
129-
this._renderer.setElementClass(this._ngEl, className, enabled);
129+
className = className.trim();
130+
if (className.length > 0) {
131+
this._renderer.setElementClass(this._ngEl, className, enabled);
132+
}
130133
}
131134
}

modules/angular2/test/core/directives/ng_class_spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,36 @@ export function main() {
218218
rootTC.debugElement.componentInstance.arrExpr = ['bar'];
219219
detectChangesAndCheck(rootTC, 'ng-binding foo bar');
220220

221+
async.done();
222+
});
223+
}));
224+
225+
it('should ignore empty or blank class names',
226+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
227+
var template = '<div class="foo" [ng-class]="arrExpr"></div>';
228+
229+
tcb.overrideTemplate(TestComponent, template)
230+
.createAsync(TestComponent)
231+
.then((rootTC) => {
232+
233+
rootTC.debugElement.componentInstance.arrExpr = ['', ' '];
234+
detectChangesAndCheck(rootTC, 'foo ng-binding');
235+
236+
async.done();
237+
});
238+
}));
239+
240+
it('should trim blanks from class names',
241+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
242+
var template = '<div class="foo" [ng-class]="arrExpr"></div>';
243+
244+
tcb.overrideTemplate(TestComponent, template)
245+
.createAsync(TestComponent)
246+
.then((rootTC) => {
247+
248+
rootTC.debugElement.componentInstance.arrExpr = [' bar '];
249+
detectChangesAndCheck(rootTC, 'foo ng-binding bar');
250+
221251
async.done();
222252
});
223253
}));
@@ -289,6 +319,20 @@ export function main() {
289319
});
290320
}));
291321

322+
it('should ignore empty and blank strings',
323+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
324+
var template = `<div class="foo" [ng-class]="strExpr"></div>`;
325+
326+
tcb.overrideTemplate(TestComponent, template)
327+
.createAsync(TestComponent)
328+
.then((rootTC) => {
329+
rootTC.debugElement.componentInstance.strExpr = '';
330+
detectChangesAndCheck(rootTC, 'foo ng-binding');
331+
332+
async.done();
333+
});
334+
}));
335+
292336
});
293337

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

0 commit comments

Comments
 (0)
X Tutup