Skip to content

Commit

Permalink
fix: <webview> focus / blur events don't work with contextIsolation e…
Browse files Browse the repository at this point in the history
…nabled (#29024)

Co-authored-by: Milan Burda <milan.burda@gmail.com>
  • Loading branch information
trop[bot] and miniak committed May 6, 2021
1 parent c542671 commit 546cb98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/renderer/web-view/web-view-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 22 additions & 0 deletions spec-main/webview-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,5 +721,27 @@ describe('<webview> 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)
})`);
});
});
});

0 comments on commit 546cb98

Please sign in to comment.