Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(aria): skip text nodes that are not rendered (#43150)

Dmitry Gozmane47f15639622
diff --git a/packages/injected/src/ariaSnapshot.ts b/packages/injected/src/ariaSnapshot.ts
index 42e3a0842c..61f96fe040 100644
--- a/packages/injected/src/ariaSnapshot.ts
+++ b/packages/injected/src/ariaSnapshot.ts
@@ -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.
diff --git a/packages/injected/src/roleUtils.ts b/packages/injected/src/roleUtils.ts
index 3b274129d3..d0e2359812 100644
--- a/packages/injected/src/roleUtils.ts
+++ b/packages/injected/src/roleUtils.ts
@@ -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 [];
@@ -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();
}
@@ -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)
@@ -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 || '');
}
diff --git a/tests/library/role-utils.spec.ts b/tests/library/role-utils.spec.ts
index a20e9329bc..2b23cd497f 100644
--- a/tests/library/role-utils.spec.ts
+++ b/tests/library/role-utils.spec.ts
@@ -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 }) => {
diff --git a/tests/page/page-aria-snapshot.spec.ts b/tests/page/page-aria-snapshot.spec.ts
index c3b42d136b..2644300a16 100644
--- a/tests/page/page-aria-snapshot.spec.ts
+++ b/tests/page/page-aria-snapshot.spec.ts
@@ -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>