From 92e48d411302fe27d7f2a6dc895a15f4bcd4a066 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 10 Jan 2019 23:59:44 +0100 Subject: [PATCH] fix: BrowserWindow.fromWebContents() can return null --- docs/api/browser-window.md | 3 ++- lib/browser/api/browser-window.js | 2 ++ spec-main/api-browser-window-spec.ts | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 117469f20f1de..73bad29f488e4 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -658,7 +658,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 939124633be7a..521414fd01bab 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 03d30154512ae..c768fe91bfed4 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -1307,10 +1307,10 @@ describe('BrowserWindow module', () => { 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() }