Skip to content

Commit

Permalink
feat(landmark-no-more-than-one-banner): add rule ensuring no more tha…
Browse files Browse the repository at this point in the history
…n one banner
  • Loading branch information
sulsanaul authored and WilcoFiers committed Mar 3, 2018
1 parent aa074fe commit 6617800
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/rule-descriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
| label-title-only | Ensures that every form element is not solely labeled using the title or aria-describedby attributes | cat.forms, best-practice | true |
| label | Ensures every form element has a label | cat.forms, wcag2a, wcag332, wcag131, section508, section508.22.n | true |
| landmark-main-is-top-level | The main landmark should not be contained in another landmark | best-practice | true |
| landmark-no-more-than-one-banner | A banner landmark identifies site-oriented content at the beginning of each page within a website | best-practice | true |
| landmark-one-main | Ensures a navigation point to the primary content of the page. If the page contains iframes, each iframe should contain either no main landmarks or just one. | best-practice | true |
| layout-table | Ensures presentational <table> elements do not use <th>, <caption> elements or the summary attribute | cat.semantics, wcag2a, wcag131 | true |
| link-in-text-block | Links can be distinguished without relying on color | cat.color, experimental, wcag2a, wcag141 | true |
Expand Down
29 changes: 29 additions & 0 deletions lib/checks/keyboard/has-no-more-than-one-banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const banners = axe.utils.querySelectorAll(virtualNode, 'header, [role=banner]');
const sectioning = ['main', 'section', 'aside', 'nav', 'article'];
var count = 0;

function isBanner(node){
var parent = axe.commons.dom.getComposedParent(node);
while (parent){
if (sectioning.includes(parent.tagName.toLowerCase())){
return false;
}
parent = axe.commons.dom.getComposedParent(parent);
}
return true;
}

