Skip to content

Commit b565e0b

Browse files
authored
Normalize whitespace in getAccessibilityTree element accessible names (#561)
1 parent d9be17d commit b565e0b

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

.changeset/healthy-forks-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pleasantest': major
3+
---
4+
5+
**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.

src/accessibility/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export const getAccessibilityTree = (
112112
);
113113
let text = (selfIsInAccessibilityTree && role) || '';
114114
if (selfIsInAccessibilityTree) {
115-
let name = computeAccessibleName(element);
115+
let name = computeAccessibleName(element).replace(/\s+/g, ' ');
116116
if (
117117
element === document.documentElement &&
118118
role === 'document' &&

tests/accessibility/getAccessibilityTree.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ test(
137137
}),
138138
);
139139

140+
test(
141+
'Whitespace is normalized in element names',
142+
withBrowser(async ({ utils, page }) => {
143+
// https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
144+
await utils.injectHTML(
145+
`<button>Between these words is a ->\u00A0<- nbsp</button>`,
146+
);
147+
// The nbsp is normalized to a space so in the snapshot it is just a space
148+
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
149+
document "pleasantest"
150+
button "Between these words is a -> <- nbsp"
151+
`);
152+
}),
153+
);
154+
140155
test(
141156
'hidden elements are excluded',
142157
withBrowser(async ({ utils, page }) => {

0 commit comments

Comments
 (0)