Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/injected/src/ariaSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export function generateAriaTree(rootElement: Element, publicOptions: AriaTreeOp
if (node.nodeType === Node.TEXT_NODE && node.nodeValue) {
if (!parentElementVisible)
return;
// Text nodes that are not rendered are neither visible nor exposed to aria,
// so they are skipped regardless of the visibility option.
if (roleUtils.isTextNodeNotRendered(node as Text))
return;

const text = node.nodeValue;
// <textarea>AAA</textarea> should not report AAA as a child of the textarea.
Expand Down
35 changes: 29 additions & 6 deletions packages/injected/src/roleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,23 @@ function belongsToDisplayNoneOrAriaHiddenOrNonSlotted(element: Element): boolean
return hidden;
}

// Text nodes are usually hidden together with their parent element, with the exception of
// a few cases where the parent is rendered, but the text node itself is not.
// Such text nodes are neither visible nor exposed to aria.
export function isTextNodeNotRendered(text: Text): boolean {
const parent = text.parentElement;
if (!parent)
return false;
if (parent.shadowRoot && !text.assignedSlot)
return true;
// Closed <details> only renders the <summary> element.
// It is actually the same shadowRoot scenario as above, but user-agent shadow roots
// are not acessible to JavaScript, so we manually check only the most common scenario.
if (elementSafeTagName(parent) === 'DETAILS' && !(parent as HTMLDetailsElement).open)
return true;
return false;
}

function getIdRefs(element: Element, ref: string | null): Element[] {
if (!ref)
return [];
Expand Down Expand Up @@ -697,13 +714,8 @@ function getTextAlternativeInternal(element: Element, options: AccessibleNameOpt
// Not part of an aria-labelledby or aria-describedby traversal, where the node directly referenced by that relation was hidden.
// Nor part of a native host language text alternative element (e.g. label in HTML) or attribute traversal, where the root of that traversal was hidden.
if (!options.includeHidden) {
const isEmbeddedInHiddenReferenceTraversal =
!!options.embeddedInLabelledBy?.hidden ||
!!options.embeddedInDescribedBy?.hidden ||
!!options.embeddedInNativeTextAlternative?.hidden ||
!!options.embeddedInLabel?.hidden;
if (isElementIgnoredForAria(element) ||
(!isEmbeddedInHiddenReferenceTraversal && isElementHiddenForAria(element))) {
(!isEmbeddedInHiddenReferenceTraversal(options) && isElementHiddenForAria(element))) {
options.visitedElements.add(element);
return emptyCompositeString();
}
Expand Down Expand Up @@ -1015,8 +1027,17 @@ function getTextAlternativeInternal(element: Element, options: AccessibleNameOpt
return emptyCompositeString();
}

function isEmbeddedInHiddenReferenceTraversal(options: AccessibleNameOptions): boolean {
return !!options.embeddedInLabelledBy?.hidden ||
!!options.embeddedInDescribedBy?.hidden ||
!!options.embeddedInNativeTextAlternative?.hidden ||
!!options.embeddedInLabel?.hidden;
}

function innerAccumulatedElementText(element: Element, options: AccessibleNameOptions): CompositeString {
const tokens: string[] = [];
// Similarly to step 2a, text nodes that are not rendered are skipped unless explicitly referenced.
const skipNotRenderedText = !options.includeHidden && !isEmbeddedInHiddenReferenceTraversal(options);
const elements = options.collectElements ? new Set<Element>() : undefined;
const visit = (node: Node, skipSlotted: boolean) => {
if (skipSlotted && (node as Element | Text).assignedSlot)
Expand All @@ -1035,6 +1056,8 @@ function innerAccumulatedElementText(element: Element, options: AccessibleNameOp
token = ' ' + token + ' ';
tokens.push(token);
} else if (node.nodeType === 3 /* Node.TEXT_NODE */) {
if (skipNotRenderedText && isTextNodeNotRendered(node as Text))
return;
// step 2g.
tokens.push(node.textContent || '');
}
Expand Down
26 changes: 26 additions & 0 deletions tests/library/role-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,32 @@ test('accessible name with slots', async ({ page }) => {
</script>
`);
expect.soft(await getNameAndRole(page, 'button')).toEqual({ role: 'button', name: 'pre' });

// Text "foo" is not assigned to any slot, should not be used.
await page.setContent(`
<div role=button>foo</div>
<script>
(() => {
const container = document.querySelector('div');
const shadow = container.attachShadow({ mode: 'open' });
shadow.appendChild(document.createTextNode('bar'));
})();
</script>
`);
expect.soft(await getNameAndRole(page, 'div')).toEqual({ role: 'button', name: 'bar' });
});

test('accessible name with closed details', async ({ page }) => {
await page.setContent(`
<button aria-labelledby="details"></button>
<details id="details">
<summary>Summary</summary>
Details
</details>
`);
expect.soft(await getNameAndRole(page, 'button')).toEqual({ role: 'button', name: 'Summary' });
await page.$eval('details', details => details.open = true);
expect.soft(await getNameAndRole(page, 'button')).toEqual({ role: 'button', name: 'Summary Details' });
});

test('accessible name nested treeitem', async ({ page }) => {
Expand Down
37 changes: 37 additions & 0 deletions tests/page/page-aria-snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,43 @@ it('should snapshot details visibility', async ({ page }) => {
`);
});

it('should not snapshot text directly inside closed details', async ({ page }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43019' });

await page.setContent(`
<details>
<summary>Summary</summary>
Details
</details>
`);

await checkAndMatchSnapshot(page.locator('body'), `
- group: Summary
`);

await page.$eval('details', details => details.open = true);
await checkAndMatchSnapshot(page.locator('body'), `
- group: Summary Details
`);
});

it('should not snapshot text that is not assigned to a slot', async ({ page }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43019' });

await page.setContent(`
<div role=button>Light</div>
<script>
document.querySelector('div').attachShadow({ mode: 'open' }).innerHTML = '<p>Shadow</p>';
</script>
`);

await checkAndMatchSnapshot(page.locator('body'), `
- button "Shadow":
- paragraph: Shadow
`);
await expect(page.getByRole('button', { name: 'Shadow', exact: true })).toBeVisible();
});

it('should snapshot integration', async ({ page }) => {
await page.setContent(`
<h1>Microsoft</h1>
Expand Down