Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion lib/checks/color/color-contrast-enhanced.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"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",
"nonBmp": "Element content contains only non-text characters",
"pseudoContent": "Element's background color could not be determined due to a pseudo element"
"pseudoContent": "Element's background color could not be determined due to a pseudo element",
"colorParse": "Could not parse color string ${data.colorParse}"
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions lib/checks/color/color-contrast-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,24 @@ export default function colorContrastEvaluate(node, options, virtualNode) {

// if fgColor or bgColor are missing, get more information.
let missing;
let colorParse;

if (bgColor === null) {
missing = incompleteData.get('bgColor');
if (incompleteData.get('colorParse')) {
missing = 'colorParse';
colorParse = incompleteData.get('colorParse');
} else {
missing = incompleteData.get('bgColor');
}
} else if (!isValid) {
missing = contrastContributor;
}

if (fgColor === null && incompleteData.get('colorParse')) {
missing = 'colorParse';
colorParse = incompleteData.get('colorParse');
}

const equalRatio = truncatedResult === 1;
const shortTextContent = visibleText.length === 1;
if (equalRatio) {
Expand All @@ -146,7 +158,8 @@ export default function colorContrastEvaluate(node, options, virtualNode) {
fontWeight: bold ? 'bold' : 'normal',
messageKey: missing,
expectedContrastRatio: expected + ':1',
shadowColor: shadowColor ? shadowColor.toHexString() : undefined
shadowColor: shadowColor ? shadowColor.toHexString() : undefined,
colorParse: colorParse
});

// We don't know, so we'll put it into Can't Tell
Expand Down
3 changes: 2 additions & 1 deletion lib/checks/color/color-contrast.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"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",
"nonBmp": "Element content contains only non-text characters",
"pseudoContent": "Element's background color could not be determined due to a pseudo element"
"pseudoContent": "Element's background color could not be determined due to a pseudo element",
"colorParse": "Could not parse color string ${data.colorParse}"
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/commons/color/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ export default class Color {
// color.alpha is a Number object so convert it to a number
this.alpha = +color.alpha;
} catch {
throw new Error(`Unable to parse color "${colorString}"`);
throw new Error(`Unable to parse color "${colorString}"`, {
cause: { code: 'COLOR_STRING', value: colorString }
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately axe-core has to still "support" IE11 (even though we deprecated it and stopped testing with it) and I don't believe the Error object supports cause in IE11. I tested it out and it doesn't seem to come through so we may not be able to do it this way.

});
}

return this;
Expand Down
14 changes: 11 additions & 3 deletions lib/commons/color/get-background-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ function _getBackgroundColor(elm, bgElms, shadowOutlineEmMax) {
}

// Get the background color
const bgColor = getOwnBackgroundColor(bgElmStyle);
if (bgColor.alpha === 0) {
continue;
let bgColor;
try {
bgColor = getOwnBackgroundColor(bgElmStyle);
if (bgColor.alpha === 0) {
continue;
}
} catch (error) {
if (error.cause) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't assume that any use of the cause object means the colorParse error. Should probably test for the specific error we expect and then set the colorParse data if it matches. That way if we throw a different error in the future that uses cause it won't alway sbe colorParse message.

Copy link
Author

Choose a reason for hiding this comment

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

I was using the error.cause object to pass along the color string that caused the error. In color.js, it is given to the original error string but it becomes part of a full string. At the time, I did not see a way to get the color to have displayed in the colorParse data.colorParse message. I will see if there is another way I can capture the original color string.

incompleteData.set('colorParse', error.cause.value);
return null;
}
}

// abort if a node is partially obscured and obscuring element has a background
Expand Down
23 changes: 15 additions & 8 deletions lib/commons/color/get-foreground-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ export default function getForegroundColor(node, _, bgColor, options = {}) {
];
let fgColors = [];

for (const colorFn of colorStack) {
const color = colorFn();
if (!color) {
continue;
}
try {
for (const colorFn of colorStack) {
const color = colorFn();
if (!color) {
continue;
}

fgColors = fgColors.concat(color);
fgColors = fgColors.concat(color);

if (color.alpha === 1) {
break;
if (color.alpha === 1) {
break;
}
}
} catch (error) {
if (error.cause) {
incompleteData.set('colorParse', error.cause.value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto for here (don't assume cause === colorParse)

return null;
}
}

Expand Down
6 changes: 4 additions & 2 deletions locales/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@
"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",
"nonBmp": "Element content contains only non-text characters",
"pseudoContent": "Element's background color could not be determined due to a pseudo element"
"pseudoContent": "Element's background color could not be determined due to a pseudo element",
"colorParse": "Could not parse color string ${data.colorParse}"
}
},
"color-contrast": {
Expand All @@ -655,7 +656,8 @@
"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",
"nonBmp": "Element content contains only non-text characters",
"pseudoContent": "Element's background color could not be determined due to a pseudo element"
"pseudoContent": "Element's background color could not be determined due to a pseudo element",
"colorParse": "Could not parse color string ${data.colorParse}"
}
},
"link-in-text-block-style": {
Expand Down
39 changes: 39 additions & 0 deletions test/checks/color/color-contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,45 @@ describe('color-contrast', function () {
axe._tree = undefined;
});

it('should return undefined if cannot handle color', function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Great tests!

var params = checkSetup(
'<div id="divundertest" style="color: oklch(0.961073 0.000047911 none / 0.2); background-color: black; font-size: 14pt; font-weight: 900;">' +
'<span id="target" style="font-weight:lighter;">My text</span></div>'
);

var expectedRelatedNodes = fixture.querySelector('#divundertest');
assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, [expectedRelatedNodes]);
assert.deepEqual(checkContext._data.messageKey, 'colorParse');
assert.equal(
checkContext._data.colorParse,
'oklch(0.961073 0.000047911 none / 0.2)'
);
});

it('should should return undefined if cannot handle backgroundcolor', function () {
var params = checkSetup(
'<div style="color: gray; background-color: oklch(0.961073 0.000047911 none / 0.2); font-size: 14pt; font-weight: 900;">' +
'<span id="target" style="font-weight:lighter;">My text</span></div>'
);
assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
assert.deepEqual(checkContext._data.messageKey, 'colorParse');
assert.equal(
checkContext._data.colorParse,
'oklch(0.961073 0.000047911 none / 0.2)'
);
});

it('should should return undefined if cannot handle text-shadow', function () {
var params = checkSetup(
'<div id="target" style="background-color: #fff; color:#000; text-shadow: 1px 1px oklch(0.961073 0.000047911 none / 0.2);">My text</div>'
);

assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});

it('should return true for hidden element', function () {
var params = checkSetup(
'<div style="color: gray; background-color: white; font-size: 14pt; font-weight: 100;">' +
Expand Down