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(color-contrast): ignore form elements that move text outside of node using text-indent #2044

Merged
merged 2 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions lib/rules/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ if (
return false;
}

// form elements that don't have direct child text nodes need to check that
// the text indent has not been changed and moved the text away from the
// control
const formElements = ['INPUT', 'SELECT', 'TEXTAREA'];
if (formElements.includes(nodeName)) {
const style = window.getComputedStyle(node);
const textIndent = parseInt(style.getPropertyValue('text-indent'), 10);

if (textIndent) {
// since we can't get the actual bounding rect of the text node, we'll
// use the current nodes bounding rect and adjust by the text-indent to
// see if it still overlaps the node
let rect = node.getBoundingClientRect();
rect = {
top: rect.top,
bottom: rect.bottom,
left: rect.left + textIndent,
right: rect.right + textIndent
};

if (!axe.commons.dom.visuallyOverlaps(rect, node)) {
return false;
}
}
}

if (nodeName === 'INPUT') {
return (
['hidden', 'range', 'color', 'checkbox', 'radio', 'image'].indexOf(
Expand Down
26 changes: 26 additions & 0 deletions test/rule-matches/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ describe('color-contrast-matches', function() {
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(target)));
});

it('should not match input when there is text that is out of the container', function() {
fixture.innerHTML =
'<input style="text-indent: -9999px" type="submit" value="Search" />';
var target = fixture.querySelector('input');
axe.testUtils.flatTreeSetup(fixture);
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(target)));
});

it('should not match select when there is text that is out of the container', function() {
fixture.innerHTML =
'<select style="text-indent: -9999px">' +
'<option selected>My text</option>' +
'</select>';
var target = fixture.querySelector('select');
axe.testUtils.flatTreeSetup(fixture);
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(target)));
});

it('should not match textarea when there is text that is out of the container', function() {
fixture.innerHTML =
'<textarea style="text-indent: -9999px">My text</textarea>';
var target = fixture.querySelector('textarea');
axe.testUtils.flatTreeSetup(fixture);
assert.isFalse(rule.matches(target, axe.utils.getNodeFromTree(target)));
});

it('should not match when there is text that is out of the container with overflow hidden', function() {
fixture.innerHTML =
'<div style="color: yellow; width: 100px; overflow: hidden; text-indent: 200px; background-color: white;" id="target">' +
Expand Down