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-enhanced): avoid duplicates between color-contrast and color-contrast-enhanced #3714

Merged
merged 4 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions lib/checks/color/color-contrast-enhanced.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"largeTextPt": 18,
"contrastRatio": {
"normal": {
"expected": 7
"expected": 7,
"minThreshold": 4.5
},
"large": {
"expected": 4.5
"expected": 4.5,
"minThreshold": 3
}
},
"pseudoSizeThreshold": 0.25,
Expand Down
4 changes: 2 additions & 2 deletions lib/checks/color/color-contrast-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export default function colorContrastEvaluate(node, options, virtualNode) {

// ratio is outside range
if (
(typeof minThreshold === 'number' && contrast < minThreshold) ||
(typeof maxThreshold === 'number' && contrast > maxThreshold)
(contrast && typeof minThreshold === 'number' && contrast < minThreshold) ||
(contrast && typeof maxThreshold === 'number' && contrast > maxThreshold)
straker marked this conversation as resolved.
Show resolved Hide resolved
) {
this.data({ contrastRatio: contrast });
return true;
Expand Down
29 changes: 29 additions & 0 deletions lib/commons/color/get-background-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import flattenColors from './flatten-colors';
import flattenShadowColors from './flatten-shadow-colors';
import getTextShadowColors from './get-text-shadow-colors';
import visuallyContains from '../dom/visually-contains';
import { getNodeFromTree } from '../../core/utils';

/**
* Returns background color for element
Expand All @@ -25,6 +26,34 @@ export default function getBackgroundColor(
bgElms = [],
shadowOutlineEmMax = 0.1
) {
const vNode = getNodeFromTree(elm);
const bgColorCache = vNode._cache.getBackgroundColor;

if (bgColorCache) {
bgElms.push(...bgColorCache.bgElms);
incompleteData.set('bgColor', bgColorCache.incompleteData);
return bgColorCache.bgColor;
}

const bgColor = _getBackgroundColor(elm, bgElms, shadowOutlineEmMax);

// TODO: this is a temporary solution to the problem
// of needing to cache the getBackgroundColor call
// but not being able to use `memoize` since this
// is not a pure function due to the `bgElms` array
// and `incompleteData`.
// remove after 4.5 release when we have time to
// properly update the function to be more cacheable
straker marked this conversation as resolved.
Show resolved Hide resolved
vNode._cache.getBackgroundColor = {
bgColor,
bgElms,
incompleteData: incompleteData.get('bgColor')
};

return bgColor;
}

function _getBackgroundColor(elm, bgElms, shadowOutlineEmMax) {
let bgColors = getTextShadowColors(elm, { minRatio: shadowOutlineEmMax });
if (bgColors.length) {
bgColors = [{ color: bgColors.reduce(flattenShadowColors) }];
Expand Down
76 changes: 76 additions & 0 deletions test/checks/color/color-contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,25 @@ describe('color-contrast', function () {
assert.deepEqual(checkContext._relatedNodes, []);
});

it('should report incomplete for options.contrastRatio.normal.minThreshold', function () {
straker marked this conversation as resolved.
Show resolved Hide resolved
var params = checkSetup(
`
<p id="target" style="color: #666; background: linear-gradient(to right, #FFF, #0FF); width: 300px">
Some text in English
</p>`,
{
contrastRatio: {
normal: {
minThreshold: 3
}
}
}
);

assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.equal(checkContext._data.messageKey, 'bgGradient');
});

it('should support options.contrastRatio.normal.maxThreshold', function () {
var params = checkSetup(
'<div style="color: #999; background-color: white; font-size: 14pt; font-weight: 100" id="target">' +
Expand All @@ -740,6 +759,25 @@ describe('color-contrast', function () {
assert.deepEqual(checkContext._relatedNodes, []);
});

it('should report incomplete for options.contrastRatio.normal.maxThreshold', function () {
var params = checkSetup(
`
<p id="target" style="color: #666; background: linear-gradient(to right, #FFF, #0FF); width: 300px">
Some text in English
</p>`,
{
contrastRatio: {
normal: {
maxThreshold: 2
}
}
}
);

assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.equal(checkContext._data.messageKey, 'bgGradient');
});

it('should support options.contrastRatio.large.expected', function () {
var params = checkSetup(
'<div style="color: #ccc; background-color: white; font-size: 18pt; font-weight: 100" id="target">' +
Expand Down Expand Up @@ -774,6 +812,25 @@ describe('color-contrast', function () {
assert.deepEqual(checkContext._relatedNodes, []);
});

it('should report incomplete for options.contrastRatio.large.minThreshold', function () {
var params = checkSetup(
`
<p id="target" style="color: #666; background: linear-gradient(to right, #FFF, #0FF); width: 300px; font-size: 18pt;">
Some text in English
</p>`,
{
contrastRatio: {
large: {
minThreshold: 2
}
}
}
);

assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.equal(checkContext._data.messageKey, 'bgGradient');
});

it('should support options.contrastRatio.large.maxThreshold', function () {
var params = checkSetup(
'<div style="color: #ccc; background-color: white; font-size: 18pt; font-weight: 100" id="target">' +
Expand All @@ -791,6 +848,25 @@ describe('color-contrast', function () {
assert.deepEqual(checkContext._relatedNodes, []);
});

it('should report incomplete for options.contrastRatio.large.maxThreshold', function () {
var params = checkSetup(
`
<p id="target" style="color: #666; background: linear-gradient(to right, #FFF, #0FF); width: 300px; font-size: 18pt;">
Some text in English
</p>`,
{
contrastRatio: {
large: {
maxThreshold: 2
}
}
}
);

assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.equal(checkContext._data.messageKey, 'bgGradient');
});

it('should ignore pseudo element with options.ignorePseudo', function () {
var params = checkSetup(
'<style>.foo { position: relative; } .foo:after { content: ""; position: absolute; width: 100%; height: 100%; background: red; }</style>' +
Expand Down