diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 2668c2fd03e..928eff76009 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -664,7 +664,8 @@ Returns `BrowserWindow | null` - The window that is focused in this application, * `webContents` [WebContents](web-contents.md) -Returns `BrowserWindow` - The window that owns the given `webContents`. +Returns `BrowserWindow | null` - The window that owns the given `webContents` +or `null` if the contents are not owned by a window. #### `BrowserWindow.fromBrowserView(browserView)` diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 939124633be..521414fd01b 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -118,6 +118,8 @@ BrowserWindow.fromWebContents = (webContents) => { for (const window of BrowserWindow.getAllWindows()) { if (window.webContents.equal(webContents)) return window } + + return null } BrowserWindow.fromBrowserView = (browserView) => { diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index f0d079aa509..00f069366e4 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -1304,13 +1304,13 @@ describe('BrowserWindow module', () => { it('returns the window with the webContents', () => { const w = new BrowserWindow({show: false}) const found = BrowserWindow.fromWebContents(w.webContents) - expect(found.id).to.equal(w.id) + expect(found!.id).to.equal(w.id) }) - it('returns undefined for webContents without a BrowserWindow', () => { + it('returns null for webContents without a BrowserWindow', () => { const contents = (webContents as any).create({}) try { - expect(BrowserWindow.fromWebContents(contents)).to.be.undefined('BrowserWindow.fromWebContents(contents)') + expect(BrowserWindow.fromWebContents(contents)).to.be.null('BrowserWindow.fromWebContents(contents)') } finally { contents.destroy() }