Skip to content

Commit 5561cf7

Browse files
crisbetommalerba
authored andcommitted
refactor(icon): id attribute not being cleared completely (#10742)
Currently when we clear the id of an icon, we set it to an empty string which still keeps it on the element. This change removes the attribute altogether.
1 parent 92e5f55 commit 5561cf7

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/lib/icon/icon-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ export class MatIconRegistry {
451451
// Clone the element and remove the ID to prevent multiple elements from being added
452452
// to the page with the same ID.
453453
const iconElement = iconSource.cloneNode(true) as Element;
454-
iconElement.id = '';
454+
iconElement.removeAttribute('id');
455455

456456
// If the icon node is itself an <svg> node, clone and return it directly. If not, set it as
457457
// the content of a new <svg> node.

src/lib/icon/icon.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ describe('MatIcon', () => {
260260
// The <svg> child should be the <g id="pig"> element.
261261
expect(svgChild.tagName.toLowerCase()).toBe('g');
262262
expect(svgChild.getAttribute('name')).toBe('pig');
263-
expect(svgChild.getAttribute('id')).toBe('');
263+
expect(svgChild.getAttribute('id')).toBeFalsy();
264264
expect(svgChild.childNodes.length).toBe(1);
265265
verifyPathChildElement(svgChild, 'oink');
266266

@@ -278,6 +278,21 @@ describe('MatIcon', () => {
278278
verifyPathChildElement(svgChild, 'moo moo');
279279
});
280280

281+
it('should clear the id attribute from the svg node', () => {
282+
iconRegistry.addSvgIconSetInNamespace('farm', trustUrl('farm-set-1.svg'));
283+
284+
const fixture = TestBed.createComponent(IconFromSvgName);
285+
286+
fixture.componentInstance.iconName = 'farm:pig';
287+
fixture.detectChanges();
288+
http.expectOne('farm-set-1.svg').flush(FAKE_SVGS.farmSet1);
289+
290+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
291+
const svgElement = verifyAndGetSingleSvgChild(matIconElement);
292+
293+
expect(svgElement.hasAttribute('id')).toBe(false);
294+
});
295+
281296
it('should unwrap <symbol> nodes', () => {
282297
iconRegistry.addSvgIconSetInNamespace('farm', trustUrl('farm-set-3.svg'));
283298

@@ -476,7 +491,7 @@ describe('MatIcon', () => {
476491
// The <svg> child should be the <g id="pig"> element.
477492
expect(svgChild.tagName.toLowerCase()).toBe('g');
478493
expect(svgChild.getAttribute('name')).toBe('pig');
479-
expect(svgChild.getAttribute('id')).toBe('');
494+
expect(svgChild.getAttribute('id')).toBeFalsy();
480495
expect(svgChild.childNodes.length).toBe(1);
481496
verifyPathChildElement(svgChild, 'oink');
482497

0 commit comments

Comments
 (0)