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

headings rules: Skip headings with a role change #722

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions lib/rules/empty-heading.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"id": "empty-heading",
"selector": "h1, h2, h3, h4, h5, h6, [role=\"heading\"]",
"matches": "heading-matches.js",
"tags": [
"cat.name-role-value",
"best-practice"
Expand All @@ -11,9 +12,7 @@
},
"all": [],
"any": [
"has-visible-text",
"role-presentation",
"role-none"
"has-visible-text"
],
"none": []
}
13 changes: 13 additions & 0 deletions lib/rules/heading-matches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Get all valid roles
let explicitRoles;
if (node.hasAttribute('role')) {
explicitRoles = node.getAttribute('role').split(/\s+/i)
.filter(axe.commons.aria.isValidRole);
Copy link
Contributor

Choose a reason for hiding this comment

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

The splitting and filtering here seems like a utility we could reuse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems pretty trivial. I would like us to at some point move towards a stronger getRoles() method, but I think that needs a little more thought, and we should probably have a better idea of how we do accessibility support before we can.

}

// Check valid roles if there are any, otherwise fall back to the inherited role
if (explicitRoles && explicitRoles.length > 0) {
return explicitRoles.includes('heading');
} else {
return axe.commons.aria.implicitRole(node) === 'heading';
}
1 change: 1 addition & 0 deletions lib/rules/heading-order.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"id": "heading-order",
"selector": "h1, h2, h3, h4, h5, h6, [role=heading]",
"matches": "heading-matches.js",
"tags": [
"cat.semantics",
"best-practice"
Expand Down
4 changes: 2 additions & 2 deletions test/integration/rules/empty-heading/empty-heading.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<script src="/axe.js"></script>
</head>
<body>
<h1 id="ignore1" role="banner">Meh</h1>

<h1 id="pass1">Ok</h1>
<h2 id="pass2">Ok</h2>
<h3 id="pass3">Ok</h3>
Expand All @@ -16,8 +18,6 @@ <h6 id="pass6">Ok</h6>
<h1 id="fail1"></h1>
<h1 id="fail2"><div style="display: none">Not Ok</div></h1>
<h1 id="fail3"> </h1>
<h3 role="presentation" id="pass8">Should still work</h3>
<h3 role="none" id="pass9">Should also work</h3>

</body>
</html>
4 changes: 1 addition & 3 deletions test/integration/rules/empty-heading/empty-heading.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
["#pass4"],
["#pass5"],
["#pass6"],
["#pass7"],
["#pass8"],
["#pass9"]
["#pass7"]
]
}
2 changes: 2 additions & 0 deletions test/integration/rules/heading-order/heading-order.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ <h3 id="heading4">Header</h3>
<h1 id="heading5">Header</h1>
<h3 id="heading6">Header</h3>
<h3 id="heading7">Header</h3>

<h6 role="banner">Ignore</h6>
55 changes: 55 additions & 0 deletions test/rule-matches/heading-matches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
describe('heading-matches', function () {
'use strict';

var fixture = document.getElementById('fixture');
var rule;

beforeEach(function () {
rule = axe._audit.rules.find(function (rule) {
return rule.id === 'empty-heading';
});
});

afterEach(function () {
fixture.innerHTML = '';
});

it('is a function', function () {
assert.isFunction(rule.matches);
});

it('should return false on elements that are not headings', function () {
var div = document.createElement('div');
assert.isFalse(rule.matches(div));
});

it('should return true on elements with "heading" in the role', function () {
var div = document.createElement('div');
div.setAttribute('role', 'heading');
assert.isTrue(rule.matches(div));

div.setAttribute('role', 'slider heading');
assert.isTrue(rule.matches(div));
});

it('should return true on regular headings without roles', function () {
var h1 = document.createElement('h1');
var h2 = document.createElement('h2');
var h3 = document.createElement('h3');
Copy link
Contributor

Choose a reason for hiding this comment

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

Do these in-memory elements get cleaned up automatically by the test runner?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, but they do by the garbage collector.

assert.isTrue(rule.matches(h1));
assert.isTrue(rule.matches(h2));
assert.isTrue(rule.matches(h3));
});

it('should return false on headings with their role changes', function () {
var h1 = document.createElement('h1');
h1.setAttribute('role', 'banner');
assert.isFalse(rule.matches(h1));
});

it('should return true on headings with their role changes to an invalid role', function () {
var h1 = document.createElement('h1');
h1.setAttribute('role', 'bruce');
assert.isTrue(rule.matches(h1));
});
});