From b6d4525e96c6b766426486a1ff15838dc8481ae0 Mon Sep 17 00:00:00 2001 From: Caleb Eby Date: Mon, 29 Aug 2022 17:09:52 -0700 Subject: [PATCH 1/2] Normalize whitespace in getAccessibilityTree element accessible names --- .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..8178d5de --- /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 }) => { From c183adc62a1885a4d932275a4b5ba1b0d23d8bdd Mon Sep 17 00:00:00 2001 From: Caleb Eby Date: Mon, 29 Aug 2022 17:12:03 -0700 Subject: [PATCH 2/2] backticks --- .changeset/healthy-forks-share.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/healthy-forks-share.md b/.changeset/healthy-forks-share.md index 8178d5de..780ed73f 100644 --- a/.changeset/healthy-forks-share.md +++ b/.changeset/healthy-forks-share.md @@ -2,4 +2,4 @@ '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. +**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.