Skip to content

Commit

Permalink
feat(rule): add color-contrast check for unicode characters.
Browse files Browse the repository at this point in the history
Adding two new config flags to color-contrast so it can check unicode character based icons.

Needs tests.

Related to dequelabs#1906.
  • Loading branch information
Kyle McNutt authored and Kyle McNutt committed Dec 4, 2019
1 parent a9506a0 commit 58e9dea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
20 changes: 16 additions & 4 deletions lib/checks/color/color-contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ if (!dom.isVisible(node, false)) {
return true;
}

const visibleText = text.visibleVirtual(virtualNode, false, true);
const ignoreUnicode = !!(options || {}).ignoreUnicode;
const textContainsOnlyUnicode = !text.removeUnicode(visibleText, {
emoji: false,
nonBmp: true,
punctuations: false
}).length;

if (textContainsOnlyUnicode && ignoreUnicode) {
return true;
}

const noScroll = !!(options || {}).noScroll;
const bgNodes = [];
const bgColor = color.getBackgroundColor(node, bgNodes, noScroll);
Expand All @@ -28,11 +40,11 @@ if (bgColor === null) {
}

const equalRatio = truncatedResult === 1;
const shortTextContent =
text.visibleVirtual(virtualNode, false, true).length === 1;
const shortTextContent = visibleText.length === 1;
const ignoreLength = !!(options || {}).ignoreLength;
if (equalRatio) {
missing = color.incompleteData.set('bgColor', 'equalRatio');
} else if (shortTextContent) {
} else if (shortTextContent && !ignoreLength) {
// Check that the text content is a single character long
missing = 'shortTextContent';
}
Expand All @@ -55,7 +67,7 @@ if (
fgColor === null ||
bgColor === null ||
equalRatio ||
(shortTextContent && !cr.isValid)
(shortTextContent && !ignoreLength && !cr.isValid)
) {
missing = null;
color.incompleteData.clear();
Expand Down
5 changes: 5 additions & 0 deletions lib/checks/color/color-contrast.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"evaluate": "color-contrast.js",
"metadata": {
"impact": "serious",
"options": {
"noScroll": false,
"ignoreUnicode": true,
"ignoreLength": false
},
"messages": {
"pass": "Element has sufficient color contrast of {{=it.data.contrastRatio}}",
"fail": "Element has insufficient color contrast of {{=it.data.contrastRatio}} (foreground color: {{=it.data.fgColor}}, background color: {{=it.data.bgColor}}, font size: {{=it.data.fontSize}}, font weight: {{=it.data.fontWeight}}). Expected contrast ratio of {{=it.data.expectedContrastRatio}}",
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/color-contrast-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ if (
visibleText === '' ||
axe.commons.text.removeUnicode(visibleText, {
emoji: true,
nonBmp: true,
nonBmp: false,
punctuations: true
}) === ''
) {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/color-contrast.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"matches": "color-contrast-matches.js",
"excludeHidden": false,
"options": {
"noScroll": false
"noScroll": false,
"ignoreUnicode": true,
"ignoreLength": false
},
"tags": ["cat.color", "wcag2aa", "wcag143"],
"metadata": {
Expand Down

0 comments on commit 58e9dea

Please sign in to comment.