Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider element's accessible names when labels are hidden #1187

Merged
merged 5 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/checks/label/hidden-explicit-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ if (node.getAttribute('id')) {
const label = root.querySelector(`label[for="${id}"]`);

if (label && !axe.commons.dom.isVisible(label)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change this so it does !axe.commons.dom.isVisible(label, true)).

Here's a test case for it:

it('should fail when the label has aria-hidden=true', function() {
	var html = '';
	html += '<div>';
	html += '  <label for="target" aria-hidden="true">';
	html += '    Hello world';
	html += '  </label>';
	html += '  <input id="target">';
	html += '</div>';
	var args = checkSetup(html, {}, '#target');
	assert.isTrue(check.evaluate.apply(check, args));
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Updated.

return true;
const name = axe.commons.text.accessibleTextVirtual(virtualNode).trim();
const isNameEmpty = name === '';
return isNameEmpty;
}
}
return false;
50 changes: 32 additions & 18 deletions test/checks/label/hidden-explicit-label.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
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(
'<label for="target" style="display:none">Text</label><input type="text" id="target">'
var args = checkSetup(
'<label for="target" style="display:none">Text</label><input type="text" id="target">',
{},
'#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(
'<label for="target">Label</label><input type="text" id="target">'
);
var node = fixture.querySelector('#target');
assert.isFalse(checks['hidden-explicit-label'].evaluate(node));
assert.isFalse(check.evaluate.apply(check, args));
});

// TODO: this test appears to be testing the wrong check. Investigate and fix.
it('should return true if an invisible empty label is present', function() {
fixtureSetup(
var args = checkSetup(
'<label for="target" style="display: none;"></label><input type="text" id="target">'
);
var node = fixture.querySelector('#target');
assert.isTrue(checks['explicit-label'].evaluate(node));
assert.isTrue(check.evaluate.apply(check, args));
});

(shadowSupport.v1 ? it : xit)(
Expand All @@ -43,9 +43,7 @@ describe('hidden-explicit-label', function() {
'<label for="target" style="display:none">Text</label><input type="text" id="target">'
);

assert.isTrue(
checks['hidden-explicit-label'].evaluate.apply(shadowCheckSetup, params)
);
assert.isTrue(check.evaluate.apply(shadowCheckSetup, params));
}
);

Expand All @@ -57,9 +55,25 @@ describe('hidden-explicit-label', function() {
'<input type="text" id="target">'
);

assert.isFalse(
checks['hidden-explicit-label'].evaluate.apply(shadowCheckSetup, params)
);
assert.isFalse(check.evaluate.apply(shadowCheckSetup, params));
}
);

describe('if the label is hidden', function() {
describe('and the element has an accessible name', function() {
it('should not fail', function() {
var html = '';

html += '<div>';
html += ' <label for="target" style="display:none">';
html += ' Hello world';
html += ' </label>';
html += ' <input id="target" title="Hi">';
html += '</div>';

var args = checkSetup(html, {}, '#target');
assert.isFalse(check.evaluate.apply(check, args));
});
});
});
});
20 changes: 15 additions & 5 deletions test/integration/rules/label/label.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
<select aria-labelledby="label" id="pass5"></select>
<textarea aria-labelledby="label" id="pass6"></textarea>
<div id="label">Label</div>
<label>Label <input type="text" id="pass7"></label>
<label>Label <select id="pass8"></select></label>
<label>Label <textarea id="pass9"></textarea></label>
<label>Label <input type="text" id="pass7"></label>
<label>Label <select id="pass8"></select></label>
<label>Label <textarea id="pass9"></textarea></label>

<label><input type="text" id="fail4"></label>
<label><select id="fail5"></select></label>
<label><textarea id="fail6"></textarea></label>
<label for="fail7"></label><input type="text" id="fail7">
<label for="fail8"></label><select id="fail8"></select>
<label for="fail9"></label><textarea id="fail9"></textarea>
<label for="fail10"><select id="fail10"><option>Thing</option></select></label>
<label for="fail10"><select id="fail10">
<option>Thing</option>
</select></label>
<label for="fail11" style="display: none;">Text</label><input type="text" id="fail11">
<label for="pass10">Label</label><input type="text" id="pass10">
<label for="pass11">Label</label><select id="pass11"></select>
Expand Down Expand Up @@ -57,5 +59,13 @@
<input type="text" id="fail25" />
</div>


<div>
<label for="pass-gh1176" style="display:none">
Hello world
</label>
<input id="pass-gh1176" title="Hi">
</div>

</div>
</form>
</form>
3 changes: 2 additions & 1 deletion test/integration/rules/label/label.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
["#pass13"],
["#pass14"],
["#pass15"],
["#pass16"]
["#pass16"],
["#pass-gh1176"]
]
}