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): parse font-weight value as number (#2031) #2040

Merged
merged 1 commit into from
Feb 12, 2020
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
5 changes: 2 additions & 3 deletions lib/checks/color/color-contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ const fgColor = color.getForegroundColor(node, noScroll, bgColor);

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;
const fontWeight = parseFloat(nodeStyle.getPropertyValue('font-weight'));
const bold = !isNaN(fontWeight) && fontWeight >= 700;

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

Expand Down
14 changes: 12 additions & 2 deletions test/checks/color/color-contrast.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ describe('color-contrast', function() {

it('should return true when there is sufficient contrast because of font weight', function() {
var params = checkSetup(
'<div style="color: gray; background-color: white; font-size: 14pt; font-weight: bold" id="target">' +
'My text</div>'
'<div style="color: gray; background-color: white; font-size: 14pt; font-weight: 900" id="target">' +
'<span style="font-weight:lighter">My text</span></div>'
);

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

it('should return false when there is not sufficient contrast because of font weight', function() {
var params = checkSetup(
'<div style="color: gray; background-color: white; font-size: 14pt; font-weight: 100" id="target">' +
'<span style="font-weight:bolder">My text</span></div>'
);

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

it('should return true when there is sufficient contrast because of font size', function() {
var params = checkSetup(
'<div style="color: gray; background-color: white; font-size: 18pt;" id="target">' +
Expand Down