Skip to content

Commit d646266

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 2b14952 commit d646266

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
@@ -57,6 +57,7 @@ describe('MatIcon', () => {
5757
IconWithBindingAndNgIf,
5858
InlineIcon,
5959
SvgIconWithUserContent,
60+
IconWithLigatureAndSvgBinding,
6061
],
6162
providers: [{
6263
provide: MAT_ICON_LOCATION,
@@ -161,6 +162,19 @@ describe('MatIcon', () => {
161162
expect(sortedClassNames(matIconElement))
162163
.toEqual(['mat-icon', 'mat-icon-no-color', 'myfont', 'notranslate']);
163164
});
165+
166+
it('should not clear the text of a ligature icon if the svgIcon is bound to something falsy',
167+
() => {
168+
let fixture = TestBed.createComponent(IconWithLigatureAndSvgBinding);
169+
170+
const testComponent = fixture.componentInstance;
171+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
172+
testComponent.iconName = undefined;
173+
fixture.detectChanges();
174+
175+
expect(matIconElement.textContent.trim()).toBe('house');
176+
});
177+
164178
});
165179

166180
describe('Icons from URLs', () => {
@@ -854,3 +868,8 @@ class InlineIcon {
854868
class SvgIconWithUserContent {
855869
iconName: string | undefined = '';
856870
}
871+
872+
@Component({template: '<mat-icon [svgIcon]="iconName">house</mat-icon>'})
873+
class IconWithLigatureAndSvgBinding {
874+
iconName: string | undefined;
875+
}

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)