Skip to content
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

fix: BrowserWindow.fromWebContents() can return null #19983

Merged
merged 1 commit into from Sep 27, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/api/browser-window.md
Expand Up @@ -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)`

Expand Down
2 changes: 2 additions & 0 deletions lib/browser/api/browser-window.js
Expand Up @@ -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) => {
Expand Down
6 changes: 3 additions & 3 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -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()
}
Expand Down