blob: 13c54222f4e789710e583c3253eb9e0822252ca2 (
plain)
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
|
exports.name = "Color use";
exports.description = "Use colors that have sufficient contrast and avoid issues with common forms of color blindness.";
var fc = fiveui.color;
var AA_NORMAL = 4.5;
var AA_BIG = 3;
var AAA_NORMAL = 7;
var AAA_BIG = 4.5;
/**
* Compute the font size in points, assuming 96dpi.
*/
var fontSizePt = function(elt) {
var px = $(elt).css('font-size');
return parseInt(px) * 72 / 96;
};
var isBold = function(elt) {
return $(elt).css('font-weight') == 'bold';
};
/**
* See if the text counts as "large scale" according to the W3C:
* (http://www.w3.org/TR/2008/REC-WCAG20-20081211/#larger-scaledef)
*/
var isLargeScale = function(elt) {
var size = fontSizePt(elt);
var bld = isBold(elt);
return (18 <= size || (14 <= size && bld));
};
var getAARatio = function(elt) {
if (isLargeScale(elt)) {
return AA_BIG;
}
return AA_NORMAL;
};
var getAAARatio = function(elt) {
if (isLargeScale(elt)) {
return AAA_BIG;
}
return AAA_NORMAL;
};
exports.rule = function (report) {
$5('#mw-content-text *')
// filter for lowest level elts having non-empty text
.filter(function () {
var $this = $(this);
return $this.children().length == 0 && $.trim($this.text()).length > 0;
})
.each(function (i,elt) {
var fg = fc.colorToRGB($(elt).css('color'));
var bg = fc.findBGColor($(elt));
if (fg && bg) {
var ratio = fc.contrast(fc.luminance(fg), fc.luminance(bg));
console.log("comparing witch ratio" + ratio);
if (ratio < getAARatio(elt)) {
report.error('Element has poor contrast: ' + ratio +
" ratio should be greater than " + getAARatio(elt), elt);
} else if (ratio < getAAARatio(elt)){
report.warning('Element has poor contrast: ' + ratio +
" ratio should be greater than " + getAAARatio(elt), elt);
}
}
});
};
|