Skip to content

Commit f2f1232

Browse files
crisbetoandrewseguin
authored andcommitted
fix(icon): clearing user content when svgIcon is bound to falsy value (#15290)
Fixes the `mat-icon` component clearing the user's content and potentially a valid ligature icon, if there's also an `svgIcon` attribute.
1 parent e979493 commit f2f1232

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/lib/icon/icon.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('MatIcon', () => {
5555
IconWithBindingAndNgIf,
5656
InlineIcon,
5757
SvgIconWithUserContent,
58+
IconWithLigatureAndSvgBinding,
5859
],
5960
providers: [{
6061
provide: MAT_ICON_LOCATION,
@@ -159,6 +160,19 @@ describe('MatIcon', () => {
159160
expect(sortedClassNames(matIconElement))
160161
.toEqual(['mat-icon', 'mat-icon-no-color', 'myfont', 'notranslate']);
161162
});
163+
164+
it('should not clear the text of a ligature icon if the svgIcon is bound to something falsy',
165+
() => {
166+
let fixture = TestBed.createComponent(IconWithLigatureAndSvgBinding);
167+
168+
const testComponent = fixture.componentInstance;
169+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
170+
testComponent.iconName = undefined;
171+
fixture.detectChanges();
172+
173+
expect(matIconElement.textContent.trim()).toBe('house');
174+
});
175+
162176
});
163177

164178
describe('Icons from URLs', () => {
@@ -851,3 +865,8 @@ class InlineIcon {
851865
class SvgIconWithUserContent {
852866
iconName: string | undefined = '';
853867
}
868+
869+
@Component({template: '<mat-icon [svgIcon]="iconName">house</mat-icon>'})
870+
class IconWithLigatureAndSvgBinding {
871+
iconName: string | undefined;
872+
}

src/lib/icon/icon.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,17 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
223223

224224
ngOnChanges(changes: SimpleChanges) {
225225
// Only update the inline SVG icon if the inputs changed, to avoid unnecessary DOM operations.
226-
if (changes['svgIcon']) {
226+
const svgIconChanges = changes['svgIcon'];
227+
228+
if (svgIconChanges) {
227229
if (this.svgIcon) {
228230
const [namespace, iconName] = this._splitIconName(this.svgIcon);
229231

230232
this._iconRegistry.getNamedSvgIcon(iconName, namespace).pipe(take(1)).subscribe(
231233
svg => this._setSvgElement(svg),
232234
(err: Error) => console.log(`Error retrieving icon: ${err.message}`)
233235
);
234-
} else {
236+
} else if (svgIconChanges.previousValue) {
235237
this._clearSvgElement();
236238
}
237239
}

0 commit comments

Comments
 (0)