Skip to content

Commit

Permalink
Fix defect in calling code when building the ElementRoleMap
Browse files Browse the repository at this point in the history
As discovered in #554

Address the defect introduced f7f6120#r143856081

This change depends on #558 which when applied (as in this PR makes the teset cases pass)
  • Loading branch information
samccone authored and ljharb committed Jul 7, 2024
1 parent db69abf commit 75cf2bc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion __tests__/src/elementRoleMap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe('elementRolesMap', function () {
});
describe('spread operator', function () {
it('should have a specific length', function () {
expect([...elementRoleMap].length).toEqual(113);
expect([...elementRoleMap].length).toEqual(112);
});
test.each([...elementRoleMap])('Testing element: %o', (obj, roles) => {
expect(entriesList).toEqual(
Expand Down
54 changes: 53 additions & 1 deletion src/elementRoleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
}
Expand Down Expand Up @@ -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<ARIARoleRelationConceptAttribute>,
b?: Array<ARIARoleRelationConceptAttribute>,
Expand Down

0 comments on commit 75cf2bc

Please sign in to comment.