Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(region): return outermost regionless node instead of html #1980

Merged
merged 9 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/checks/navigation/region-after.js

This file was deleted.

37 changes: 34 additions & 3 deletions lib/checks/navigation/region.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const { dom, aria } = axe.commons;
const landmarkRoles = aria.getRolesByType('landmark');

let regionlessNodes = axe._cache.get('regionlessNodes');
if (regionlessNodes) {
return !regionlessNodes.includes(node);
}

// Create a list of nodeNames that have a landmark as an implicit role
const implicitLandmarks = landmarkRoles
.reduce((arr, role) => arr.concat(aria.implicitNodes(role)), [])
Expand Down Expand Up @@ -47,6 +52,13 @@ function findRegionlessElms(virtualNode) {
dom.getElementByReference(virtualNode.actualNode, 'href')) ||
!dom.isVisible(node, true)
) {
// Mark each parent node as having region descendant
let vNode = axe.utils.getNodeFromTree(node);
straker marked this conversation as resolved.
Show resolved Hide resolved
while (vNode) {
vNode._hasRegionDescendant = true;
vNode = vNode.parent;
}

return [];

// Return the node is a content element
Expand All @@ -62,7 +74,26 @@ function findRegionlessElms(virtualNode) {
}
}

var regionlessNodes = findRegionlessElms(virtualNode);
this.relatedNodes(regionlessNodes);
regionlessNodes = findRegionlessElms(virtualNode)
// Find first parent marked as having region descendant (or body) and
// return the node right before it as the "outer" element
.map(node => {
let vNode = axe.utils.getNodeFromTree(node);
straker marked this conversation as resolved.
Show resolved Hide resolved
while (
vNode.parent &&
!vNode.parent._hasRegionDescendant &&
vNode.parent.actualNode !== document.body
) {
vNode = vNode.parent;
}

return vNode.actualNode;
})
// Remove duplicate containers
.filter((node, index, array) => {
return array.indexOf(node) === index;
});
this.data(regionlessNodes);
straker marked this conversation as resolved.
Show resolved Hide resolved
axe._cache.set('regionlessNodes', regionlessNodes);

return regionlessNodes.length === 0;
return !regionlessNodes.includes(node);
1 change: 0 additions & 1 deletion lib/checks/navigation/region.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"id": "region",
"evaluate": "region.js",
"after": "region-after.js",
"metadata": {
"impact": "moderate",
"messages": {
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/region.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"id": "region",
"selector": "html",
"pageLevel": true,
"selector": "body *",
"tags": ["cat.keyboard", "best-practice"],
"metadata": {
"description": "Ensures all page content is contained by landmarks",
Expand Down