From d5024d25b6843ef03956288ea66589cccabbc654 Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Sun, 7 Jul 2024 13:46:16 -0700 Subject: [PATCH] Fix defect in calling code when building the ElementRoleMap As discovered in #554 Address the defect introduced f7f6120#r143856081 This change depends on https://github.com/A11yance/aria-query/pull/558 which when applied (as in this PR makes the teset cases pass) --- src/elementRoleMap.js | 54 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/elementRoleMap.js b/src/elementRoleMap.js index 0d016a4..eeeff70 100644 --- a/src/elementRoleMap.js +++ b/src/elementRoleMap.js @@ -23,7 +23,27 @@ for (let i = 0; i < keys.length; i++) { if (relation.module === 'HTML') { const concept = relation.concept; if (concept) { - elementRoles.push([concept, [key]]); + const elementRoleRelation: ?ElementARIARoleRelationTuple = elementRoles.find(relation => ariaRoleRelationConceptEquals(relation[0], concept)); + let roles: RoleSet; + + if (elementRoleRelation) { + roles = elementRoleRelation[1]; + } else { + roles = []; + } + let isUnique = true; + for (let i = 0; i < roles.length; i++) { + if (roles[i] === key) { + isUnique = false; + break; + } + } + if (isUnique) { + roles.push(key); + } + if (!elementRoleRelation) { + elementRoles.push([concept, roles]); + } } } } @@ -63,6 +83,38 @@ const elementRoleMap: TAriaQueryMap< }, }; +function ariaRoleRelationConceptEquals(a: ARIARoleRelationConcept, b: ARIARoleRelationConcept): boolean { + return ( + a.name === b.name && + ariaRoleRelationConstraintsEquals(a.constraints, b.constraints) && + ariaRoleRelationConceptAttributeEquals(a.attributes, b.attributes) + ) +} + +function ariaRoleRelationConstraintsEquals(a?: ARIARoleRelationConcept['constraints'], b?: ARIARoleRelationConcept['constraints']): boolean { + if (a === undefined && b !== undefined) { + return false; + } + + if (a !== undefined && b === undefined) { + return false; + } + + if (a !== undefined && b !== undefined) { + if (a.length != b.length) { + return false; + } + + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) { + return false; + } + } + } + + return true; +} + function ariaRoleRelationConceptAttributeEquals( a?: Array, b?: Array,