Skip to content

Commit 82072ac

Browse files
authored
refactor: clean up removeChild usages (#23592)
Now that we don't have to support IE11 anymore, we can use `Element.remove` instead of `removeChild`.
1 parent 64ba72f commit 82072ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+136
-221
lines changed

src/cdk-experimental/column-resize/resizable.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,8 @@ export abstract class Resizable<HandleComponent extends ResizeOverlayHandle>
108108
ngOnDestroy(): void {
109109
this.destroyed.next();
110110
this.destroyed.complete();
111-
112-
if (this.inlineHandle) {
113-
this.elementRef.nativeElement!.removeChild(this.inlineHandle);
114-
}
115-
116-
if (this.overlayRef) {
117-
this.overlayRef.dispose();
118-
}
111+
this.inlineHandle?.remove();
112+
this.overlayRef?.dispose();
119113
}
120114

121115
protected abstract getInlineHandleCssClassName(): string;

src/cdk-experimental/column-resize/resize-strategy.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,8 @@ export class CdkFlexTableResizeStrategy extends ResizeStrategy implements OnDest
178178
}
179179

180180
ngOnDestroy(): void {
181-
// TODO: Use remove() once we're off IE11.
182-
if (this._styleElement && this._styleElement.parentNode) {
183-
this._styleElement.parentNode.removeChild(this._styleElement);
184-
this._styleElement = undefined;
185-
}
181+
this._styleElement?.remove();
182+
this._styleElement = undefined;
186183
}
187184

