Browse Source

fix: <webview> focus / blur events don't work with contextIsolation enabled (#29004) (#29026)

Milan Burda 4 years ago
parent
commit
29d8cf5423
2 changed files with 26 additions and 1 deletions
  1. 1 1
      lib/renderer/web-view/web-view-impl.ts
  2. 25 0
      spec-main/webview-spec.ts

+ 1 - 1
lib/renderer/web-view/web-view-impl.ts

@@ -158,7 +158,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(new Event(hasFocus ? 'focus' : 'blur'));

+ 25 - 0
spec-main/webview-spec.ts

@@ -659,4 +659,29 @@ describe('<webview> tag', function () {
     generateSpecs('without sandbox', false);
     generateSpecs('with sandbox', true);
   });
+
+  describe('DOM events', () => {
+    afterEach(closeAllWindows);
+    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)
+      })`);
+    });
+  });
 });