Skip to content

Commit 8e394a1

Browse files
crisbetoandrewseguin
authored andcommitted
fix(icon): IE/Edge ignoring style tags inside inline SVG (#11531)
Adds a workaround for an issue in IE11 and Edge where `style` tags inside dynamically-created SVGs will be ignored. Fixes #11458.
1 parent fcb4a47 commit 8e394a1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/lib/icon/icon.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,26 @@ describe('MatIcon', () => {
539539
verifyPathChildElement(svgElement, 'left');
540540
expect(svgElement.getAttribute('viewBox')).toBeFalsy();
541541
});
542+
543+
it('should add an extra string to the end of `style` tags inside SVG', fakeAsync(() => {
544+
iconRegistry.addSvgIconLiteral('fido', trustHtml(`
545+
<svg>
546+
<style>#woof {color: blue;}</style>
547+
<path id="woof" name="woof"></path>
548+
</svg>
549+
`));
550+
551+
const fixture = TestBed.createComponent(IconFromSvgName);
552+
fixture.componentInstance.iconName = 'fido';
553+
fixture.detectChanges();
554+
const styleTag = fixture.nativeElement.querySelector('mat-icon svg style');
555+
556+
// Note the extra whitespace at the end which is what we're testing for. This is a
557+
// workaround for IE and Edge ignoring `style` tags in dynamically-created SVGs.
558+
expect(styleTag.textContent).toBe('#woof {color: blue;} ');
559+
560+
tick();
561+
}));
542562
});
543563

544564
describe('custom fonts', () => {

src/lib/icon/icon.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
182182

183183
private _setSvgElement(svg: SVGElement) {
184184
this._clearSvgElement();
185+
186+
// Workaround for IE11 and Edge ignoring `style` tags inside dynamically-created SVGs.
187+
// See: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10898469/
188+
// Do this before inserting the element into the DOM, in order to avoid a style recalculation.
189+
const styleTags = svg.querySelectorAll('style') as NodeListOf<HTMLStyleElement>;
190+
191+
for (let i = 0; i < styleTags.length; i++) {
192+
styleTags[i].textContent += ' ';
193+
}
194+
185195
this._elementRef.nativeElement.appendChild(svg);
186196
}
187197

0 commit comments

Comments
 (0)