Skip to content

Commit b6ac084

Browse files
authored
fix: Do not flag font icons in color-contrast rule (#1095)
This fix ensures that font icons and other single character elements are put into needs review for the color-contrast rule. Closes #1068 ## Reviewer checks **Required fields, to be filled out by PR reviewer(s)** - [x] Follows the commit message policy, appropriate for next version - [x] Has documentation updated, a DU ticket, or requires no documentation change - [x] Includes new tests, or was unnecessary - [x] Code is reviewed for security by: @dylanb
1 parent 6af5733 commit b6ac084

File tree

3 files changed

+187
-177
lines changed

3 files changed

+187
-177
lines changed

lib/checks/color/color-contrast.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
if (!axe.commons.dom.isVisible(node, false)) {
1+
const { dom, color, text } = axe.commons;
2+
3+
if (!dom.isVisible(node, false)) {
24
return true;
35
}
46

5-
var noScroll = !!(options || {}).noScroll;
6-
var bgNodes = [],
7-
bgColor = axe.commons.color.getBackgroundColor(node, bgNodes, noScroll),
8-
fgColor = axe.commons.color.getForegroundColor(node, noScroll);
7+
const noScroll = !!(options || {}).noScroll;
8+
const bgNodes = [];
9+
const bgColor = color.getBackgroundColor(node, bgNodes, noScroll);
10+
const fgColor = color.getForegroundColor(node, noScroll);
911

10-
var nodeStyle = window.getComputedStyle(node);
11-
var fontSize = parseFloat(nodeStyle.getPropertyValue('font-size'));
12-
var fontWeight = nodeStyle.getPropertyValue('font-weight');
13-
var bold =
12+
const nodeStyle = window.getComputedStyle(node);
13+
const fontSize = parseFloat(nodeStyle.getPropertyValue('font-size'));
14+
const fontWeight = nodeStyle.getPropertyValue('font-weight');
15+
const bold =
1416
['bold', 'bolder', '600', '700', '800', '900'].indexOf(fontWeight) !== -1;
1517

16-
var cr = axe.commons.color.hasValidContrastRatio(
17-
bgColor,
18-
fgColor,
19-
fontSize,
20-
bold
21-
);
18+
const cr = color.hasValidContrastRatio(bgColor, fgColor, fontSize, bold);
2219

2320
// truncate ratio to three digits while rounding down
2421
// 4.499 = 4.49, 4.019 = 4.01
25-
var truncatedResult = Math.floor(cr.contrastRatio * 100) / 100;
22+
const truncatedResult = Math.floor(cr.contrastRatio * 100) / 100;
2623

2724
// if fgColor or bgColor are missing, get more information.
28-
var missing;
25+
let missing;
2926
if (bgColor === null) {
30-
missing = axe.commons.color.incompleteData.get('bgColor');
27+
missing = color.incompleteData.get('bgColor');
3128
}
3229

33-
let equalRatio = false;
34-
if (truncatedResult === 1) {
35-
equalRatio = true;
36-
missing = axe.commons.color.incompleteData.set('bgColor', 'equalRatio');
30+
const equalRatio = truncatedResult === 1;
31+
const shortTextContent =
32+
text.visibleVirtual(virtualNode, false, true).length === 1;
33+
if (equalRatio) {
34+
missing = color.incompleteData.set('bgColor', 'equalRatio');
35+
} else if (shortTextContent) {
36+
// Check that the text content is a single character long
37+
missing = 'shortTextContent';
3738
}
39+
3840
// need both independently in case both are missing
39-
var data = {
41+
const data = {
4042
fgColor: fgColor ? fgColor.toHexString() : undefined,
4143
bgColor: bgColor ? bgColor.toHexString() : undefined,
4244
contrastRatio: cr ? truncatedResult : undefined,
@@ -48,13 +50,21 @@ var data = {
4850

4951
this.data(data);
5052

51-
//We don't know, so we'll put it into Can't Tell
52-
if (fgColor === null || bgColor === null || equalRatio) {
53+
// We don't know, so we'll put it into Can't Tell
54+
if (
55+
fgColor === null ||
56+
bgColor === null ||
57+
equalRatio ||
58+
(shortTextContent && !cr.isValid)
59+
) {
5360
missing = null;
54-
axe.commons.color.incompleteData.clear();
61+
color.incompleteData.clear();
5562
this.relatedNodes(bgNodes);
5663
return undefined;
57-
} else if (!cr.isValid) {
64+
}
65+
66+
if (!cr.isValid) {
5867
this.relatedNodes(bgNodes);
5968
}
69+
6070
return cr.isValid;

lib/checks/color/color-contrast.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"elmPartiallyObscuring": "Element's background color could not be determined because it partially overlaps other elements",
1717
"outsideViewport": "Element's background color could not be determined because it's outside the viewport",
1818
"equalRatio": "Element has a 1:1 contrast ratio with the background",
19+
"shortTextContent": "Element content is too short to determine if it is actual text content",
1920
"default": "Unable to determine contrast ratio"
2021
}
2122
}

0 commit comments

Comments
 (0)