Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(desktop): leave closed dialogs out of browser snapshots (#20415)

Arvin Xud18699a11cc5
diff --git a/apps/desktop/src/main/controllers/BrowserControlCtr.ts b/apps/desktop/src/main/controllers/BrowserControlCtr.ts
index 9a42f149687..3fcb3f7e307 100644
--- a/apps/desktop/src/main/controllers/BrowserControlCtr.ts
+++ b/apps/desktop/src/main/controllers/BrowserControlCtr.ts
@@ -103,16 +103,29 @@ const evaluate = (guest: WebContents, code: string): Promise<any> =>
* caches the elements on `window.__lobeBrowserRefs` so later actions can
* resolve `ref` ids without re-querying.
*/
-const SNAPSHOT_SCRIPT = `(() => {
+export const SNAPSHOT_SCRIPT = `(() => {
const MAX = 250;
const refs = {};
let counter = 0;
const lines = [];
+ // Closed modals are commonly kept in the DOM under a wrapper faded to
+ // opacity 0, or marked aria-hidden / inert. Their own box still has a size,
+ // so checking only the element listed every closed dialog as if it were open.
+ // The element's own opacity is not checked: styled checkboxes and file
+ // inputs are often a transparent native control over a visible label.
+ const hiddenByAncestor = (el) => {
+ for (let node = el; node && node !== document.documentElement; node = node.parentElement) {
+ if (node.getAttribute('aria-hidden') === 'true' || node.hasAttribute('inert')) return true;
+ if (node !== el && getComputedStyle(node).opacity === '0') return true;
+ }
+ return false;
+ };
const isVisible = (el) => {
const r = el.getBoundingClientRect();
if (r.width < 2 || r.height < 2) return false;
const s = getComputedStyle(el);
- return s.visibility !== 'hidden' && s.display !== 'none';
+ if (s.visibility === 'hidden' || s.display === 'none') return false;
+ return !hiddenByAncestor(el);
};
const tagRoles = { A: 'link', BUTTON: 'button', H1: 'heading', H2: 'heading', H3: 'heading', SELECT: 'combobox', SUMMARY: 'button', TEXTAREA: 'textbox' };
const roleOf = (el) => {
diff --git a/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.snapshot.test.ts b/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.snapshot.test.ts
new file mode 100644
index 00000000000..6c5c7008fab
--- /dev/null
+++ b/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.snapshot.test.ts
@@ -0,0 +1,77 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { SNAPSHOT_SCRIPT } from '../BrowserControlCtr';
+
+vi.mock('electron', () => ({
+ ipcMain: { handle: vi.fn() },
+}));
+
+const snapshotOf = (html: string): string => {
+ document.body.innerHTML = html;
+
+ return JSON.parse((0, eval)(SNAPSHOT_SCRIPT)).snapshot;
+};
+
+describe('SNAPSHOT_SCRIPT visibility', () => {
+ beforeEach(() => {
+ // happy-dom does no layout; give every element a real box.
+ vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
+ bottom: 30,
+ height: 30,
+ left: 0,
+ right: 100,
+ top: 0,
+ width: 100,
+ x: 0,
+ y: 0,
+ } as DOMRect);
+ });
+
+ it('leaves out a closed dialog kept in the DOM under a transparent wrapper', () => {
+ const snapshot = snapshotOf(`
+ <button>Add album</button>
+ <div class="modal" style="opacity: 0">
+ <div role="dialog" aria-label="Enter admin password">
+ <input placeholder="Password" />
+ <button>Verify and continue</button>
+ </div>
+ </div>
+ `);
+
+ expect(snapshot).toContain('button "Add album"');
+ expect(snapshot).not.toContain('Enter admin password');
+ expect(snapshot).not.toContain('Verify and continue');
+ });
+
+ it('leaves out aria-hidden and inert subtrees', () => {
+ const snapshot = snapshotOf(`
+ <div aria-hidden="true"><button>Hidden by aria</button></div>
+ <div inert><button>Hidden by inert</button></div>
+ <button>Visible</button>
+ `);
+
+ expect(snapshot).toBe('- button "Visible" [ref=e1]');
+ });
+
+ it('keeps a transparent native checkbox styled by its label', () => {
+ const snapshot = snapshotOf(`
+ <label><input type="checkbox" aria-label="Remember me" style="opacity: 0" /> Remember me</label>
+ `);
+
+ expect(snapshot).toContain('checkbox "Remember me"');
+ });
+
+ it('lists the dialog once it is shown', () => {
+ const snapshot = snapshotOf(`
+ <div class="modal" style="opacity: 1">
+ <div role="dialog" aria-label="Enter admin password"><button>Verify and continue</button></div>
+ </div>
+ `);
+
+ expect(snapshot).toContain('dialog "Enter admin password');
+ expect(snapshot).toContain('button "Verify and continue"');
+ });
+});