@@ -17,10 +17,13 @@ import {
17
17
OnInit ,
18
18
SimpleChanges ,
19
19
ViewEncapsulation ,
20
+ Inject ,
21
+ Optional ,
20
22
} from '@angular/core' ;
21
23
import { CanColor , mixinColor } from '@angular/material/core' ;
22
24
import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
23
25
import { MatIconRegistry } from './icon-registry' ;
26
+ import { DOCUMENT } from '@angular/platform-browser' ;
24
27
25
28
26
29
// Boilerplate for applying mixins to MatIcon.
@@ -74,6 +77,7 @@ export const _MatIconMixinBase = mixinColor(MatIconBase);
74
77
changeDetection : ChangeDetectionStrategy . OnPush ,
75
78
} )
76
79
export class MatIcon extends _MatIconMixinBase implements OnChanges , OnInit , CanColor {
80
+ private _document : Document ;
77
81
78
82
/**
79
83
* Whether the icon should be inlined, automatically sizing the icon to match the font size of
@@ -113,9 +117,13 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
113
117
constructor (
114
118
elementRef : ElementRef ,
115
119
private _iconRegistry : MatIconRegistry ,
116
- @Attribute ( 'aria-hidden' ) ariaHidden : string ) {
120
+ @Attribute ( 'aria-hidden' ) ariaHidden : string ,
121
+ /** @deletion -target 8.0.0 `document` parameter to be made required. */
122
+ @Optional ( ) @Inject ( DOCUMENT ) document ?: any ) {
117
123
super ( elementRef ) ;
118
124
125
+ this . _document = document ;
126
+
119
127
// If the user has not explicitly set aria-hidden, mark the icon as hidden, as this is
120
128
// the right thing to do for the majority of icon use-cases.
121
129
if ( ! ariaHidden ) {
@@ -198,11 +206,16 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
198
206
private _clearSvgElement ( ) {
199
207
const layoutElement : HTMLElement = this . _elementRef . nativeElement ;
200
208
const childCount = layoutElement . childNodes . length ;
209
+ const elementNodeType = this . _document ? this . _document . ELEMENT_NODE : 1 ;
201
210
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.
211
+ // Remove existing non-element child nodes and SVGs, and add the new SVG element. Note that
212
+ // we can't use innerHTML, because IE will throw if the element has a data binding.
204
213
for ( let i = 0 ; i < childCount ; i ++ ) {
205
- layoutElement . removeChild ( layoutElement . childNodes [ i ] ) ;
214
+ const child = layoutElement . childNodes [ i ] ;
215
+
216
+ if ( child . nodeType !== elementNodeType || child . nodeName . toLowerCase ( ) === 'svg' ) {
217
+ layoutElement . removeChild ( child ) ;
218
+ }
206
219
}
207
220
}
208
221
0 commit comments