Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(icon): handle values with unnecessary spaces being passed into fontIcon and fontSet #9056

Merged
merged 1 commit into from
Jan 4, 2018
Merged
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
43 changes: 41 additions & 2 deletions src/lib/icon/icon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ describe('MatIcon', () => {
iconRegistry.registerFontClassAlias('f1', 'font1');
iconRegistry.registerFontClassAlias('f2');

let fixture = TestBed.createComponent(IconWithCustomFontCss);

const fixture = TestBed.createComponent(IconWithCustomFontCss);
const testComponent = fixture.componentInstance;
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');

testComponent.fontSet = 'f1';
testComponent.fontIcon = 'house';
fixture.detectChanges();
Expand All @@ -377,6 +377,45 @@ describe('MatIcon', () => {
fixture.detectChanges();
expect(sortedClassNames(matIconElement)).toEqual(['f3', 'mat-icon', 'tent']);
});

it('should handle values with extraneous spaces being passed in to `fontSet`', () => {
const fixture = TestBed.createComponent(IconWithCustomFontCss);
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');

expect(() => {
fixture.componentInstance.fontSet = 'font set';
fixture.detectChanges();
}).not.toThrow();

expect(sortedClassNames(matIconElement)).toEqual(['font', 'mat-icon']);

expect(() => {
fixture.componentInstance.fontSet = ' changed';
fixture.detectChanges();
}).not.toThrow();

expect(sortedClassNames(matIconElement)).toEqual(['changed', 'mat-icon']);
});

it('should handle values with extraneous spaces being passed in to `fontIcon`', () => {
const fixture = TestBed.createComponent(IconWithCustomFontCss);
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');

expect(() => {
fixture.componentInstance.fontIcon = 'font icon';
fixture.detectChanges();
}).not.toThrow();

expect(sortedClassNames(matIconElement)).toEqual(['font', 'mat-icon', 'material-icons']);

expect(() => {
fixture.componentInstance.fontIcon = ' changed';
fixture.detectChanges();
}).not.toThrow();

expect(sortedClassNames(matIconElement)).toEqual(['changed', 'mat-icon', 'material-icons']);
});

});

/** Marks an svg icon url as explicitly trusted. */
Expand Down
23 changes: 21 additions & 2 deletions src/lib/icon/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,20 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
@Input() svgIcon: string;

/** Font set that the icon is a part of. */
@Input() fontSet: string;
@Input()
get fontSet(): string { return this._fontSet; }
set fontSet(value: string) {
this._fontSet = this._cleanupFontValue(value);
}
private _fontSet: string;

/** Name of an icon within a font set. */
@Input() fontIcon: string;
@Input()
get fontIcon(): string { return this._fontIcon; }
set fontIcon(value: string) {
this._fontIcon = this._cleanupFontValue(value);
}
private _fontIcon: string;

private _previousFontSetClass: string;
private _previousFontIconClass: string;
Expand Down Expand Up @@ -202,4 +212,13 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
this._previousFontIconClass = this.fontIcon;
}
}

/**
* Cleans up a value to be used as a fontIcon or fontSet.
* Since the value ends up being assigned as a CSS class, we
* have to trim the value and omit space-separated values.
*/
private _cleanupFontValue(value: string) {
return typeof value === 'string' ? value.trim().split(' ')[0] : value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like ligatures can include whitespace:
https://twitter.com/larrythefox/status/422813021807714304

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our current logic still doesn't hold up though. We used to take the value verbatim and set it as a class which is invalid. I switched it up to only take it up to the first space.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. I mixed up class and ligature

}
}