Skip to content

Commit

Permalink
fix(empty-heading): Skip headings with the role changed #645 (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Feb 14, 2018
1 parent 87fa280 commit 80ef961
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
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);
}

// 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');
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));
});
});

0 comments on commit 80ef961

Please sign in to comment.