diff --git a/lib/renderer/web-view/web-view-impl.ts b/lib/renderer/web-view/web-view-impl.ts index 494c779cbf2e7..ccffddfd5d7c9 100644 --- a/lib/renderer/web-view/web-view-impl.ts +++ b/lib/renderer/web-view/web-view-impl.ts @@ -162,7 +162,7 @@ export class WebViewImpl { // Emits focus/blur events. onFocusChange () { - const hasFocus = document.activeElement === this.webviewNode; + const hasFocus = this.webviewNode.ownerDocument.activeElement === this.webviewNode; if (hasFocus !== this.hasFocus) { this.hasFocus = hasFocus; this.dispatchEvent(hasFocus ? 'focus' : 'blur'); diff --git a/spec-main/webview-spec.ts b/spec-main/webview-spec.ts index 46b164a9dc581..e52af7cbba705 100644 --- a/spec-main/webview-spec.ts +++ b/spec-main/webview-spec.ts @@ -672,5 +672,27 @@ describe(' tag', function () { })`); expect(message).to.equal('hi'); }); + + it('emits focus event when contextIsolation is enabled', async () => { + const w = new BrowserWindow({ + show: false, + webPreferences: { + webviewTag: true, + contextIsolation: true + } + }); + await w.loadURL('about:blank'); + await w.webContents.executeJavaScript(`new Promise((resolve, reject) => { + const webview = new WebView() + webview.setAttribute('src', 'about:blank') + webview.addEventListener('dom-ready', () => { + webview.focus() + }) + webview.addEventListener('focus', () => { + resolve(); + }) + document.body.appendChild(webview) + })`); + }); }); });