for (var i=0; i<banners.length; i++){
var node = banners[i].actualNode;
var role = node.getAttribute('role');
if (!!role){
role = role.toLowerCase();
}
if (role==='banner' || isBanner(node)){
count++;
}
if (count>1){
return false;
}
}
return true;
11 changes: 11 additions & 0 deletions lib/checks/keyboard/has-no-more-than-one-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "has-no-more-than-one-banner",
"evaluate": "has-no-more-than-one-banner.js",
"metadata": {
"impact": "moderate",
"messages": {
"pass": "Document has no more than one banner landmark",
"fail": "Document has more than one banner landmark"
}
}
}
16 changes: 16 additions & 0 deletions lib/rules/landmark-no-more-than-one-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "landmark-no-more-than-one-banner",
"selector": "html",
"tags": [
"best-practice"
],
"metadata": {
"description": "A banner landmark identifies site-oriented content at the beginning of each page within a website",
"help": "Page must not contain more than one banner landmark"
},
"all": [],
"any": [
"has-no-more-than-one-banner"
],
"none": []
}
50 changes: 50 additions & 0 deletions test/checks/keyboard/has-no-more-than-one-banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
describe('has-no-more-than-one-banner', function () {
'use strict';

var fixture = document.getElementById('fixture');
var checkContext = new axe.testUtils.MockCheckContext();
var checkSetup = axe.testUtils.checkSetup;
var shadowCheckSetup = axe.testUtils.shadowCheckSetup;
var shadowSupported = axe.testUtils.shadowSupport.v1;

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

it('should return false if there is more than one element with role banner', function () {
var params = checkSetup('<div id="target"><div role="banner"></div><div role="banner"></div></div>');
assert.isFalse(checks['has-no-more-than-one-banner'].evaluate.apply(checkContext, params));

});

it('should return false if there is more than one header serving as a banner', function () {
var params = checkSetup('<div id="target"><header></header><header></header></div>');
assert.isFalse(checks['has-no-more-than-one-banner'].evaluate.apply(checkContext, params));
});

it('should return true if there are multiple headers contained in sectioning elements', function(){
var params = checkSetup('<div role="main" id="target"><header></header><header></header></div>');
assert.isTrue(checks['has-no-more-than-one-main'].evaluate.apply(checkContext, params));
});

it('should return true if there is only one element with role banner', function(){
var params = checkSetup('<div role="banner" id="target"></div>');
assert.isTrue(checks['has-no-more-than-one-banner'].evaluate.apply(checkContext, params));
});

it('should return true if there is only one header serving as a banner', function(){
var params = checkSetup('<header id="target"></header>');
assert.isTrue(checks['has-no-more-than-one-banner'].evaluate.apply(checkContext, params));
});

(shadowSupported ? it : xit)
('should return false if there is a second banner inside the shadow dom', function () {
var params = shadowCheckSetup(
'<div role="banner" id="target"></div>',
'<div role="banner"></div>'
);
assert.isFalse(checks['has-no-more-than-one-banner'].evaluate.apply(checkContext, params));
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en" id="fail2">
<head>
<meta charset="utf8">
<script src="/axe.js"></script>
</head>
<body>
<header>
Header 1
</header>
<header>
Header 2
</header>
<iframe id="frame2" src="level2.html"></iframe>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en" id="pass2">
<head>
<meta charset="utf8">
<script src="/axe.js"></script>
</head>
<body>
<main>
<header>
Header 1 contained in main landmark
</header>
<header>
Header 2 contained in main landmark
</header>
</main>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en" id="fail3">
<head>
<meta charset="utf8">
<script src="/axe.js"></script>
</head>
<body>
<main>
<div role="banner">
Div 1 with role banner in main landmark
</div>
<div role="banner">
Div 2 with role banner in main landmark
</div>
</main>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!doctype html>
<html lang="en" id="fail1">
<head>
<meta charset="utf8">
<link rel="stylesheet" type="text/css" href="/node_modules/mocha/mocha.css" />
<script src="/node_modules/mocha/mocha.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script src="/axe.js"></script>
<script>
mocha.setup({
timeout: 10000,
ui: 'bdd'
});
var assert = chai.assert;
</script>
</head>
<body>
<div role="banner">
Div 1 with role of "banner"
</div>
<div role="banner">
Div 2 with role of "banner"
</div>
<iframe id="frame1" src="frames/level1-fail.html"></iframe>
<div id="mocha"></div>
<script src="landmark-no-more-than-one-banner-fail.js"></script>
<script src="/test/integration/adapter.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
describe('landmark-no-more-than-one-banner test failure', function () {
'use strict';
var results;
before(function (done) {
function start() {
axe.run({ runOnly: { type: 'rule', values: ['landmark-no-more-than-one-banner'] } }, function (err, r) {
assert.isNull(err);
results = r;
done();
});
}
if (document.readyState !== 'complete') {
window.addEventListener('load', start);
} else {
start();
}
});

describe('violations', function () {
it('should find 3', function () {
assert.lengthOf(results.violations[0].nodes, 3);
});

it('should find #fail1', function () {
assert.deepEqual(results.violations[0].nodes[0].target, ['#fail1']);
});

it('should find #frame1, #fail2', function () {
assert.deepEqual(results.violations[0].nodes[1].target, ['#frame1', '#fail2']);
});

it('should find #frame1, #frame2, #fail3', function () {
assert.deepEqual(results.violations[0].nodes[2].target, ['#frame1', '#frame2', '#fail3']);
});

});

describe('passes', function () {
it('should find 0', function () {
assert.lengthOf(results.passes, 0);
});
});

it('should find 0 inapplicable', function () {
assert.lengthOf(results.inapplicable, 0);
});

it('should find 0 incomplete', function () {
assert.lengthOf(results.incomplete, 0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en" id="pass1">
<head>
<meta charset="utf8">
<link rel="stylesheet" type="text/css" href="/node_modules/mocha/mocha.css" />
<script src="/node_modules/mocha/mocha.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script src="/axe.js"></script>
<script>
mocha.setup({
timeout: 10000,
ui: 'bdd'
});
var assert = chai.assert;
</script>
</head>
<body>
<p>No banner landmarks</p>
<iframe id="frame1" src="frames/level1.html"></iframe>
<div id="mocha"></div>
<script src="landmark-no-more-than-one-banner-pass.js"></script>
<script src="/test/integration/adapter.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
describe('landmark-no-more-than-one-banner test pass', function () {
'use strict';
var results;
before(function (done) {
function start() {
axe.run({ runOnly: { type: 'rule', values: ['landmark-no-more-than-one-banner'] } }, function (err, r) {
assert.isNull(err);
results = r;
done();
});
}
if (document.readyState !== 'complete') {
window.addEventListener('load', start);
} else {
start();
}
});

describe('violations', function () {
it('should find 0', function () {
assert.lengthOf(results.violations, 0);
});
});

describe('passes', function () {
it('should find 2', function () {
assert.lengthOf(results.passes[0].nodes, 2);
});

it('should find #pass1', function () {
assert.deepEqual(results.passes[0].nodes[0].target, ['#pass1']);
});

it('should find #frame1, #pass2', function () {
assert.deepEqual(results.passes[0].nodes[1].target, ['#frame1', '#pass2']);
});
});

it('should find 0 inapplicable', function () {
assert.lengthOf(results.inapplicable, 0);
});

it('should find 0 incomplete', function () {
assert.lengthOf(results.incomplete, 0);
});

});

0 comments on commit 6617800

Please sign in to comment.