Skip to content

Commit

Permalink
fix(rule): Ignore hashbang URLs for skiplinks (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers authored Apr 11, 2018
1 parent 5651ecc commit e1f0c57
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/checks/navigation/internal-link-present.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const links = axe.utils.querySelectorAll(virtualNode, 'a[href]');
return links.some(vLink => vLink.actualNode.getAttribute('href')[0] === '#');
return links.some(vLink => {
return /^#[^/!]/.test(vLink.actualNode.getAttribute('href'))
});
3 changes: 1 addition & 2 deletions lib/rules/skip-link-matches.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
const href = node.getAttribute('href');
return (href[0] === '#' && href.length > 1);
return /^#[^/!]/.test(node.getAttribute('href'));
17 changes: 16 additions & 1 deletion test/checks/navigation/internal-link-present.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('internal-link-present', function () {

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

Expand All @@ -18,6 +18,21 @@ describe('internal-link-present', function () {
assert.isTrue(checks['internal-link-present'].evaluate.apply(checkContext, params));
});

it('should return false when a hashbang URL was used', function () {
var params = checkSetup('<div id="target"><a href="#!foo">hi</a></div>');
assert.isFalse(checks['internal-link-present'].evaluate.apply(checkContext, params));
});

it('should return false when a hash route URL was used', function () {
var params = checkSetup('<div id="target"><a href="#/home">hi</a></div>');
assert.isFalse(checks['internal-link-present'].evaluate.apply(checkContext, params));
});

it('should return false when a hashbang + slash route URL was used', function () {
var params = checkSetup('<div id="target"><a href="#!/home">hi</a></div>');
assert.isFalse(checks['internal-link-present'].evaluate.apply(checkContext, params));
});

it('should otherwise return false', function () {
var params = checkSetup('<div id="target"><a href="http://www.deque.com/#haha">hi</a></div>');
assert.isFalse(checks['internal-link-present'].evaluate.apply(checkContext, params));
Expand Down
2 changes: 1 addition & 1 deletion test/integration/full/bypass/skip-link.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<script src="/test/integration/no-ui-reporter.js"></script>
</head>
<body>
<a href="#">Skip</a>
<a href="#mocha">Skip</a>
<div>Test.</div>
<a href="http://www.deque.com">stuff</a>
<div id="mocha"></div>
Expand Down
46 changes: 46 additions & 0 deletions test/rule-matches/skip-link-matches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
describe('skip-link-matches', function () {
'use strict';

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

beforeEach(function () {
rule = axe._audit.rules.find(function (rule) {
return rule.id === 'skip-link';
});
link = document.createElement('a')
});

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

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

it('returns false if the href attribute does not start with #', function () {
link.href = 'foo#bar';
assert.isFalse(rule.matches(link));
});

it('returns false if the href attribute is `#`', function () {
link.href = '#';
assert.isFalse(rule.matches(link));
});

it('returns true if the href attribute starts with #', function () {
link.href = '#foo';
assert.isTrue(rule.matches(link));
});

it('returns false if the href attribute starts with #!', function () {
link.href = '#!foo';
assert.isFalse(rule.matches(link));
});

it('returns false if the href attribute starts with #/', function () {
link.href = '#/foo';
assert.isFalse(rule.matches(link));
});
});

0 comments on commit e1f0c57

Please sign in to comment.