From b565e0b64e535337ea5ac53c0c17fa7227861f0f Mon Sep 17 00:00:00 2001 From: Caleb Eby Date: Tue, 30 Aug 2022 10:24:07 -0700 Subject: [PATCH] Normalize whitespace in getAccessibilityTree element accessible names (#561) --- .changeset/healthy-forks-share.md | 5 +++++ src/accessibility/browser.ts | 2 +- tests/accessibility/getAccessibilityTree.test.ts | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 .changeset/healthy-forks-share.md diff --git a/.changeset/healthy-forks-share.md b/.changeset/healthy-forks-share.md new file mode 100644 index 00000000..780ed73f --- /dev/null +++ b/.changeset/healthy-forks-share.md @@ -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. diff --git a/src/accessibility/browser.ts b/src/accessibility/browser.ts index 72e82b1f..f49077c9 100644 --- a/src/accessibility/browser.ts +++ b/src/accessibility/browser.ts @@ -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' && diff --git a/tests/accessibility/getAccessibilityTree.test.ts b/tests/accessibility/getAccessibilityTree.test.ts index 2bb1b19f..364520b7 100644 --- a/tests/accessibility/getAccessibilityTree.test.ts +++ b/tests/accessibility/getAccessibilityTree.test.ts @@ -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( + ``, + ); + // 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 }) => {