@@ -74,7 +74,6 @@ export const _MatIconMixinBase = mixinColor(MatIconBase);
74
74
changeDetection : ChangeDetectionStrategy . OnPush ,
75
75
} )
76
76
export class MatIcon extends _MatIconMixinBase implements OnChanges , OnInit , CanColor {
77
-
78
77
/**
79
78
* Whether the icon should be inlined, automatically sizing the icon to match the font size of
80
79
* the element the icon is contained in.
@@ -199,10 +198,16 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
199
198
const layoutElement : HTMLElement = this . _elementRef . nativeElement ;
200
199
const childCount = layoutElement . childNodes . length ;
201
200
202
- // Remove existing child nodes and add the new SVG element. Note that we can't
203
- // use innerHTML, because IE will throw if the element has a data binding.
201
+ // Remove existing non-element child nodes and SVGs, and add the new SVG element. Note that
202
+ // we can't use innerHTML, because IE will throw if the element has a data binding.
204
203
for ( let i = 0 ; i < childCount ; i ++ ) {
205
- layoutElement . removeChild ( layoutElement . childNodes [ i ] ) ;
204
+ const child = layoutElement . childNodes [ i ] ;
205
+
206
+ // 1 corresponds to Node.ELEMENT_NODE. We remove all non-element nodes in order to get rid
207
+ // of any loose text nodes, as well as any SVG elements in order to remove any old icons.
208
+ if ( child . nodeType !== 1 || child . nodeName . toLowerCase ( ) === 'svg' ) {
209
+ layoutElement . removeChild ( child ) ;
210
+ }
206
211
}
207
212
}
208
213
0 commit comments