fix(web): fold content under a closed details across shadow roots

isInClosedDetails walked parentElement and closest('details'), which
never cross a shadow boundary or follow a slot. Content a closed
<details> folds away keeps a box on Chromium and Firefox, so such content
read hidden: false and toBeVisible() passed on a folded button:

- a light child slotted into a <details> of its host's shadow root
- anything in the shadow root of a host that sits under a <details>

It now walks the flat tree, as isSkippedContent does: content is folded
when a closed <details> is on the way up and the node on that path under
it is not its first summary. The walk is memoized like its sibling.

Tests (visibility.test.ts): both shapes, open and closed shadow roots, a
plain button and a display: contents one, each with the <details> closed
and open, read three ways: states.hidden, a role query, an observation.
A button slotted into the summary of a closed <details> stays shown.

Before the fix all eight cases failed on Chromium: hidden read false, a
slotted button was listed in the observation, and a display: contents
button matched the role query. On Firefox the four plain-button cases
failed on hidden; the display: contents ones passed there already, since
Firefox lays out no box for folded text. The summary cases passed before
and after.
This commit is contained in:
Oskar Kwaśniewski
2026-11-17 13:01:58 +01:00
parent 084c3b0090
commit f57eeb7e36
2 changed files with 66 additions and 16 deletions
+13 -15
View File
@@ -227,22 +227,20 @@ export const createSemanticReader = (
};
/**
* True for content a closed `<details>` folds away: anything under it that
* is not in its `<summary>`. Every closed ancestor is asked, so a summary's
* own controls stay visible while the closed body's do not, nested
* `<details>` and their summaries included.
* True for content a closed `<details>` folds away: anything under it in
* the flat tree that is not in its first `<summary>` child, so what is
* slotted into a `<details>` of a shadow root folds with it, and so does
* the shadow tree of a host under one. Every closed `<details>` on the way
* up is asked, so a summary's own controls stay visible while the closed
* body's do not, nested `<details>` and their summaries included.
* Memoized, so siblings under one ancestor share the walk up.
*/
const isInClosedDetails = (el: Element): boolean => {
let details = el.parentElement?.closest('details') ?? null;
while (details !== null) {
if (!details.open) {
const summary = details.querySelector(':scope > summary');
if (summary === null || !summary.contains(el)) return true;
}
details = details.parentElement?.closest('details') ?? null;
}
return false;
};
const isInClosedDetails = memoized((el: Element): boolean => {
const parent = flatParentOf(el);
if (parent === null) return false;
if (parent instanceof HTMLDetailsElement && !parent.open && parent.querySelector(':scope > summary') !== el) return true;
return isInClosedDetails(parent);
});
/**
* True when an ancestor in the flat tree has `content-visibility: hidden`:
@@ -9,7 +9,7 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import type { SemanticNode } from '@e2e-dev/core/engine';
import { byRole, flatten, startWebSession, type WebSession } from '../helpers/web-session.ts';
import { byRole, byTestId, flatten, startWebSession, type WebSession } from '../helpers/web-session.ts';
let page: WebSession;
@@ -120,6 +120,58 @@ describe('a role query on a display: contents element', () => {
});
});
describe('content a closed <details> folds away across a shadow boundary', () => {
/** A page whose light child `target` is slotted into the body of a `<details>` in its host's shadow root. */
const slotted = (mode: string, open: string, target: string): string => `<fold-host>${target}</fold-host><script>
customElements.define('fold-host', class extends HTMLElement {
connectedCallback() { this.attachShadow({ mode: '${mode}' }).innerHTML = '<details${open}><summary>More</summary><slot></slot></details>'; }
});
</script>`;
/** A page whose `target` is in the shadow root of a host in the body of a `<details>`. */
const hosted = (mode: string, open: string, target: string): string => `<details${open}><summary>More</summary><fold-host></fold-host></details><script>
customElements.define('fold-host', class extends HTMLElement {
connectedCallback() { this.attachShadow({ mode: '${mode}' }).innerHTML = ${JSON.stringify(target)}; }
});
</script>`;
const BUTTON = '<button data-testid="target">Go</button>';
const CONTENTS = '<span data-testid="target" role="button" style="display:contents">Go</span>';
/** What the page says of its target: its `hidden` state, how many buttons a role query finds, and how many an observation lists. */
async function read(html: string): Promise<{ hidden: boolean; byRole: number; observed: number }> {
await page.setContent(html);
const [target] = await page.locate(byTestId('target'));
return {
hidden: target!.states?.hidden ?? false,
byRole: (await page.locate(byRole('button', 'Go'))).length,
observed: (await captureTree()).filter((node) => node.role === 'button' && node.name === 'Go').length,
};
}
it.each([
['a button slotted into the <details> of an open shadow root', slotted, 'open', BUTTON],
['a button slotted into the <details> of a closed shadow root', slotted, 'closed', BUTTON],
['a display: contents button slotted into the <details> of an open shadow root', slotted, 'open', CONTENTS],
['a display: contents button slotted into the <details> of a closed shadow root', slotted, 'closed', CONTENTS],
['a button in the open shadow root of a host under <details>', hosted, 'open', BUTTON],
['a button in the closed shadow root of a host under <details>', hosted, 'closed', BUTTON],
['a display: contents button in the open shadow root of a host under <details>', hosted, 'open', CONTENTS],
['a display: contents button in the closed shadow root of a host under <details>', hosted, 'closed', CONTENTS],
])('hides %s while the <details> is closed', async (_case, build, mode, target) => {
expect(await read(build(mode, '', target))).toEqual({ hidden: true, byRole: 0, observed: 0 });
expect(await read(build(mode, ' open', target))).toEqual({ hidden: false, byRole: 1, observed: 1 });
});
it.each(['open', 'closed'])('shows a button slotted into the summary of a closed <details> in a shadow root: %s', async (mode) => {
expect(await read(`<fold-host><button slot="label" data-testid="target">Go</button><p>Folded</p></fold-host><script>
customElements.define('fold-host', class extends HTMLElement {
connectedCallback() { this.attachShadow({ mode: '${mode}' }).innerHTML = '<details><summary><slot name="label"></slot></summary><slot></slot></details>'; }
});
</script>`)).toEqual({ hidden: false, byRole: 1, observed: 1 });
});
});
describe('the bounding box', () => {
/** The reader's rect for one element next to its laid-out box, null when it has no layout box. */
async function boxOf(selector: string) {