Skip to content

Commit

Permalink
fix: handle spoken containers that don't have a role (due to alternat…
Browse files Browse the repository at this point in the history
…ive reading flow)
  • Loading branch information
jlp-craigmorten committed Jan 28, 2025
1 parent dd87384 commit acb9c25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/flattenTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type {
AccessibilityNodeTree,
} from "./createAccessibilityTree";
import { getAccessibleAttributeLabels } from "./getNodeAccessibilityData/getAccessibleAttributeLabels";
import { HTMLElementWithValue } from "./getNodeAccessibilityData/getAccessibleValue";
import type { HTMLElementWithValue } from "./getNodeAccessibilityData/getAccessibleValue";

export const END_OF_ROLE_PREFIX = "end of";
export const END_OF_NO_ROLE_PREFIX = "end";

const TEXT_NODE = 3;

Expand Down Expand Up @@ -65,17 +66,15 @@ export function flattenTree(

const flattenedTree = ignoreChildren
? []
: [
...children.flatMap((child) =>
flattenTree(container, child, {
...treeNodeWithAttributeLabels,
children,
})
),
];
: children.flatMap((child) =>
flattenTree(container, child, {
...treeNodeWithAttributeLabels,
children,
})
);

const isRoleContainer =
!!flattenedTree.length && !ignoreChildren && !!treeNode.spokenRole;
!!flattenedTree.length && !ignoreChildren && isAnnounced;

if (isAnnounced) {
flattenedTree.unshift(treeNodeWithAttributeLabels);
Expand All @@ -84,7 +83,9 @@ export function flattenTree(
if (isRoleContainer) {
flattenedTree.push({
...treeNodeWithAttributeLabels,
spokenRole: `${END_OF_ROLE_PREFIX} ${treeNodeWithAttributeLabels.spokenRole}`,
spokenRole: treeNodeWithAttributeLabels.spokenRole
? `${END_OF_ROLE_PREFIX} ${treeNodeWithAttributeLabels.spokenRole}`
: END_OF_NO_ROLE_PREFIX,
});
}

Expand Down
12 changes: 12 additions & 0 deletions test/int/ariaFlowTo.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@ describe("Aria Flow To", () => {
await virtual.next();
await virtual.next();
await virtual.next();
await virtual.next();
await virtual.next();
await virtual.perform(
virtual.commands.moveToNextAlternateReadingOrderElement
);
await virtual.next();

expect(await virtual.spokenPhraseLog()).toEqual([
"document",
"3 alternate reading orders, 1 previous alternate reading order",
"apple",
"end, 3 alternate reading orders, 1 previous alternate reading order",
"1 previous alternate reading order",
"banana",
"end, 1 previous alternate reading order",
"1 alternate reading order, 1 previous alternate reading order",
"3 alternate reading orders, 1 previous alternate reading order",
"apple",
]);

await virtual.stop();
Expand All @@ -53,6 +59,7 @@ describe("Aria Flow To", () => {
);
await virtual.previous();
await virtual.previous();
await virtual.previous();
await virtual.perform(
virtual.commands.moveToNextAlternateReadingOrderElement,
{ index: 1 }
Expand All @@ -61,6 +68,8 @@ describe("Aria Flow To", () => {
await virtual.previous();
await virtual.previous();
await virtual.previous();
await virtual.previous();
await virtual.previous();
await virtual.perform(
virtual.commands.moveToNextAlternateReadingOrderElement,
{ index: 2 }
Expand All @@ -71,11 +80,14 @@ describe("Aria Flow To", () => {
"document",
"3 alternate reading orders, 1 previous alternate reading order",
"1 previous alternate reading order",
"end, 3 alternate reading orders, 1 previous alternate reading order",
"apple",
"3 alternate reading orders, 1 previous alternate reading order",
"1 alternate reading order, 1 previous alternate reading order",
"end, 1 previous alternate reading order",
"banana",
"1 previous alternate reading order",
"end, 3 alternate reading orders, 1 previous alternate reading order",
"apple",
"3 alternate reading orders, 1 previous alternate reading order",
"1 previous alternate reading order",
Expand Down

0 comments on commit acb9c25

Please sign in to comment.