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 6 commits
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
47 changes: 31 additions & 16 deletions lib/checks/navigation/heading-order-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import cache from '../../core/base/cache';
import { querySelectorAllFilter, getAncestry } from '../../core/utils';
import { isVisible } from '../../commons/dom';
import { getRole } from '../../commons/aria';

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 role = getRole(vNode);
const headingRole = role && role.includes('heading');
const ariaHeadingLevel = vNode.attr('aria-level');
const ariaLevel = parseInt(ariaHeadingLevel, 10);

const [, headingLevel] = vNode.props.nodeName.match(/h(\d)/) || [];

if (!headingRole) {
return -1;
}

return level;
if (headingLevel && !ariaHeadingLevel) {
return parseInt(headingLevel, 10);
}

const headingLevel = vNode.props.nodeName.match(/h(\d)/);
if (headingLevel) {
return parseInt(headingLevel[1], 10);
/*
* default aria-level for a role=heading is 2 if it is
* not set or set to an incorrect value.
* default aria-level for a heading element is the
* heading level.
* note that NVDA and VO allow any positive level
* @see https://www.w3.org/TR/wai-aria-1.1/#heading
* @see https://codepen.io/straker/pen/jOBjNNe
*/
if (isNaN(ariaLevel) || ariaLevel < 1 || ariaLevel > 6) {
Copy link
Contributor

@straker straker Jun 25, 2021

Choose a reason for hiding this comment

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

Alright. Wilco and I talked and we came to the conclusion that we want to change how we're handling positive values. Since aria-level > 6 is handled differently by different screen readers, we're going to flag it as part of a new check for aria-valid-attr-value and call out its lack of support.

But for heading-order we're just going to accept what the user gave us so there isn't two different failures caused by the same problem.

Suggested change
if (isNaN(ariaLevel) || ariaLevel < 1 || ariaLevel > 6) {
if (isNaN(ariaLevel) || ariaLevel < 1) {

if (headingLevel) {
return parseInt(headingLevel, 10);
}
return 2;
}

if (ariaLevel) {
return ariaLevel;
}

return -1;
Expand Down Expand Up @@ -49,7 +65,6 @@ function headingOrderEvaluate() {
level: getLevel(vNode)
};
});

this.data({ headingOrder });
cache.set('headingOrder', vNodes);
return true;
Expand Down
84 changes: 84 additions & 0 deletions test/checks/navigation/heading-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,90 @@ 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 ignore aria-level on iframe when not used with role=heading', function() {
var vNode = queryFixture('<iframe aria-level="2"></iframe>');
axe.testUtils
.getCheckEvaluate('heading-order')
.call(checkContext, null, {}, vNode, { initiator: true });
assert.deepEqual(checkContext._data, {
headingOrder: [
{
ancestry: ['html > body > div:nth-child(1) > iframe'],
level: -1
}
]
});
});

it('should correctly give level on hn tag with role=heading', function() {
var vNode = queryFixture(
'<h1 role="heading" id="target">One</h1><h3 role="heading">Three</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: 1
},
{
ancestry: ['html > body > div:nth-child(1) > h3:nth-child(2)'],
level: 3
}
]
});
});

it('should return the heading level when an hn tag has an invalid aria-level', function() {
var vNode = queryFixture(
'<h1 aria-level="-1" id="target">One</h1><h3 aria-level="12">Three</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: 1
},
{
ancestry: ['html > body > div:nth-child(1) > h3:nth-child(2)'],
level: 3
}
]
});
});

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