Skip to content

fix(aria): process children of hidden elements #36316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 15 additions & 4 deletions packages/injected/src/ariaSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ type AriaRef = {

let lastRef = 0;

function isVisible(element: Element, options?: { forAI?: boolean }): boolean {
if (!roleUtils.isElementHiddenForAria(element))
return true;
if (options?.forAI && isElementVisible(element))
return true;
return false;
}

export function generateAriaTree(rootElement: Element, options?: { forAI?: boolean, refPrefix?: string }): AriaSnapshot {
const visited = new Set<Node>();

Expand All @@ -61,6 +69,9 @@ export function generateAriaTree(rootElement: Element, options?: { forAI?: boole
visited.add(node);

if (node.nodeType === Node.TEXT_NODE && node.nodeValue) {
if (node.parentElement && !isVisible(node.parentElement, options))
return;

const text = node.nodeValue;
// <textarea>AAA</textarea> should not report AAA as a child of the textarea.
if (ariaNode.role !== 'textbox' && text)
Expand All @@ -72,11 +83,11 @@ export function generateAriaTree(rootElement: Element, options?: { forAI?: boole
return;

const element = node as Element;
let isVisible = !roleUtils.isElementHiddenForAria(element);
if (options?.forAI)
isVisible = isVisible || isElementVisible(element);
if (!isVisible)
if (!isVisible(element, options)) {
// skip this element, but still process its children: https://github.com/w3c/aria/issues/1055
processElement(ariaNode, element, []);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how this makes sense. Also, you can't process children as if they are visible. And we probably only want this for non-aria visibility.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how this makes sense

How so? As in, you're surprised how Chromium and this issue interpret the ARIA spec, or as in this code is weird?

Also, you can't process children as if they are visible.

I think we can, because the isVisible method takes parent elements into account when determining visibility.

we probably only want this for non-aria visibility.

It already does so, based on my reading of isElementHiddenForAria. I added a test in 9775c4a to ensure that.

return;
}

const ariaChildren: Element[] = [];
if (element.hasAttribute('aria-owns')) {
Expand Down
26 changes: 26 additions & 0 deletions tests/page/page-aria-snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,29 @@ it('should not report textarea textContent', async ({ page }) => {
- textbox: After
`);
});

it('should show visible children of hidden elements', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36296' } }, async ({ page }) => {
await page.setContent(`
<div style="visibility: hidden;">
<div style="visibility: visible;">
<button>Button</button>
</div>
</div>
`);

await checkAndMatchSnapshot(page.locator('body'), `
- button "Button"
`);
});

it('should not show unhidden children of aria-hidden elements', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36296' } }, async ({ page }) => {
await page.setContent(`
<div aria-hidden="true">
<div aria-hidden="false">
<button>Button</button>
</div>
</div>
`);

expect(await page.locator('body').ariaSnapshot()).toBe('');
});
Loading