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(heading-order): use aria-level on headings in addition to role=header elements #3028

Merged
merged 8 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/checks/navigation/heading-order-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { isVisible } from '../../commons/dom';

function getLevel(vNode) {
const role = vNode.attr('role');
if (role && role.includes('heading')) {
const ariaHeadingLevel = vNode.attr('aria-level');
const level = parseInt(ariaHeadingLevel, 10);

// default aria-level for a heading is 2 if it is
// not set or set to an incorrect value
// @see https://www.w3.org/TR/wai-aria-1.1/#heading
if (isNaN(level) || level < 1 || level > 6) {
return 2;
}
const headingRole = role && role.includes('heading');
const ariaHeadingLevel = vNode.attr('aria-level');
const level = parseInt(ariaHeadingLevel, 10);

// default aria-level for a heading is 2 if it is
// not set or set to an incorrect value
// @see https://www.w3.org/TR/wai-aria-1.1/#heading
if ((headingRole && isNaN(level)) || level < 1 || level > 6) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to fail on <h1 role="heading">. Not sure if that's a new problem, but it's a problem.

return 2;
}

if (level) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will return the aria-level for non-header elements like <iframe aria-level="2"></iframe>, when we actually need the iframe to be level=-1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule doesn't run on iframes, as far as I can tell.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule doesn't, but we use a selector inside the evaluate to look for iframes and assign them a level of -1 so in the after we can reconstruct heading order correctly for all iframes on the page.

const selector = 'h1, h2, h3, h4, h5, h6, [role=heading], iframe, frame';
// TODO: es-modules_tree
const vNodes = querySelectorAllFilter(axe._tree[0], selector, vNode =>
isVisible(vNode.actualNode, true)
);
headingOrder = vNodes.map(vNode => {

return level;
}

Expand Down
23 changes: 23 additions & 0 deletions test/checks/navigation/heading-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ describe('heading-order', function() {
});
});

it('should allow aria-level to override semantic level for hn tags and return true', function() {
var vNode = queryFixture(
'<h1 aria-level="2" id="target">Two</h1><h3 aria-level="4">Four</h3>'
);
assert.isTrue(
axe.testUtils
.getCheckEvaluate('heading-order')
.call(checkContext, null, {}, vNode, {})
);
assert.deepEqual(checkContext._data, {
headingOrder: [
{
ancestry: ['html > body > div:nth-child(1) > h1:nth-child(1)'],
level: 2
},
{
ancestry: ['html > body > div:nth-child(1) > h3:nth-child(2)'],
level: 4
}
]
});
});

it('should store the location of iframes', function() {
var vNode = queryFixture(
'<h1 id="target">One</h1><iframe></iframe><h3>Three</h3>'
Expand Down