Skip to content

Commit

Permalink
chore(region): work with standards object (#2380)
Browse files Browse the repository at this point in the history
  • Loading branch information
straker authored Jul 15, 2020
1 parent c496ad6 commit fce023c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
28 changes: 6 additions & 22 deletions lib/checks/navigation/region-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,37 @@
import * as dom from '../../commons/dom';
import * as aria from '../../commons/aria';
import * as standards from '../../commons/standards';
import * as text from '../../commons/text';
import matches from '../../commons/matches';
import { matchesSelector } from '../../core/utils';
import cache from '../../core/base/cache';

const landmarkRoles = standards.getAriaRolesByType('landmark');
const implicitAriaLiveRoles = ['alert', 'log', 'status'];

// Create a list of nodeNames that have a landmark as an implicit role
const implicitLandmarks = landmarkRoles
.reduce((arr, role) => arr.concat(aria.implicitNodes(role)), [])
.filter(r => r !== null);

// Check if the current element is a landmark
function isRegion(virtualNode, options) {
const node = virtualNode.actualNode;
const explicitRole = aria.getExplicitRole(node);
const role = aria.getRole(virtualNode);
const ariaLive = (node.getAttribute('aria-live') || '').toLowerCase().trim();

// Ignore content inside of aria-live
if (
['assertive', 'polite'].includes(ariaLive) ||
implicitAriaLiveRoles.includes(explicitRole)
implicitAriaLiveRoles.includes(role)
) {
return true;
}

if (explicitRole) {
return explicitRole === 'dialog' || landmarkRoles.includes(explicitRole);
// Check if the node matches a landmark role
if (landmarkRoles.includes(role)) {
return true;
}

// Check if node matches an option
if (options.regionMatcher && matches(virtualNode, options.regionMatcher)) {
return true;
}

// Check if the node matches any of the CSS selectors of implicit landmarks
return implicitLandmarks.some(implicitSelector => {
let matches = matchesSelector(node, implicitSelector);
if (node.nodeName.toUpperCase() === 'FORM') {
let titleAttr = node.getAttribute('title');
let title =
titleAttr && titleAttr.trim() !== '' ? text.sanitize(titleAttr) : null;
return matches && (!!aria.labelVirtual(virtualNode) || !!title);
}
return matches;
});
return false;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions test/integration/full/region/region-fail.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ <h1 id="mainheader" tabindex="0">This is a header.</h1>
<section>
<p>Content</p>
</section>
<section title="section 3">
<p>Content</p>
</section>
</div>

This should be ignored
Expand Down
3 changes: 0 additions & 3 deletions test/integration/full/region/region-pass.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ <h1 id="hdng">Section heading</h1>
<section aria-label="section 2">
<p>Content</p>
</section>
<section title="section 3">
<p>Content</p>
</section>
<div id="mocha" role="complementary"></div>
<script src="region-pass.js"></script>
<script src="/test/integration/adapter.js"></script>
Expand Down

0 comments on commit fce023c

Please sign in to comment.