Skip to content

Commit dd6aec6

Browse files
pavel-agarkovjelbourn
authored andcommitted
fix(icon): reverse for loop when removing child nodes from mat-icon (#12078)
When mat-icon element has more than one child node (like when mat-badge is used) then child nodes should be removed in the reversed order otherwise you will attempt to get node by index which is not correct any more since you've deleted some nodes already in the previous iterations and it will cause an error.
1 parent d4623ff commit dd6aec6

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/lib/icon/icon.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
196196

197197
private _clearSvgElement() {
198198
const layoutElement: HTMLElement = this._elementRef.nativeElement;
199-
const childCount = layoutElement.childNodes.length;
199+
let childCount = layoutElement.childNodes.length;
200200

201201
// Remove existing non-element child nodes and SVGs, and add the new SVG element. Note that
202202
// we can't use innerHTML, because IE will throw if the element has a data binding.
203-
for (let i = 0; i < childCount; i++) {
204-
const child = layoutElement.childNodes[i];
203+
while (childCount--) {
204+
const child = layoutElement.childNodes[childCount];
205205

206206
// 1 corresponds to Node.ELEMENT_NODE. We remove all non-element nodes in order to get rid
207207
// of any loose text nodes, as well as any SVG elements in order to remove any old icons.

0 commit comments

Comments
 (0)