Skip to content

Commit 037e621

Browse files
samcconeljharb
authored andcommitted
Fix defect in calling code when building the ElementRoleMap
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)
1 parent db69abf commit 037e621

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

__tests__/src/elementRoleMap-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ describe('elementRolesMap', function () {
230230
});
231231
describe('spread operator', function () {
232232
it('should have a specific length', function () {
233-
expect([...elementRoleMap].length).toEqual(113);
233+
expect([...elementRoleMap].length).toEqual(112);
234234
});
235235
test.each([...elementRoleMap])('Testing element: %o', (obj, roles) => {
236236
expect(entriesList).toEqual(

src/elementRoleMap.js

+53-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,27 @@ for (let i = 0; i < keys.length; i++) {
2323
if (relation.module === 'HTML') {
2424
const concept = relation.concept;
2525
if (concept) {
26-
elementRoles.push([concept, [key]]);
26+
const elementRoleRelation: ?ElementARIARoleRelationTuple = elementRoles.find(relation => ariaRoleRelationConceptEquals(relation[0], concept));
27+
let roles: RoleSet;
28+
29+
if (elementRoleRelation) {
30+
roles = elementRoleRelation[1];
31+
} else {
32+
roles = [];
33+
}
34+
let isUnique = true;
35+
for (let i = 0; i < roles.length; i++) {
36+
if (roles[i] === key) {
37+
isUnique = false;
38+
break;
39+
}
40+
}
41+
if (isUnique) {
42+
roles.push(key);
43+
}
44+
if (!elementRoleRelation) {
45+
elementRoles.push([concept, roles]);
46+
}
2747
}
2848
}
2949
}
@@ -63,6 +83,38 @@ const elementRoleMap: TAriaQueryMap<
6383
},
6484
};
6585

86+
function ariaRoleRelationConceptEquals(a: ARIARoleRelationConcept, b: ARIARoleRelationConcept): boolean {
87+
return (
88+
a.name === b.name &&
89+
ariaRoleRelationConstraintsEquals(a.constraints, b.constraints) &&
90+
ariaRoleRelationConceptAttributeEquals(a.attributes, b.attributes)
91+
)
92+
}
93+
94+
function ariaRoleRelationConstraintsEquals(a?: ARIARoleRelationConcept['constraints'], b?: ARIARoleRelationConcept['constraints']): boolean {
95+
if (a === undefined && b !== undefined) {
96+
return false;
97+
}
98+
99+
if (a !== undefined && b === undefined) {
100+
return false;
101+
}
102+
103+
if (a !== undefined && b !== undefined) {
104+
if (a.length !== b.length) {
105+
return false;
106+
}
107+
108+
for (let i = 0; i < a.length; i++) {
109+
if (a[i] !== b[i]) {
110+
return false;
111+
}
112+
}
113+
}
114+
115+
return true;
116+
}
117+
66118
function ariaRoleRelationConceptAttributeEquals(
67119
a?: Array<ARIARoleRelationConceptAttribute>,
68120
b?: Array<ARIARoleRelationConceptAttribute>,

0 commit comments

Comments
 (0)