diff --git a/lib/checks/label/hidden-explicit-label.js b/lib/checks/label/hidden-explicit-label.js index 0fcdd0dd24..8d7013bbca 100644 --- a/lib/checks/label/hidden-explicit-label.js +++ b/lib/checks/label/hidden-explicit-label.js @@ -3,8 +3,10 @@ if (node.getAttribute('id')) { const id = axe.commons.utils.escapeSelector(node.getAttribute('id')); const label = root.querySelector(`label[for="${id}"]`); - if (label && !axe.commons.dom.isVisible(label)) { - return true; + if (label && !axe.commons.dom.isVisible(label, true)) { + const name = axe.commons.text.accessibleTextVirtual(virtualNode).trim(); + const isNameEmpty = name === ''; + return isNameEmpty; } } return false; diff --git a/test/checks/label/hidden-explicit-label.js b/test/checks/label/hidden-explicit-label.js index 22ac2c7428..2961aa5b91 100644 --- a/test/checks/label/hidden-explicit-label.js +++ b/test/checks/label/hidden-explicit-label.js @@ -1,38 +1,37 @@ describe('hidden-explicit-label', function() { 'use strict'; - var fixture = document.getElementById('fixture'); - var fixtureSetup = axe.testUtils.fixtureSetup; var shadowSupport = axe.testUtils.shadowSupport; var shadowCheckSetup = axe.testUtils.shadowCheckSetup; var checkContext = axe.testUtils.MockCheckContext(); + var checkSetup = axe.testUtils.checkSetup; + var check = checks['hidden-explicit-label']; afterEach(function() { checkContext.reset(); }); it('should return true if a hidden non-empty label is present', function() { - fixtureSetup( - '' + var args = checkSetup( + '', + {}, + '#target' ); - var node = fixture.querySelector('#target'); - assert.isTrue(checks['hidden-explicit-label'].evaluate(node)); + assert.isTrue(check.evaluate.apply(check, args)); }); it('should return false if a visible non-empty label is present', function() { - fixtureSetup( + var args = checkSetup( '' ); - var node = fixture.querySelector('#target'); - assert.isFalse(checks['hidden-explicit-label'].evaluate(node)); + assert.isFalse(check.evaluate.apply(check, args)); }); it('should return true if an invisible empty label is present', function() { - fixtureSetup( + var args = checkSetup( '' ); - var node = fixture.querySelector('#target'); - assert.isTrue(checks['explicit-label'].evaluate(node)); + assert.isTrue(check.evaluate.apply(check, args)); }); (shadowSupport.v1 ? it : xit)( @@ -43,9 +42,7 @@ describe('hidden-explicit-label', function() { '' ); - assert.isTrue( - checks['hidden-explicit-label'].evaluate.apply(shadowCheckSetup, params) - ); + assert.isTrue(check.evaluate.apply(shadowCheckSetup, params)); } ); @@ -57,9 +54,37 @@ describe('hidden-explicit-label', function() { '' ); - assert.isFalse( - checks['hidden-explicit-label'].evaluate.apply(shadowCheckSetup, params) - ); + assert.isFalse(check.evaluate.apply(shadowCheckSetup, params)); } ); + + it('should fail when the label has aria-hidden=true', function() { + var html = ''; + html += '
'; + html += ' '; + html += ' '; + html += '
'; + var args = checkSetup(html, {}, '#target'); + assert.isTrue(check.evaluate.apply(check, args)); + }); + + describe('if the label is hidden', function() { + describe('and the element has an accessible name', function() { + it('should not fail', function() { + var html = ''; + + html += '
'; + html += ' '; + html += ' '; + html += '
'; + + var args = checkSetup(html, {}, '#target'); + assert.isFalse(check.evaluate.apply(check, args)); + }); + }); + }); }); diff --git a/test/integration/rules/label/label.html b/test/integration/rules/label/label.html index 32a090ec66..72cdda7ab9 100644 --- a/test/integration/rules/label/label.html +++ b/test/integration/rules/label/label.html @@ -14,9 +14,9 @@
Label
- - - + + + @@ -24,7 +24,9 @@ - + @@ -57,5 +59,13 @@ + +
+ + +
+ - + \ No newline at end of file diff --git a/test/integration/rules/label/label.json b/test/integration/rules/label/label.json index 6ecf46b157..78c1067532 100644 --- a/test/integration/rules/label/label.json +++ b/test/integration/rules/label/label.json @@ -35,6 +35,7 @@ ["#pass13"], ["#pass14"], ["#pass15"], - ["#pass16"] + ["#pass16"], + ["#pass-gh1176"] ] }