Skip to content

Commit

Permalink
Normalize whitespace in getAccessibilityTree element accessible names (
Browse files Browse the repository at this point in the history
  • Loading branch information
calebeby authored Aug 30, 2022
1 parent d9be17d commit b565e0b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/healthy-forks-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pleasantest': major
---

**Normalize whitespace in element accessible names in `getAccessibilityTree`**. Markup with elements which have an accessible name that includes irregular whitespace, like non-breaking spaces, will now have a different output for `getAccessibilityTree` snapshots. Previously, the whitespace was included, now, whitespace is replaced with a single space.
2 changes: 1 addition & 1 deletion src/accessibility/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const getAccessibilityTree = (
);
let text = (selfIsInAccessibilityTree && role) || '';
if (selfIsInAccessibilityTree) {
let name = computeAccessibleName(element);
let name = computeAccessibleName(element).replace(/\s+/g, ' ');
if (
element === document.documentElement &&
role === 'document' &&
Expand Down
15 changes: 15 additions & 0 deletions tests/accessibility/getAccessibilityTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ test(
}),
);

test(
'Whitespace is normalized in element names',
withBrowser(async ({ utils, page }) => {
// https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
await utils.injectHTML(
`<button>Between these words is a ->\u00A0<- nbsp</button>`,
);
// The nbsp is normalized to a space so in the snapshot it is just a space
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
document "pleasantest"
button "Between these words is a -> <- nbsp"
`);
}),
);

test(
'hidden elements are excluded',
withBrowser(async ({ utils, page }) => {
Expand Down

0 comments on commit b565e0b

Please sign in to comment.