-
Notifications
You must be signed in to change notification settings - Fork 742
/
color-contrast.js
400 lines (344 loc) · 15.2 KB
/
color-contrast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
describe('color-contrast', function() {
'use strict';
var fixture = document.getElementById('fixture');
var fixtureSetup = axe.testUtils.fixtureSetup;
var checkSetup = axe.testUtils.checkSetup;
var shadowSupported = axe.testUtils.shadowSupport.v1;
var shadowCheckSetup = axe.testUtils.shadowCheckSetup;
var checkContext = axe.testUtils.MockCheckContext();
var contrastEvaluate = checks['color-contrast'].evaluate;
afterEach(function() {
fixture.innerHTML = '';
checkContext.reset();
axe._tree = undefined;
});
it('should return the proper values stored in data', function() {
var params = checkSetup(
'<div id="parent" style="color: black; background-color: white; font-size: 14pt">' +
'<b id="target">My text</b></div>'
);
var white = new axe.commons.color.Color(255, 255, 255, 1);
var black = new axe.commons.color.Color(0, 0, 0, 1);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.equal(checkContext._data.bgColor, white.toHexString());
assert.equal(checkContext._data.fgColor, black.toHexString());
assert.equal(checkContext._data.contrastRatio, '21.00');
assert.equal(checkContext._data.fontWeight, 'bold');
assert.isAtLeast(parseFloat(checkContext._data.fontSize), 14, 0.5);
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return true when there is sufficient contrast because of bold tag', function() {
var params = checkSetup(
'<div id="parent" style="color: gray; background-color: white; font-size: 14pt">' +
'<b id="target">My text</b></div>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
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>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
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">' +
'My text</div>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return false when there is not sufficient contrast because of font size', function() {
var params = checkSetup(
'<div style="color: gray; background-color: white; font-size: 8pt; -webkit-text-size-adjust: none;" id="target">' +
'My text</div>'
);
assert.isFalse(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, [params[0]]);
});
it('should return true when there is sufficient contrast with explicit transparency', function() {
var params = checkSetup(
'<div id="parent" style="color: white; background-color: white;">' +
'<span style="color: black; background-color: rgba(0,0,0,0)" id="target">My text</span></div>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return true when there is sufficient contrast with implicit transparency', function() {
var params = checkSetup(
'<div id="parent" style="color: white; background-color: white;">' +
'<span style="color: black;" id="target">My text</span></div>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return true when there is sufficient contrast', function() {
var params = checkSetup(
'<div style="color: black; background-color: white;" id="target">' +
'My text</div>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return true for inline elements with sufficient contrast spanning multiple lines', function() {
var params = checkSetup(
'<p>Text oh heyyyy <a href="#" id="target">and here\'s <br>a link</a></p>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return undefined for inline elements spanning multiple lines that are overlapped', function() {
var params = checkSetup(
'<div style="position:relative;"><div style="background-color:rgba(0,0,0,1);position:absolute;width:300px;height:200px;"></div>' +
'<p>Text oh heyyyy <a href="#" id="target">and here\'s <br>a link</a></p></div>'
);
assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return true for inline elements with sufficient contrast', function() {
var params = checkSetup(
'<p>Text oh heyyyy <b id="target">and here\'s bold text</b></p>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return false when there is not sufficient contrast', function() {
var params = checkSetup(
'<div style="color: yellow; background-color: white;" id="target">' +
'My text</div>'
);
assert.isFalse(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, [params[0]]);
});
it('should ignore position:fixed elements above the target', function() {
var params = checkSetup(
'<div style="background-color: #e5f1e5;" id="background">' +
'<div style="width:100%; position:fixed; top:0; height:50px; background: #F0F0F0; z-index: 200; color:#fff" >header</div>' +
'<div style="height: 6000px;"></div>' +
'stuff <span id="target" style="color: rgba(91, 91, 90, 0.7)">This is some text</span>' +
'<div style="height: 6000px;"></div>' +
'</div>'
);
var expectedRelatedNodes = fixture.querySelector('#background');
assert.isFalse(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, [expectedRelatedNodes]);
});
it('should find contrast issues on position:fixed elements', function() {
var params = checkSetup(
'<div style="background-color: #e5f1e5;" id="background">' +
'<div style="width:100%; position:fixed; top:0; height:50px; background: #F0F0F0; z-index: 200; color:#fff" id="target">header</div>' +
'<div style="height: 6000px;"></div>' +
'stuff <span style="color: rgba(91, 91, 90, 0.7)">This is some text</span>' +
'<div style="height: 6000px;"></div>' +
'</div>'
);
assert.isFalse(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, [params[0]]);
});
it('should return undefined for background-image elements', function() {
var dataURI =
'data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/' +
'XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkA' +
'ABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKU' +
'E1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7';
var params = checkSetup(
'<div id="background" style="background:url(' +
dataURI +
') no-repeat left center; padding: 5px 0 5px 25px;">' +
'<p id="target">Text 1</p>' +
'</div>'
);
assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.isUndefined(checkContext._data.bgColor);
assert.equal(checkContext._data.contrastRatio, 0);
assert.equal(checkContext._data.messageKey, 'bgImage');
});
it('should return undefined for background-gradient elements', function() {
var params = checkSetup(
'<div id="background" style="background-image:linear-gradient(red, orange);">' +
'<p id="target">Text 2</p>' +
'</div>'
);
assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.isUndefined(checkContext._data.bgColor);
assert.equal(checkContext._data.messageKey, 'bgGradient');
assert.equal(checkContext._data.contrastRatio, 0);
});
it('should return undefined when there are elements overlapping', function(done) {
// Give Edge time to scroll... :/
setTimeout(function() {
var params = checkSetup(
'<div style="color: black; background-color: white; width: 200px; height: 100px; position: relative;" id="target">' +
'My text <div style="position: absolute; top:0; left: 0; background-color: white; width: 100%; height: 100%;"></div></div>'
);
var result = contrastEvaluate.apply(checkContext, params);
assert.isUndefined(result);
assert.equal(checkContext._data.messageKey, 'bgOverlap');
assert.equal(checkContext._data.contrastRatio, 0);
done();
}, 10);
});
it('should return true when a form wraps mixed content', function() {
var params = checkSetup(
'<form id="target"><p>Some text</p><label for="input6">Text</label><input id="input6"></form>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
});
it('should return true when a label wraps a text input', function() {
fixtureSetup('<label id="target">' + 'My text <input type="text"></label>');
var target = fixture.querySelector('#target');
var virtualNode = axe.utils.getNodeFromTree(target);
var result = contrastEvaluate.call(checkContext, target, {}, virtualNode);
assert.isTrue(result);
});
it("should return true when a label wraps a text input but doesn't overlap", function() {
var params = checkSetup(
'<label id="target">' +
'My text <input type="text" style="position: absolute; top: 200px;"></label>'
);
var result = contrastEvaluate.apply(checkContext, params);
assert.isTrue(result);
});
it('should return true when there is sufficient contrast based on thead', function() {
var params = checkSetup(
'<table><thead style="background: #d00d2c"><tr><th id="target" style="color: #fff; padding: .5em">Col 1</th></tr></thead></table>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return true when there is sufficient contrast based on tbody', function() {
var params = checkSetup(
'<table><tbody style="background: #d00d2c"><tr><td id="target" style="color: #fff; padding: .5em">Col 1</td></tr></tbody></table>'
);
assert.isTrue(contrastEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._relatedNodes, []);
});
it('should return undefined if element overlaps text content', function(done) {
// Give Edge time to scroll
setTimeout(function() {
var params = checkSetup(
'<div style="background-color: white; height: 60px; width: 80px; border:1px solid;position: relative;">' +
'<div id="target" style="color: white; height: 40px; width: 60px; border:1px solid red;">Hi</div>' +
'<div style="position: absolute; top: 0; width: 60px; height: 40px;background-color: #000"></div>' +
'</div>'
);
assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.equal(checkContext._data.messageKey, 'bgOverlap');
assert.equal(checkContext._data.contrastRatio, 0);
done();
}, 10);
});
it('should return undefined if element has same color as background', function() {
var params = checkSetup(
'<div style="background-color: white;">' +
'<div style="color:white;" id="target">Text</div>' +
'</div>'
);
assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.equal(checkContext._data.messageKey, 'equalRatio');
assert.equal(checkContext._data.contrastRatio, 1);
});
it('returns relatedNodes with undefined', function() {
var dataURI =
'data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/' +
'XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkA' +
'ABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKU' +
'E1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7';
var params = checkSetup(
'<div id="background" style="background:url(' +
dataURI +
') no-repeat left center; padding: 5px 0 5px 25px;">' +
'<p id="target">Text 1</p>' +
'</div>'
);
assert.isUndefined(contrastEvaluate.apply(checkContext, params));
assert.equal(
checkContext._relatedNodes[0],
document.querySelector('#background')
);
});
it('should return undefined for a single character text with insufficient contrast', function() {
var params = checkSetup(
'<div style="background-color: #FFF;">' +
'<div style="color:#DDD;" id="target">X</div>' +
'</div>'
);
var actual = contrastEvaluate.apply(checkContext, params);
assert.isUndefined(actual);
assert.equal(checkContext._data.messageKey, 'shortTextContent');
});
it('should return true for a single character text with insufficient contrast', function() {
var params = checkSetup(
'<div style="background-color: #FFF;">' +
'<div style="color:#DDD;" id="target">X</div>' +
'</div>'
);
var actual = contrastEvaluate.apply(checkContext, params);
assert.isUndefined(actual);
assert.equal(checkContext._data.messageKey, 'shortTextContent');
});
it('should return undefined when the text only contains nonBmp unicode by default', function() {
var params = checkSetup(
'<div style="background-color: #FFF;">' +
'<div style="color:#DDD;" id="target">◓</div>' +
'</div>'
);
var actual = contrastEvaluate.apply(checkContext, params);
assert.isUndefined(actual);
assert.equal(checkContext._data.messageKey, 'shortTextContent');
});
it('should return true when the text only contains nonBmp unicode when the ignoreUnicode option is false, and there is sufficient contrast', function() {
var params = checkSetup(
'<div style="background-color: #FFF;">' +
'<div style="color:#000;" id="target">◓</div>' +
'</div>',
{
ignoreUnicode: false
}
);
var actual = contrastEvaluate.apply(checkContext, params);
assert.isTrue(actual);
});
it('should return undefined when the text only contains nonBmp unicode when the ignoreUnicode option is false and the ignoreLength option is default, and there is insufficient contrast', function() {
var params = checkSetup(
'<div style="background-color: #FFF;">' +
'<div style="color:#DDD;" id="target">◓</div>' +
'</div>',
{
ignoreUnicode: false
}
);
var actual = contrastEvaluate.apply(checkContext, params);
assert.isUndefined(actual);
assert.equal(checkContext._data.messageKey, 'shortTextContent');
});
it('should return false when the text only contains nonBmp unicode when the ignoreUnicode option is false and the ignoreLength option is true, and there is insufficient contrast', function() {
var params = checkSetup(
'<div style="background-color: #FFF;">' +
'<div style="color:#DDD;" id="target">◓</div>' +
'</div>',
{
ignoreUnicode: false,
ignoreLength: true
}
);
var actual = contrastEvaluate.apply(checkContext, params);
assert.isFalse(actual);
});
(shadowSupported ? it : xit)(
'returns colors across Shadow DOM boundaries',
function() {
var params = shadowCheckSetup(
'<div id="container" style="background-color:black;"></div>',
'<p style="color: #333;" id="target">Text</p>'
);
var container = fixture.querySelector('#container');
var result = contrastEvaluate.apply(checkContext, params);
assert.isFalse(result);
assert.deepEqual(checkContext._relatedNodes, [container]);
}
);
});