Skip to content

Commit d280c5f

Browse files
marcysuttonWilcoFiers
authored andcommitted
fix(rule): Allow empty aria-labelledby values (#829)
Closes #372
1 parent 688f427 commit d280c5f

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/commons/aria/attributes.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ aria.validateAttr = function (att) {
5555
* @return {Boolean}
5656
*/
5757
aria.validateAttrValue = function (node, attr) {
58-
/*eslint complexity: ["error",15]*/
58+
/*eslint complexity: ["error",17]*/
5959
'use strict';
6060
var matches, list,
6161
value = node.getAttribute(attr),
@@ -84,6 +84,10 @@ aria.validateAttrValue = function (node, attr) {
8484
return !!(value && doc.getElementById(value));
8585

8686
case 'idrefs':
87+
// exempt attributes that allow empty strings
88+
if ((attrInfo.values && attrInfo.values.indexOf('') !== -1) && value.trim().length === 0) {
89+
return true;
90+
}
8791
list = axe.utils.tokenList(value);
8892
// Check if any value isn't in the list of values
8993
return list.reduce(function (result, token) {

lib/commons/aria/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ lookupTable.attributes = {
8787
type: 'string'
8888
},
8989
'aria-labelledby': {
90-
type: 'idrefs'
90+
type: 'idrefs',
91+
values: ['']
9192
},
9293
'aria-level': {
9394
type: 'int'

test/checks/aria/valid-attr-value.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ describe('aria-valid-attr-value', function () {
33

44
var fixture = document.getElementById('fixture');
55
var checkContext = axe.testUtils.MockCheckContext();
6+
var fixtureSetup = axe.testUtils.fixtureSetup;
67

78
afterEach(function () {
89
fixture.innerHTML = '';
@@ -89,6 +90,17 @@ describe('aria-valid-attr-value', function () {
8990
axe.commons.aria.validateAttrValue = orig;
9091
});
9192

93+
it('should allow empty strings rather than idrefs for specific attributes', function () {
94+
fixtureSetup(
95+
'<button aria-labelledby="">Button</button>' +
96+
'<div aria-owns=""></div>'
97+
);
98+
var passing = fixture.querySelector('button');
99+
var failing = fixture.querySelector('div');
100+
assert.isTrue(checks['aria-valid-attr-value'].evaluate.call(checkContext, passing));
101+
assert.isFalse(checks['aria-valid-attr-value'].evaluate.call(checkContext, failing));
102+
});
103+
92104
describe('options', function () {
93105
it('should exclude supplied attributes', function () {
94106
fixture.innerHTML = '<div id="target" aria-live="nope" aria-describedby="no exist k thx"></div>';

0 commit comments

Comments
 (0)