Skip to content

Commit 1a735bc

Browse files
crisbetojelbourn
authored andcommitted
fix(icon): handle values with unnecessary spaces being passed into fontIcon and fontSet (#9056)
Handles values with trailing/leading spaces, as well as space-separated values being passed into the `fontIcon` and `fontSet` properties, rather than letting the browser throw a DOM exception. Fixes #9054.
1 parent f8cd8eb commit 1a735bc

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/lib/icon/icon.spec.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,10 @@ describe('MatIcon', () => {
358358
iconRegistry.registerFontClassAlias('f1', 'font1');
359359
iconRegistry.registerFontClassAlias('f2');
360360

361-
let fixture = TestBed.createComponent(IconWithCustomFontCss);
362-
361+
const fixture = TestBed.createComponent(IconWithCustomFontCss);
363362
const testComponent = fixture.componentInstance;
364363
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
364+
365365
testComponent.fontSet = 'f1';
366366
testComponent.fontIcon = 'house';
367367
fixture.detectChanges();
@@ -377,6 +377,45 @@ describe('MatIcon', () => {
377377
fixture.detectChanges();
378378
expect(sortedClassNames(matIconElement)).toEqual(['f3', 'mat-icon', 'tent']);
379379
});
380+
381+
it('should handle values with extraneous spaces being passed in to `fontSet`', () => {
382+
const fixture = TestBed.createComponent(IconWithCustomFontCss);
383+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
384+
385+
expect(() => {
386+
fixture.componentInstance.fontSet = 'font set';
387+
fixture.detectChanges();
388+
}).not.toThrow();
389+
390+
expect(sortedClassNames(matIconElement)).toEqual(['font', 'mat-icon']);
391+
392+
expect(() => {
393+
fixture.componentInstance.fontSet = ' changed';
394+
fixture.detectChanges();
395+
}).not.toThrow();
396+
397+
expect(sortedClassNames(matIconElement)).toEqual(['changed', 'mat-icon']);
398+
});
399+
400+
it('should handle values with extraneous spaces being passed in to `fontIcon`', () => {
401+
const fixture = TestBed.createComponent(IconWithCustomFontCss);
402+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
403+
404+
expect(() => {
405+
fixture.componentInstance.fontIcon = 'font icon';
406+
fixture.detectChanges();
407+
}).not.toThrow();
408+
409+
expect(sortedClassNames(matIconElement)).toEqual(['font', 'mat-icon', 'material-icons']);
410+
411+
expect(() => {
412+
fixture.componentInstance.fontIcon = ' changed';
413+
fixture.detectChanges();
414+
}).not.toThrow();
415+
416+
expect(sortedClassNames(matIconElement)).toEqual(['changed', 'mat-icon', 'material-icons']);
417+
});
418+
380419
});
381420

382421
/** Marks an svg icon url as explicitly trusted. */

src/lib/icon/icon.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,20 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
7878
@Input() svgIcon: string;
7979

8080
/** Font set that the icon is a part of. */
81-
@Input() fontSet: string;
81+
@Input()
82+
get fontSet(): string { return this._fontSet; }
83+
set fontSet(value: string) {
84+
this._fontSet = this._cleanupFontValue(value);
85+
}
86+
private _fontSet: string;
8287

8388
/** Name of an icon within a font set. */
84-
@Input() fontIcon: string;
89+
@Input()
90+
get fontIcon(): string { return this._fontIcon; }
91+
set fontIcon(value: string) {
92+
this._fontIcon = this._cleanupFontValue(value);
93+
}
94+
private _fontIcon: string;
8595

8696
private _previousFontSetClass: string;
8797
private _previousFontIconClass: string;
@@ -202,4 +212,13 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
202212
this._previousFontIconClass = this.fontIcon;
203213
}
204214
}
215+
216+
/**
217+
* Cleans up a value to be used as a fontIcon or fontSet.
218+
* Since the value ends up being assigned as a CSS class, we
219+
* have to trim the value and omit space-separated values.
220+
*/
221+
private _cleanupFontValue(value: string) {
222+
return typeof value === 'string' ? value.trim().split(' ')[0] : value;
223+
}
205224
}

0 commit comments

Comments
 (0)