Skip to content
This repository was archived by the owner on Dec 7, 2022. It is now read-only.

Commit e69a420

Browse files
author
Alice Boxhall
committed
Add more tests for color contrast; switch to rounding to nearest 0.1 instead of using a tolerance value
1 parent baa436f commit e69a420

3 files changed

Lines changed: 59 additions & 9 deletions

File tree

src/audits/LowContrast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ axs.AuditRule.specs.lowContrastElements = {
3333
var style = window.getComputedStyle(element, null);
3434
var contrastRatio =
3535
axs.utils.getContrastRatioForElementWithComputedStyle(style, element);
36-
return (contrastRatio && axs.utils.isLowContrast(contrastRatio, style, false, 0.02));
36+
return (contrastRatio && axs.utils.isLowContrast(contrastRatio, style, false));
3737
},
3838
code: 'AX_COLOR_01'
3939
};

src/js/AccessibilityUtils.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -817,18 +817,17 @@ axs.utils.isNativeTextElement = function(element) {
817817
* @param {number} contrastRatio
818818
* @param {CSSStyleDeclaration} style
819819
* @param {boolean=} opt_strict Whether to use AA (false) or AAA (true) level
820-
* @param {number=} opt_tolerance The amount by which the contrast ratio can
821-
* acceptably be less than the recommended values.
822820
* @return {boolean}
823821
*/
824-
axs.utils.isLowContrast = function(contrastRatio, style, opt_strict, opt_tolerance) {
825-
var tolerance = opt_tolerance || 0;
822+
axs.utils.isLowContrast = function(contrastRatio, style, opt_strict) {
823+
// Round to nearest 0.1
824+
var roundedContrastRatio = (Math.round(contrastRatio * 10) / 10);
826825
if (!opt_strict) {
827-
return contrastRatio + tolerance < 3.0 ||
828-
(!axs.utils.isLargeFont(style) && contrastRatio + tolerance < 4.5);
826+
return roundedContrastRatio < 3.0 ||
827+
(!axs.utils.isLargeFont(style) && roundedContrastRatio < 4.5);
829828
} else {
830-
return contrastRatio + tolerance < 4.5 ||
831-
(!axs.utils.isLargeFont(style) && contrastRatio + tolerance < 7.0);
829+
return roundedContrastRatio < 4.5 ||
830+
(!axs.utils.isLargeFont(style) && roundedContrastRatio < 7.0);
832831
}
833832
};
834833

test/audits/low-contrast-test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
module("LowContrast");
22

3+
test("No text = no relevant elements", function() {
4+
var fixture = document.getElementById('qunit-fixture');
5+
var div = document.createElement('div');
6+
div.style.backgroundColor = 'white';
7+
div.style.color = 'white';
8+
fixture.appendChild(div);
9+
deepEqual(
10+
axs.AuditRules.getRule('lowContrastElements').run([], fixture),
11+
{ result: axs.constants.AuditResult.NA }
12+
);
13+
});
14+
15+
test("Black on white = no problem", function() {
16+
var fixture = document.getElementById('qunit-fixture');
17+
var div = document.createElement('div');
18+
div.style.backgroundColor = 'white';
19+
div.style.color = 'black';
20+
div.textContent = 'Some text';
21+
fixture.appendChild(div);
22+
deepEqual(
23+
axs.AuditRules.getRule('lowContrastElements').run([], fixture),
24+
{ elements: [], result: axs.constants.AuditResult.PASS }
25+
);
26+
});
27+
28+
test("Low contrast = fail", function() {
29+
var fixture = document.getElementById('qunit-fixture');
30+
var div = document.createElement('div');
31+
div.style.backgroundColor = 'white';
32+
div.style.color = '#aaa'; // Contrast ratio = 2.32
33+
div.textContent = 'Some text';
34+
fixture.appendChild(div);
35+
deepEqual(
36+
axs.AuditRules.getRule('lowContrastElements').run([], fixture),
37+
{ elements: [div], result: axs.constants.AuditResult.FAIL }
38+
);
39+
});
40+
341
test("Opacity is handled", function() {
442
// Setup fixture
543
var fixtures = document.getElementById('qunit-fixture');
@@ -12,3 +50,16 @@ test("Opacity is handled", function() {
1250
{ elements: [elementWithOpacity], result: axs.constants.AuditResult.FAIL }
1351
);
1452
});
53+
54+
test("Uses tolerance value", function() {
55+
var fixture = document.getElementById('qunit-fixture');
56+
var div = document.createElement('div');
57+
div.style.backgroundColor = 'white';
58+
div.style.color = '#777'; // Contrast ratio = 4.48
59+
div.textContent = 'Some text';
60+
fixture.appendChild(div);
61+
deepEqual(
62+
axs.AuditRules.getRule('lowContrastElements').run([], fixture),
63+
{ elements: [], result: axs.constants.AuditResult.PASS }
64+
);
65+
});

0 commit comments

Comments
 (0)