Skip to content

Commit f649d60

Browse files
committed
Merge branch 'phillbaker-rgba-warnings'
2 parents 690e26d + e9b878b commit f649d60

6 files changed

Lines changed: 102 additions & 20 deletions

File tree

HTMLCS.Util.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,15 @@ _global.HTMLCS.util = function() {
191191
*
192192
* @returns {Object}
193193
*/
194-
self.style = function(element) {
194+
self.style = function(element, pseudo) {
195195
var computedStyle = null;
196196
var window = self.getElementWindow(element);
197+
var pseudo = pseudo || null;
197198

198199
if (element.currentStyle) {
199200
computedStyle = element.currentStyle;
200201
} else if (window.getComputedStyle) {
201-
computedStyle = window.getComputedStyle(element, null);
202+
computedStyle = window.getComputedStyle(element, pseudo);
202203
}
203204

204205
return computedStyle;
@@ -240,7 +241,7 @@ _global.HTMLCS.util = function() {
240241
* Returns true if the element is deliberately hidden from Accessibility APIs using ARIA hidden.
241242
*
242243
* Not: This is separate to isAccessibilityHidden() due to a need to check specifically for aria hidden.
243-
*
244+
*
244245
* @param {Node} element The element to check.
245246
*
246247
* @return {Boolean}
@@ -397,7 +398,7 @@ _global.HTMLCS.util = function() {
397398
* Returns all elements that are visible to the accessibility API.
398399
*
399400
* @param {Node} element The parent element to search.
400-
* @param {String} selector Optional selector to pass to
401+
* @param {String} selector Optional selector to pass to
401402
*
402403
* @return {Array}
403404
*/
@@ -533,10 +534,9 @@ _global.HTMLCS.util = function() {
533534
}
534535

535536
/**
536-
* Convert a colour string to a structure with red/green/blue elements.
537+
* Convert a colour string to a structure with red/green/blue/alpha elements.
537538
*
538-
* Supports rgb() and hex colours (3 or 6 hex digits, optional "#").
539-
* rgba() also supported but the alpha channel is currently ignored.
539+
* Supports rgb() and hex colours (3, 4, 6 or 8 hex digits, optional "#").
540540
* Each red/green/blue element is in the range [0.0, 1.0].
541541
*
542542
* @param {String} colour The colour to convert.
@@ -552,7 +552,11 @@ _global.HTMLCS.util = function() {
552552
colour = {
553553
red: (matches[1] / 255),
554554
green: (matches[2] / 255),
555-
blue: (matches[3] / 255)
555+
blue: (matches[3] / 255),
556+
alpha: 1.0
557+
};
558+
if (matches[4]) {
559+
colour.alpha = parseFloat(/^,\s*(.*)$/.exec(matches[4])[1]);
556560
}
557561
} else {
558562
// Hex digit format.
@@ -564,10 +568,20 @@ _global.HTMLCS.util = function() {
564568
colour = colour.replace(/^(.)(.)(.)$/, '$1$1$2$2$3$3');
565569
}
566570

571+
if (colour.length === 4) {
572+
colour = colour.replace(/^(.)(.)(.)(.)$/, '$1$1$2$2$3$3$4$4');
573+
}
574+
575+
var alpha = 1; // Default if alpha is not specified
576+
if (colour.length === 8) {
577+
alpha = parseInt(colour.substr(6, 2), 16) / 255;
578+
}
579+
567580
colour = {
568581
red: (parseInt(colour.substr(0, 2), 16) / 255),
569582
green: (parseInt(colour.substr(2, 2), 16) / 255),
570-
blue: (parseInt(colour.substr(4, 2), 16) / 255)
583+
blue: (parseInt(colour.substr(4, 2), 16) / 255),
584+
alpha: alpha,
571585
};
572586
}
573587

@@ -1290,4 +1304,4 @@ _global.HTMLCS.util = function() {
12901304
};
12911305

12921306
return self;
1293-
}();
1307+
}();

Standards/WCAG2AAA/Sniffs/Principle1/Guideline1_4/1_4_3.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,22 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3 = {
3939

4040
for (var i = 0; i < failures.length; i++) {
4141
var element = failures[i].element;
42-
42+
4343
var decimals = 2;
4444
var value = (Math.round(failures[i].value * Math.pow(10, decimals)) / Math.pow(10, decimals));
4545
var required = failures[i].required;
4646
var recommend = failures[i].recommendation;
4747
var hasBgImg = failures[i].hasBgImage || false;
4848
var bgColour = failures[i].bgColour || false;
4949
var isAbsolute = failures[i].isAbsolute || false;
50+
var hasAlpha = failures[i].hasAlpha || false;
5051

5152
// If the values would look identical, add decimals to the value.
5253
while (required === value) {
5354
decimals++;
5455
value = (Math.round(failures[i].value * Math.pow(10, decimals)) / Math.pow(10, decimals));
5556
}
56-
57+
5758
if (required === 4.5) {
5859
var code = 'G18';
5960
} else if (required === 3.0) {
@@ -80,6 +81,9 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3 = {
8081
} else if (hasBgImg === true) {
8182
code += '.BgImage';
8283
HTMLCS.addMessage(HTMLCS.WARNING, element, _global.HTMLCS.getTranslation("1_4_3_G18_or_G145.BgImage").replace(/\{\{required\}\}/g, required), code);
84+
} else if (hasAlpha === true) {
85+
code += '.Alpha';
86+
HTMLCS.addMessage(HTMLCS.WARNING, element, _global.HTMLCS.getTranslation("1_4_3_G18_or_G145.Alpha").replace(/\{\{required\}\}/g, required), code);
8387
} else {
8488
code += '.Fail';
8589
HTMLCS.addMessage(HTMLCS.ERROR, element, _global.HTMLCS.getTranslation("1_4_3_G18_or_G145.Fail").replace(/\{\{required\}\}/g, required).replace(/\{\{value\}\}/g, value) + recommendText, code);

Standards/WCAG2AAA/Sniffs/Principle1/Guideline1_4/1_4_3_Contrast.js

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3_Contrast = {
2020
var failures = [];
2121

2222
if (!top.ownerDocument) {
23-
var toProcess = [top.getElementsByTagName('body')[0]];
23+
var toProcess = [];
24+
var body = top.getElementsByTagName('body');
25+
if (body.length) {
26+
// SVG objects will not have a body element. Don't check them.
27+
var toProcess = [body[0]];
28+
}
2429
} else {
2530
var toProcess = [top];
2631
}
@@ -52,11 +57,11 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3_Contrast = {
5257
var bgElement = node;
5358
var hasBgImg = false;
5459
var isAbsolute = false;
55-
56-
if (style.backgroundImage !== 'none') {
60+
61+
if (style.backgroundImage !== 'none') {
5762
hasBgImg = true;
5863
}
59-
64+
6065
if (style.position == 'absolute') {
6166
isAbsolute = true;
6267
}
@@ -86,22 +91,67 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3_Contrast = {
8691

8792
var parentStyle = HTMLCS.util.style(parent);
8893
var bgColour = parentStyle.backgroundColor;
94+
var bgElement = parent;
8995
if (parentStyle.backgroundImage !== 'none') {
9096
hasBgImg = true;
9197
}
9298
if (parentStyle.position == 'absolute') {
9399
isAbsolute = true;
94100
}
95101

102+
// Search for the smooth scrolling willChange: 'transform' background hack
103+
// See http://fourkitchens.com/blog/article/fix-scrolling-performance-css-will-change-property
104+
var beforeStyle = HTMLCS.util.style(parent, ':before');
105+
if (
106+
beforeStyle
107+
&& beforeStyle.position == 'fixed'
108+
&& beforeStyle.willChange == 'transform'
109+
//Make sure it is trying to cover the entire content area
110+
&& beforeStyle.width == parentStyle.width
111+
&& parseInt(beforeStyle.height, 10) <= parseInt(parentStyle.height, 10)
112+
//And finally it needs a background image
113+
&& beforeStyle.backgroundImage !== 'none'
114+
) {
115+
hasBgImg = true;
116+
break;
117+
}
118+
96119
parent = parent.parentNode;
97120
}//end while
98121

99-
if (hasBgImg === true) {
122+
var bgAlpha = HTMLCS.util.colourStrToRGB(bgColour).alpha;
123+
var fgAlpha = HTMLCS.util.colourStrToRGB(foreColour).alpha;
124+
125+
if (bgColour && bgAlpha < 1.0 && bgAlpha > 0) {
126+
// If we have a rgba background colour, skip the contrast ratio checks,
127+
// and push a warning instead.
128+
failures.push({
129+
element: node,
130+
colour: foreColour,
131+
bgColour: bgColour,
132+
value: undefined,
133+
required: reqRatio,
134+
hasAlpha: true,
135+
});
136+
continue;
137+
} else if (foreColour && fgAlpha < 1.0 && fgAlpha > 0) {
138+
// If we have a rgba fore colour, skip the contrast ratio checks,
139+
// and push a warning instead.
140+
failures.push({
141+
element: node,
142+
colour: foreColour,
143+
bgColour: foreColour,
144+
value: undefined,
145+
required: reqRatio,
146+
hasAlpha: true
147+
});
148+
continue;
149+
} else if (hasBgImg === true) {
100150
// If we have a background image, skip the contrast ratio checks,
101151
// and push a warning instead.
102152
failures.push({
103153
element: node,
104-
colour: style.color,
154+
colour: foreColour,
105155
bgColour: undefined,
106156
value: undefined,
107157
required: reqRatio,
@@ -125,9 +175,10 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_4_1_4_3_Contrast = {
125175
continue;
126176
}
127177

128-
var contrastRatio = HTMLCS.util.contrastRatio(bgColour, style.color);
178+
var contrastRatio = HTMLCS.util.contrastRatio(bgColour, foreColour);
179+
129180
if (contrastRatio < reqRatio) {
130-
var recommendation = this.recommendColour(bgColour, style.color, reqRatio);
181+
var recommendation = this.recommendColour(bgColour, foreColour, reqRatio);
131182

132183
failures.push({
133184
element: node,

Tests/WCAG2/1_4_3_Contrast.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Standard: WCAG2AA
88
Assert: Error *.G18.Fail on #basicContrastFail
99
Assert: No Error *.G18 on #issue155
10+
Assert: Warning *.G145.Alpha on #rgba
11+
Assert: Warning *.G18.Alpha on #rgba2
1012
-->
1113
</head>
1214
<body>
@@ -15,6 +17,15 @@
1517

1618
<p>Sample: <span id="issue155" style="color: #7c7cff; background-color: #ffffff; font-size: 14pt; font-weight: bold;">I am 14pt bold text and should pass</span></p>
1719

20+
<div>Warning: <div id="pr134" style="color: #7c7cff; background-color: #0000000C; font-size: 14pt; font-weight: bold;">I am 14pt bold text on a transparent background and should generate a warning</div></div>
21+
22+
<div>Warning RGBA: <div id="rgba" style="color: #7c7cff; background-color: rgba(0,0,0,0.5); font-size: 14pt; font-weight: bold;">I am 14pt bold text on a transparent background and should generate a warning</div></div>
23+
24+
<div style="background-color: #ccc">
25+
<span>Warning RBGA in BG</span>
26+
<div id="rgba2" style="background-color: rgba(0,0,0,0.2);">Text</div>
27+
</div>
28+
1829
</body>
1930
</html>
2031

Translations/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ _global.translation['en'] = {
177177
//1_4_3.js
178178
,"1_4_3_G18_or_G145.Abs" : 'This element is absolutely positioned and the background color can not be determined. Ensure the contrast ratio between the text and all covered parts of the background are at least {{required}}:1.'
179179
,"1_4_3_G18_or_G145.BgImage" : 'This element\'s text is placed on a background image. Ensure the contrast ratio between the text and all covered parts of the image are at least {{required}}:1.'
180+
,"1_4_3_G18_or_G145.Alpha" : 'This element\'s text or background contains transparency. Ensure the contrast ratio between the text and background are at least {{required}}:1.'
180181
,"1_4_3_G18_or_G145.Fail" : 'This element has insufficient contrast at this conformance level. Expected a contrast ratio of at least {{required}}:1, but text in this element has a contrast ratio of {{value}}:1.'
181182
,"1_4_3_G18_or_G145.Fail.Recomendation" : 'Recommendation: change'
182183
,"1_4_3_G18_or_G145.Fail.Recomendation.Text" : 'text colour to'

Translations/pl.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ _global.translation['pl'] = {
177177
//1_4_3.js
178178
,"1_4_3_G18_or_G145.Abs" : 'Ten element jest pozycjonowany absolutnie i nie jest możliwe zweryfikowanie jego kontrastu. Sprawdź samodzielnie czy kontrast jest na wystarczającym poziomie: {{required}}:1.'
179179
,"1_4_3_G18_or_G145.BgImage" : 'Tekst elementu wyświetlany jest na obrazku. Należy upewnić się, że stosunek kontrastu tekstu do tła wynosi co najmniej {{required}}:1.'
180+
,"1_4_3_G18_or_G145.Alpha" : 'Tekst lub tło tego elementu jest przezroczyste. Upewnij się, że współczynnik kontrastu między tekstem a tłem wynosi co najmniej {{required}}:1.'
180181
,"1_4_3_G18_or_G145.Fail" : 'Ten element ma niewystarczający na tym poziomie zgodności stosunek kontrastu tekstu do tła. Powinien wynosić co najmniej {{required}}:1, a tekst umieszczony w tym elemencie posiada stosunek {{value}}:1.'
181182
,"1_4_3_G18_or_G145.Fail.Recomendation" : 'Zalecenie: zmiana'
182183
,"1_4_3_G18_or_G145.Fail.Recomendation.Text" : 'koloru tekstu na'

0 commit comments

Comments
 (0)