Skip to content

Commit

Permalink
fix(aria-required-parent): allow nested group and presentational roles (
Browse files Browse the repository at this point in the history
#3492)

* fix(aria-required-parent): allow nested group and presentational roles

* update

* remove comments
  • Loading branch information
straker committed Jun 3, 2022
1 parent 2674841 commit 4685270
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
25 changes: 12 additions & 13 deletions lib/checks/aria/aria-required-parent-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,30 @@ function getMissingContext(
return null;
}

const allowsGroup = reqContext.includes('group');
let vNode = includeElement ? virtualNode : virtualNode.parent;

while (vNode) {
const parentRole = getRole(vNode);
const role = getRole(vNode, { noPresentational: true });

// if parent node has role=group and role=group is an allowed
// context, check next parent
if (reqContext.includes('group') && parentRole === 'group') {
// if parent node has no role or is presentational, or if role
// allows group, we keep parsing the parent tree.
// this means intermediate roles between a required parent and
// child will fail the check
if (!role) {
vNode = vNode.parent;
} else if (role === 'group' && allowsGroup) {
// Allow the own role; i.e. tree > treeitem > group > treeitem
if (ownGroupRoles.includes(explicitRole)) {
reqContext.push(explicitRole);
}
reqContext = reqContext.filter(r => r !== 'group');
vNode = vNode.parent;
continue;
}

// if parent node has a role that is not the required role and not
// presentational we will fail the check
if (reqContext.includes(parentRole)) {
} else if (reqContext.includes(role)) {
return null;
} else if (parentRole && !['presentation', 'none'].includes(parentRole)) {
} else {
return reqContext;
}

vNode = vNode.parent;
}

return reqContext;
Expand Down
11 changes: 11 additions & 0 deletions test/checks/aria/required-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ describe('aria-required-parent', function() {
);
});

it('should pass for multiple group and presentational roles', function() {
var params = checkSetup(
'<div role="list"><div role="none"><div role="group"><div role="none"><div role="group"><div role="listitem" id="target">Nothing here.</div></div></div></div></div></div>'
);
assert.isTrue(
axe.testUtils
.getCheckEvaluate('aria-required-parent')
.apply(checkContext, params)
);
});

(shadowSupported ? it : xit)(
'should pass when required parent is present across shadow boundary',
function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@
<div role="option" id="pass13">option</div>
</div>
</div>

<div role="tree">
<ul role="group">
<li role="none">
<ul role="group">
<li role="none">
<div role="treeitem" id="pass14">tree item</div>
</li>
</ul>
</li>
</ul>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
["#pass10"],
["#pass11"],
["#pass12"],
["#pass13"]
["#pass13"],
["#pass14"]
]
}

0 comments on commit 4685270

Please sign in to comment.