diff --git a/shell/browser/api/electron_api_view.cc b/shell/browser/api/electron_api_view.cc index eea9c8098b..fb35345e09 100644 --- a/shell/browser/api/electron_api_view.cc +++ b/shell/browser/api/electron_api_view.cc @@ -189,6 +189,14 @@ void View::AddChildViewAt(gin::Handle child, // has a View, possibly a wrapper view around the underlying platform View. if (!view_) return; + + // This will CHECK and crash in View::AddChildViewAtImpl if not handled here. + if (view_ == child->view()) { + gin_helper::ErrorThrower(isolate()).ThrowError( + "A view cannot be added as its own child"); + return; + } + size_t index = std::min(child_views_.size(), maybe_index.value_or(child_views_.size())); child_views_.emplace(child_views_.begin() + index, // index diff --git a/spec/api-view-spec.ts b/spec/api-view-spec.ts index ada81800ea..020f3ce0d6 100644 --- a/spec/api-view-spec.ts +++ b/spec/api-view-spec.ts @@ -1,3 +1,4 @@ +import { expect } from 'chai'; import { closeWindow } from './lib/window-helpers'; import { BaseWindow, View } from 'electron/main'; @@ -10,6 +11,15 @@ describe('View', () => { it('can be used as content view', () => { w = new BaseWindow({ show: false }); - w.setContentView(new View()); + const v = new View(); + w.setContentView(v); + expect(w.contentView).to.equal(v); + }); + + it('will throw when added as a child to itself', () => { + w = new BaseWindow({ show: false }); + expect(() => { + w.contentView.addChildView(w.contentView); + }).to.throw('A view cannot be added as its own child'); }); });