Skip to content

Replace removeChild with remove #213465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extensions/notebook-renderers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { formatStackTrace } from './stackTraceHelper';

function clearContainer(container: HTMLElement) {
while (container.firstChild) {
container.removeChild(container.firstChild);
container.firstChild.remove();
}
}

Expand Down Expand Up @@ -378,7 +378,7 @@ function renderStream(outputInfo: OutputWithAppend, outputElement: HTMLElement,
contentParent = document.createElement('div');
contentParent.appendChild(newContent);
while (outputElement.firstChild) {
outputElement.removeChild(outputElement.firstChild);
outputElement.firstChild.remove();
}
outputElement.appendChild(contentParent);
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/dnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function applyDragImage(event: DragEvent, label: string | null, clazz: st
event.dataTransfer.setDragImage(dragImage, -10, -10);

// Removes the element when the DND operation is done
setTimeout(() => ownerDocument.body.removeChild(dragImage), 0);
setTimeout(() => dragImage.remove(), 0);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/vs/base/browser/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ export function createStyleSheet(container: HTMLElement = mainWindow.document.he
container.appendChild(style);

if (disposableStore) {
disposableStore.add(toDisposable(() => container.removeChild(style)));
disposableStore.add(toDisposable(() => style.remove()));
}

// With <head> as container, the stylesheet becomes global and is tracked
Expand Down Expand Up @@ -1005,7 +1005,7 @@ function cloneGlobalStyleSheet(globalStylesheet: HTMLStyleElement, globalStylesh

const clone = globalStylesheet.cloneNode(true) as HTMLStyleElement;
targetWindow.document.head.appendChild(clone);
disposables.add(toDisposable(() => targetWindow.document.head.removeChild(clone)));
disposables.add(toDisposable(() => clone.remove()));

for (const rule of getDynamicStyleSheetRules(globalStylesheet)) {
clone.sheet?.insertRule(rule.cssText, clone.sheet?.cssRules.length);
Expand Down Expand Up @@ -1727,7 +1727,7 @@ export function triggerDownload(dataOrUri: Uint8Array | URI, name: string): void
anchor.click();

// Ensure to remove the element from DOM eventually
setTimeout(() => activeWindow.document.body.removeChild(anchor));
setTimeout(() => anchor.remove());
}

export function triggerUpload(): Promise<FileList | undefined> {
Expand All @@ -1750,7 +1750,7 @@ export function triggerUpload(): Promise<FileList | undefined> {
input.click();

// Ensure to remove the element from DOM eventually
setTimeout(() => activeWindow.document.body.removeChild(input));
setTimeout(() => input.remove());
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/markdownRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ function sanitizeRenderedMarkdown(
if (element.attributes.getNamedItem('type')?.value === 'checkbox') {
element.setAttribute('disabled', '');
} else if (!options.replaceWithPlaintext) {
element.parentElement?.removeChild(element);
element.remove();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/actionbar/actionbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export class ActionBar extends Disposable implements IActionRunner {

pull(index: number): void {
if (index >= 0 && index < this.viewItems.length) {
this.actionsList.removeChild(this.actionsList.childNodes[index]);
this.actionsList.childNodes[index].remove();
this.viewItemDisposables.deleteAndDispose(this.viewItems[index]);
dispose(this.viewItems.splice(index, 1));
this.refreshRole();
Expand Down
6 changes: 2 additions & 4 deletions src/vs/base/browser/ui/centered/centeredViewLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class CenteredViewLayout implements IDisposable {
}

if (active) {
this.container.removeChild(this.view.element);
this.view.element.remove();
this.splitView = new SplitView(this.container, {
inverseAltBehavior: true,
orientation: Orientation.HORIZONTAL,
Expand Down Expand Up @@ -195,9 +195,7 @@ export class CenteredViewLayout implements IDisposable {

this.resizeSplitViews();
} else {
if (this.splitView) {
this.container.removeChild(this.splitView.el);
}
this.splitView?.el.remove();
this.splitViewDisposables.clear();
this.splitView?.dispose();
this.splitView = undefined;
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/contextview/contextview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,11 @@ export class ContextView extends Disposable {
if (this.container) {
this.toDisposeOnSetContainer.dispose();

this.view.remove();
if (this.shadowRoot) {
this.shadowRoot.removeChild(this.view);
this.shadowRoot = null;
this.shadowRootHostElement?.remove();
this.shadowRootHostElement = null;
} else {
this.container.removeChild(this.view);
}

this.container = null;
Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/browser/ui/grid/gridview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ export class GridView implements IDisposable {
const oldRoot = this._root;

if (oldRoot) {
this.element.removeChild(oldRoot.element);
oldRoot.element.remove();
oldRoot.dispose();
}

Expand Down Expand Up @@ -1831,6 +1831,6 @@ export class GridView implements IDisposable {
dispose(): void {
this.onDidSashResetRelay.dispose();
this.root.dispose();
this.element.parentElement?.removeChild(this.element);
this.element.remove();
}
}
8 changes: 3 additions & 5 deletions src/vs/base/browser/ui/list/listView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ export class ListView<T> implements IListView<T> {
const container = getDragImageContainer(this.domNode);
container.appendChild(dragImage);
event.dataTransfer.setDragImage(dragImage, -10, -10);
setTimeout(() => container.removeChild(dragImage), 0);
setTimeout(() => dragImage.remove(), 0);
}

this.domNode.classList.add('dragging');
Expand Down Expand Up @@ -1542,7 +1542,7 @@ export class ListView<T> implements IListView<T> {
this.virtualDelegate.setDynamicHeight?.(item.element, item.size);

item.lastDynamicHeightWidth = this.renderWidth;
this.rowsContainer.removeChild(row.domNode);
row.domNode.remove();
this.cache.release(row);

return item.size - size;
Expand Down Expand Up @@ -1570,9 +1570,7 @@ export class ListView<T> implements IListView<T> {

this.items = [];

if (this.domNode && this.domNode.parentNode) {
this.domNode.parentNode.removeChild(this.domNode);
}
this.domNode?.remove();

this.dragOverAnimationDisposable?.dispose();
this.disposables.dispose();
Expand Down
10 changes: 1 addition & 9 deletions src/vs/base/browser/ui/list/rowCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ export interface IRow {
templateData: any;
}

function removeFromParent(element: HTMLElement): void {
try {
element.parentElement?.removeChild(element);
} catch (e) {
// this will throw if this happens due to a blur event, nasty business
}
}

export class RowCache<T> implements IDisposable {

private cache = new Map<string, IRow[]>();
Expand Down Expand Up @@ -104,7 +96,7 @@ export class RowCache<T> implements IDisposable {

private doRemoveNode(domNode: HTMLElement) {
domNode.classList.remove('scrolling');
removeFromParent(domNode);
domNode.remove();
}

private getTemplateCache(templateId: string): IRow[] {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/sash/sash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ export class Sash extends Disposable {
const onPointerUp = (e: PointerEvent) => {
EventHelper.stop(e, false);

this.el.removeChild(style);
style.remove();

this.el.classList.remove('active');
this._onDidEnd.fire();
Expand Down
25 changes: 10 additions & 15 deletions src/vs/base/browser/ui/selectBox/selectBoxCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,7 @@ export class SelectBoxList extends Disposable implements ISelectBoxDelegate, ILi
return {
dispose: () => {
// contextView will dispose itself if moving from one View to another
try {
container.removeChild(this.selectDropDownContainer); // remove to take out the CSS rules we add
}
catch (error) {
// Ignore, removed already by change of focus
}
this.selectDropDownContainer.remove(); // remove to take out the CSS rules we add
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this throw? why was it try/catch before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. parent.removeChild(child) throws if child is not a child of parent. child.remove() never throws. Aside from not needing a reference to the parent, that’s the only difference.

}
};
}
Expand Down Expand Up @@ -612,20 +607,20 @@ export class SelectBoxList extends Disposable implements ISelectBoxDelegate, ILi
&& this.options.length > maxVisibleOptionsBelow
) {
this._dropDownPosition = AnchorPosition.ABOVE;
this.selectDropDownContainer.removeChild(this.selectDropDownListContainer);
this.selectDropDownContainer.removeChild(this.selectionDetailsPane);
this.selectDropDownContainer.appendChild(this.selectionDetailsPane);
this.selectDropDownContainer.appendChild(this.selectDropDownListContainer);
this.selectDropDownListContainer.remove();
this.selectionDetailsPane.remove();
this.selectionDetailsPane.remove();
this.selectDropDownListContainer.remove();

this.selectionDetailsPane.classList.remove('border-top');
this.selectionDetailsPane.classList.add('border-bottom');

} else {
this._dropDownPosition = AnchorPosition.BELOW;
this.selectDropDownContainer.removeChild(this.selectDropDownListContainer);
this.selectDropDownContainer.removeChild(this.selectionDetailsPane);
this.selectDropDownContainer.appendChild(this.selectDropDownListContainer);
this.selectDropDownContainer.appendChild(this.selectionDetailsPane);
this.selectDropDownListContainer.remove();
this.selectionDetailsPane.remove();
this.selectDropDownListContainer.remove();
this.selectionDetailsPane.remove();

this.selectionDetailsPane.classList.remove('border-bottom');
this.selectionDetailsPane.classList.add('border-top');
Expand Down Expand Up @@ -879,7 +874,7 @@ export class SelectBoxList extends Disposable implements ISelectBoxDelegate, ILi

const tagName = child.tagName && child.tagName.toLowerCase();
if (tagName === 'img') {
element.removeChild(child);
child.remove();
} else {
cleanRenderedMarkdown(child);
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/splitview/paneview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class PaneDraggable extends Disposable {

const dragImage = append(this.pane.element.ownerDocument.body, $('.monaco-drag-image', {}, this.pane.draggableElement.textContent || ''));
e.dataTransfer.setDragImage(dragImage, -10, -10);
setTimeout(() => this.pane.element.ownerDocument.body.removeChild(dragImage), 0);
setTimeout(() => dragImage.remove(), 0);

this.context.draggable = this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/splitview/splitview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ export class SplitView<TLayoutContext = undefined, TView extends IView<TLayoutCo
}

const onChangeDisposable = view.onDidChange(size => this.onViewChange(item, size));
const containerDisposable = toDisposable(() => this.viewContainer.removeChild(container));
const containerDisposable = toDisposable(() => container.remove());
const disposable = combinedDisposable(onChangeDisposable, containerDisposable);

let viewSize: ViewItemSize;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/tree/abstractTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ class FindWidget<T, TFilterData> extends Disposable {
super();

container.appendChild(this.elements.root);
this._register(toDisposable(() => container.removeChild(this.elements.root)));
this._register(toDisposable(() => this.elements.root.remove()));

const styles = options?.styles ?? unthemedFindWidgetStyles;

Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/test/browser/progressBar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ suite('ProgressBar', () => {
});

teardown(() => {
mainWindow.document.body.removeChild(fixture);
fixture.remove();
});

test('Progress Bar', function () {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/config/charWidthReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DomCharWidthReader {
this._readFromDomElements();

// Remove the container from the DOM
targetWindow.document.body.removeChild(this._container!);
this._container?.remove();

this._container = null;
this._testElements = null;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/controller/textAreaHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ function measureText(targetDocument: Document, text: string, fontInfo: FontInfo,

const res = regularDomNode.offsetWidth;

targetDocument.body.removeChild(container);
container.remove();

return res;
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class RefCountedStyleSheet {
public unref(): void {
this._refCount--;
if (this._refCount === 0) {
this._styleSheet.parentNode?.removeChild(this._styleSheet);
this._styleSheet.remove();
this._parent._removeEditorStyleSheets(this._editorId);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/view/domLineBreaksComputer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function createLineBreaks(targetWindow: Window, requests: string[], fontInfo: Fo
result[i] = new ModelLineProjectionData(injectionOffsets, injectionOptions, breakOffsets, breakOffsetsVisibleColumn, wrappedTextIndentLength);
}

targetWindow.document.body.removeChild(containerDomNode);
containerDomNode.remove();
return result;
}

Expand Down
16 changes: 4 additions & 12 deletions src/vs/editor/browser/view/viewLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ export class VisibleLinesCollection<T extends IVisibleLine> {
// Remove from DOM
for (let i = 0, len = deleted.length; i < len; i++) {
const lineDomNode = deleted[i].getDomNode();
if (lineDomNode) {
this.domNode.domNode.removeChild(lineDomNode);
}
lineDomNode?.remove();
}
}

Expand All @@ -310,9 +308,7 @@ export class VisibleLinesCollection<T extends IVisibleLine> {
// Remove from DOM
for (let i = 0, len = deleted.length; i < len; i++) {
const lineDomNode = deleted[i].getDomNode();
if (lineDomNode) {
this.domNode.domNode.removeChild(lineDomNode);
}
lineDomNode?.remove();
}
}

Expand Down Expand Up @@ -481,9 +477,7 @@ class ViewLayerRenderer<T extends IVisibleLine> {
private _removeLinesBefore(ctx: IRendererContext<T>, removeCount: number): void {
for (let i = 0; i < removeCount; i++) {
const lineDomNode = ctx.lines[i].getDomNode();
if (lineDomNode) {
this.domNode.removeChild(lineDomNode);
}
lineDomNode?.remove();
}
ctx.lines.splice(0, removeCount);
}
Expand All @@ -502,9 +496,7 @@ class ViewLayerRenderer<T extends IVisibleLine> {

for (let i = 0; i < removeCount; i++) {
const lineDomNode = ctx.lines[removeIndex + i].getDomNode();
if (lineDomNode) {
this.domNode.removeChild(lineDomNode);
}
lineDomNode?.remove();
}
ctx.lines.splice(removeIndex, removeCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class ViewContentWidgets extends ViewPart {
delete this._widgets[widgetId];

const domNode = myWidget.domNode.domNode;
domNode.parentNode!.removeChild(domNode);
domNode.remove();
domNode.removeAttribute('monaco-visible-content-widget');

this.setShouldRender();
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/viewParts/glyphMargin/glyphMargin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export class GlyphMarginWidgets extends ViewPart {
const domNode = widgetData.domNode.domNode;
delete this._widgets[widgetId];

domNode.parentNode?.removeChild(domNode);
domNode.remove();
this.setShouldRender();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/browser/viewParts/viewZones/viewZones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ export class ViewZones extends ViewPart {

zone.domNode.removeAttribute('monaco-visible-view-zone');
zone.domNode.removeAttribute('monaco-view-zone');
zone.domNode.domNode.parentNode!.removeChild(zone.domNode.domNode);
zone.domNode.domNode.remove();

if (zone.marginDomNode) {
zone.marginDomNode.removeAttribute('monaco-visible-view-zone');
zone.marginDomNode.removeAttribute('monaco-view-zone');
zone.marginDomNode.domNode.parentNode!.removeChild(zone.marginDomNode.domNode);
zone.marginDomNode.domNode.remove();
}

this.setShouldRender();
Expand Down
Loading
Loading