-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rule): improve messaging for hidden labels
Closes #242
- Loading branch information
1 parent
54fa569
commit ae07b8e
Showing
8 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
if (node.getAttribute('id')) { | ||
const root = axe.commons.dom.getRootNode(node); | ||
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; | ||
} | ||
} | ||
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"id": "hidden-explicit-label", | ||
"evaluate": "hidden-explicit-label.js", | ||
"metadata": { | ||
"impact": "critical", | ||
"messages": { | ||
"pass": "Form element has a visible explicit <label>", | ||
"fail": "Form element has explicit <label> that is hidden" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
], | ||
"none": [ | ||
"help-same-as-label", | ||
"multiple-label" | ||
"multiple-label", | ||
"hidden-explicit-label" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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(); | ||
|
||
afterEach(function () { | ||
checkContext.reset(); | ||
}); | ||
|
||
it('should return true if a hidden non-empty label is present', function () { | ||
fixtureSetup('<label for="target" style="display:none">Text</label><input type="text" id="target">'); | ||
var node = fixture.querySelector('#target'); | ||
assert.isTrue(checks['hidden-explicit-label'].evaluate(node)); | ||
}); | ||
|
||
it('should return false if a visible non-empty label is present', function () { | ||
fixtureSetup('<label for="target">Label</label><input type="text" id="target">'); | ||
var node = fixture.querySelector('#target'); | ||
assert.isFalse(checks['hidden-explicit-label'].evaluate(node)); | ||
}); | ||
|
||
it('should return true if an invisible empty label is present', function () { | ||
fixtureSetup('<label for="target" style="display: none;"></label><input type="text" id="target">'); | ||
var node = fixture.querySelector('#target'); | ||
assert.isTrue(checks['explicit-label'].evaluate(node)); | ||
}); | ||
|
||
(shadowSupport.v1 ? it : xit)('should return true if content is inside of shadow DOM', function () { | ||
var params = shadowCheckSetup( | ||
'<div></div>', | ||
'<label for="target" style="display:none">Text</label><input type="text" id="target">' | ||
); | ||
|
||
assert.isTrue(checks['hidden-explicit-label'].evaluate.apply(shadowCheckSetup, params)); | ||
}); | ||
|
||
(shadowSupport.v1 ? it : xit)('should return false if part of the pairing is inside of shadow DOM', function () { | ||
var params = shadowCheckSetup( | ||
'<div><label for="target" style="display:none">Text</label></div>', | ||
'<input type="text" id="target">' | ||
); | ||
|
||
assert.isFalse(checks['hidden-explicit-label'].evaluate.apply(shadowCheckSetup, params)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
["#fail8"], | ||
["#fail9"], | ||
["#fail10"], | ||
["#fail11"], | ||
["#fail22"], | ||
["#fail23"], | ||
["#fail24"], | ||
|