188185
private _getPropertyValue(cssFriendlyColumnName: string, key: string): string|undefined {

src/cdk-experimental/dialog/dialog.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ describe('Dialog', () => {
877877
describe('focus management', () => {
878878
// When testing focus, all of the elements must be in the DOM.
879879
beforeEach(() => document.body.appendChild(overlayContainerElement));
880-
afterEach(() => document.body.removeChild(overlayContainerElement));
880+
afterEach(() => overlayContainerElement.remove());
881881

882882
it('should focus the first tabbable element of the dialog on open (the default)',
883883
fakeAsync(() => {
@@ -971,7 +971,7 @@ describe('Dialog', () => {
971971
.withContext('Expected that the trigger was refocused after the dialog is closed.')
972972
.toBe('dialog-trigger');
973973

974-
document.body.removeChild(button);
974+
button.remove();
975975
}));
976976

977977
it('should re-focus trigger element inside the shadow DOM when dialog closes', fakeAsync(() => {
@@ -1036,8 +1036,8 @@ describe('Dialog', () => {
10361036
expect(document.activeElement!.id)
10371037
.withContext('Expected focus to stay on the alternate button.').toBe('other-button');
10381038

1039-
body.removeChild(button);
1040-
body.removeChild(otherButton);
1039+
button.remove();
1040+
otherButton.remove();
10411041
}));
10421042

10431043
it('should allow the consumer to shift focus in afterClosed', fakeAsync(() => {
@@ -1069,8 +1069,8 @@ describe('Dialog', () => {
10691069
.withContext('Expected that the trigger was refocused after the dialog is closed.')
10701070
.toBe('input-to-be-focused');
10711071

1072-
document.body.removeChild(button);
1073-
document.body.removeChild(input);
1072+
button.remove();
1073+
input.remove();
10741074
flush();
10751075
}));
10761076

src/cdk-experimental/listbox/listbox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
174174
private _removeIcons(element: Element) {
175175
// TODO: make this a configurable function that can removed any desired type of node.
176176
for (const icon of Array.from(element.querySelectorAll('mat-icon, .material-icons'))) {
177-
icon.parentNode?.removeChild(icon);
177+
icon.remove();
178178
}
179179
}
180180

src/cdk-experimental/menu/menu-item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {Toggler, MENU_AIM, MenuAim} from './menu-aim';
3535
/** Removes all icons from within the given element. */
3636
function removeIcons(element: Element) {
3737
for (const icon of Array.from(element.querySelectorAll('mat-icon, .material-icons'))) {
38-
icon.parentNode?.removeChild(icon);
38+
icon.remove();
3939
}
4040
}
4141

src/cdk-experimental/table-scroll-container/table-scroll-container.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,8 @@ export class CdkTableScrollContainer implements StickyPositioningListener,
6767
}
6868

6969
ngOnDestroy(): void {
70-
// TODO: Use remove() once we're off IE11.
71-
if (this._styleElement?.parentNode) {
72-
this._styleElement.parentNode.removeChild(this._styleElement);
73-
this._styleElement = undefined;
74-
}
70+
this._styleElement?.remove();
71+
this._styleElement = undefined;
7572
}
7673

7774
stickyColumnsUpdated({sizes}: StickyUpdate): void {

src/cdk/a11y/aria-describer/aria-describer.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,7 @@ describe('AriaDescriber', () => {
214214

215215
// Use `querySelectorAll` with an attribute since `getElementById` will stop at the first match.
216216
expect(document.querySelectorAll(`[id='${MESSAGES_CONTAINER_ID}']`).length).toBe(1);
217-
218-
if (extraContainer.parentNode) {
219-
extraContainer.parentNode.removeChild(extraContainer);
220-
}
217+
extraContainer.remove();
221218
});
222219

223220
it('should not describe messages that match up with the aria-label of the element', () => {

src/cdk/a11y/aria-describer/aria-describer.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ export class AriaDescriber implements OnDestroy {
156156
/** Deletes the message element from the global messages container. */
157157
private _deleteMessageElement(key: string|Element) {
158158
const registeredMessage = messageRegistry.get(key);
159-
const messageElement = registeredMessage && registeredMessage.messageElement;
160-
if (messagesContainer && messageElement) {
161-
messagesContainer.removeChild(messageElement);
162-
}
159+
registeredMessage?.messageElement?.remove();
163160
messageRegistry.delete(key);
164161
}
165162

@@ -172,9 +169,7 @@ export class AriaDescriber implements OnDestroy {
172169
// already a container on the page, but we don't have a reference to it. Clear the
173170
// old container so we don't get duplicates. Doing this, instead of emptying the previous
174171
// container, should be slightly faster.
175-
if (preExistingContainer && preExistingContainer.parentNode) {
176-
preExistingContainer.parentNode.removeChild(preExistingContainer);
177-
}
172+
preExistingContainer?.remove();
178173

179174
messagesContainer = this._document.createElement('div');
180175
messagesContainer.id = MESSAGES_CONTAINER_ID;
@@ -193,8 +188,8 @@ export class AriaDescriber implements OnDestroy {
193188

194189
/** Deletes the global messages container. */
195190
private _deleteMessagesContainer() {
196-
if (messagesContainer && messagesContainer.parentNode) {
197-
messagesContainer.parentNode.removeChild(messagesContainer);
191+
if (messagesContainer) {
192+
messagesContainer.remove();
198193
messagesContainer = null;
199194
}
200195
}

src/cdk/a11y/aria-describer/aria-reference.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('AriaReference', () => {
99
});
1010

1111
afterEach(() => {
12-
document.body.removeChild(testElement!);
12+
testElement?.remove();
1313
});
1414

1515
it('should be able to append/remove aria reference IDs', () => {

src/cdk/a11y/focus-trap/focus-trap.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,12 @@ export class FocusTrap {
7676

7777
if (startAnchor) {
7878
startAnchor.removeEventListener('focus', this.startAnchorListener);
79-
80-
if (startAnchor.parentNode) {
81-
startAnchor.parentNode.removeChild(startAnchor);
82-
}
79+
startAnchor.remove();
8380
}
8481

8582
if (endAnchor) {
8683
endAnchor.removeEventListener('focus', this.endAnchorListener);
87-
88-
if (endAnchor.parentNode) {
89-
endAnchor.parentNode.removeChild(endAnchor);
90-
}
84+
endAnchor.remove();
9185
}
9286

9387
this._startAnchor = this._endAnchor = null;

0 commit comments

Comments
 (0)