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

ensure pseudo elements also return required contrast information #3453

Merged
merged 3 commits into from
May 12, 2022
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
33 changes: 20 additions & 13 deletions lib/checks/color/color-contrast-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,33 @@ export default function colorContrastEvaluate(node, options, virtualNode) {
return undefined;
}

const nodeStyle = window.getComputedStyle(node);
const fontSize = parseFloat(nodeStyle.getPropertyValue('font-size'));
const fontWeight = nodeStyle.getPropertyValue('font-weight');
const bold = parseFloat(fontWeight) >= boldValue || fontWeight === 'bold';

const ptSize = Math.ceil(fontSize * 72) / 96;
stephenmathieson marked this conversation as resolved.
Show resolved Hide resolved
const isSmallFont =
(bold && ptSize < boldTextPt) || (!bold && ptSize < largeTextPt);

const { expected, minThreshold, maxThreshold } = isSmallFont
? contrastRatio.normal
: contrastRatio.large;

// if element or a parent has pseudo content then we need to mark
// as needs review
const pseudoElm = findPseudoElement(virtualNode, {
ignorePseudo,
pseudoSizeThreshold
});
if (pseudoElm) {
this.data({ messageKey: 'pseudoContent' });
this.data({
fontSize: `${((fontSize * 72) / 96).toFixed(1)}pt (${fontSize}px)`,
fontWeight: bold ? 'bold' : 'normal',
messageKey: 'pseudoContent',
expectedContrastRatio: expected + ':1'
});

this.relatedNodes(pseudoElm.actualNode);
return undefined;
}
Expand All @@ -61,11 +80,6 @@ export default function colorContrastEvaluate(node, options, virtualNode) {
maxRatio: shadowOutlineEmMax
});

const nodeStyle = window.getComputedStyle(node);
const fontSize = parseFloat(nodeStyle.getPropertyValue('font-size'));
const fontWeight = nodeStyle.getPropertyValue('font-weight');
const bold = parseFloat(fontWeight) >= boldValue || fontWeight === 'bold';

let contrast = null;
let contrastContributor = null;
let shadowColor = null;
Expand All @@ -84,13 +98,6 @@ export default function colorContrastEvaluate(node, options, virtualNode) {
}
}

const ptSize = Math.ceil(fontSize * 72) / 96;
const isSmallFont =
(bold && ptSize < boldTextPt) || (!bold && ptSize < largeTextPt);

const { expected, minThreshold, maxThreshold } = isSmallFont
? contrastRatio.normal
: contrastRatio.large;
const isValid = contrast > expected;

// ratio is outside range
Expand Down
19 changes: 14 additions & 5 deletions test/checks/color/color-contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ describe('color-contrast', function() {

assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._data, {
messageKey: 'pseudoContent'
fontSize: '12.0pt (16px)',
fontWeight: 'normal',
messageKey: 'pseudoContent',
expectedContrastRatio: '4.5:1'
});
assert.equal(
checkContext._relatedNodes[0],
Expand All @@ -425,7 +428,10 @@ describe('color-contrast', function() {

assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._data, {
messageKey: 'pseudoContent'
fontSize: '12.0pt (16px)',
fontWeight: 'normal',
messageKey: 'pseudoContent',
expectedContrastRatio: '4.5:1'
});
assert.equal(
checkContext._relatedNodes[0],
Expand All @@ -449,7 +455,10 @@ describe('color-contrast', function() {

assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._data, {
messageKey: 'pseudoContent'
fontSize: '12.0pt (16px)',
fontWeight: 'normal',
messageKey: 'pseudoContent',
expectedContrastRatio: '4.5:1'
});
assert.equal(
checkContext._relatedNodes[0],
Expand Down Expand Up @@ -880,7 +889,7 @@ describe('color-contrast', function() {
assert.equal(checkContext._data.messageKey, 'shadowOnBgColor');
});

it('fails if thick text shadows don\'t have sufficient contrast', function() {
it("fails if thick text shadows don't have sufficient contrast", function() {
straker marked this conversation as resolved.
Show resolved Hide resolved
var params = checkSetup(
'<div id="target" style="background-color: #aaa; color:#666; ' +
'text-shadow: 0 0 0.09em #000, 0 0 0.09em #000, 0 0 0.09em #000;">' +
Expand All @@ -890,7 +899,7 @@ describe('color-contrast', function() {
assert.isTrue(contrastEvaluate.apply(checkContext, params));
});

it('passes if thin text shadows don\'t have sufficient contrast, but the text and background do', function() {
it("passes if thin text shadows don't have sufficient contrast, but the text and background do", function() {
straker marked this conversation as resolved.
Show resolved Hide resolved
var params = checkSetup(
'<div id="target" style="background-color: #aaa; color:#666; ' +
'text-shadow: 0 0 0.09em #000, 0 0 0.09em #000, 0 0 0.09em #000;">' +
Expand Down