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: Do not flag font icons in color-contrast rule #1095

Merged
merged 3 commits into from
Aug 23, 2018
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
64 changes: 37 additions & 27 deletions lib/checks/color/color-contrast.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
if (!axe.commons.dom.isVisible(node, false)) {
const { dom, color, text } = axe.commons;

if (!dom.isVisible(node, false)) {
return true;
}

var noScroll = !!(options || {}).noScroll;
var bgNodes = [],
bgColor = axe.commons.color.getBackgroundColor(node, bgNodes, noScroll),
fgColor = axe.commons.color.getForegroundColor(node, noScroll);
const noScroll = !!(options || {}).noScroll;
const bgNodes = [];
const bgColor = color.getBackgroundColor(node, bgNodes, noScroll);
const fgColor = color.getForegroundColor(node, noScroll);

var nodeStyle = window.getComputedStyle(node);
var fontSize = parseFloat(nodeStyle.getPropertyValue('font-size'));
var fontWeight = nodeStyle.getPropertyValue('font-weight');
var bold =
const nodeStyle = window.getComputedStyle(node);
const fontSize = parseFloat(nodeStyle.getPropertyValue('font-size'));
const fontWeight = nodeStyle.getPropertyValue('font-weight');
const bold =
['bold', 'bolder', '600', '700', '800', '900'].indexOf(fontWeight) !== -1;

var cr = axe.commons.color.hasValidContrastRatio(
bgColor,
fgColor,
fontSize,
bold
);
const cr = color.hasValidContrastRatio(bgColor, fgColor, fontSize, bold);

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

// if fgColor or bgColor are missing, get more information.
var missing;
let missing;
if (bgColor === null) {
missing = axe.commons.color.incompleteData.get('bgColor');
missing = color.incompleteData.get('bgColor');
}

let equalRatio = false;
if (truncatedResult === 1) {
equalRatio = true;
missing = axe.commons.color.incompleteData.set('bgColor', 'equalRatio');
const equalRatio = truncatedResult === 1;
const shortTextContent =
text.visibleVirtual(virtualNode, false, true).length === 1;
if (equalRatio) {
missing = color.incompleteData.set('bgColor', 'equalRatio');
} else if (shortTextContent) {
// Check that the text content is a single character long
missing = 'shortTextContent';
}

// need both independently in case both are missing
var data = {
const data = {
fgColor: fgColor ? fgColor.toHexString() : undefined,
bgColor: bgColor ? bgColor.toHexString() : undefined,
contrastRatio: cr ? truncatedResult : undefined,
Expand All @@ -48,13 +50,21 @@ var data = {

this.data(data);

//We don't know, so we'll put it into Can't Tell
if (fgColor === null || bgColor === null || equalRatio) {
// We don't know, so we'll put it into Can't Tell
if (
fgColor === null ||
bgColor === null ||
equalRatio ||
(shortTextContent && !cr.isValid)
) {
missing = null;
axe.commons.color.incompleteData.clear();
color.incompleteData.clear();
this.relatedNodes(bgNodes);
return undefined;
} else if (!cr.isValid) {
}

if (!cr.isValid) {
this.relatedNodes(bgNodes);
}

return cr.isValid;
1 change: 1 addition & 0 deletions lib/checks/color/color-contrast.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"elmPartiallyObscuring": "Element's background color could not be determined because it partially overlaps other elements",
"outsideViewport": "Element's background color could not be determined because it's outside the viewport",
"equalRatio": "Element has a 1:1 contrast ratio with the background",
"shortTextContent": "Element content is too short to determine if it is actual text content",
"default": "Unable to determine contrast ratio"
}
}
Expand Down
Loading