Skip to content

Commit 9cc55f6

Browse files
committed
Add validation also to text field
1 parent 92e98e5 commit 9cc55f6

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

biome.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"ignore": [],
1111
"include": [
1212
"media/commitfest/js/commitfest.js",
13+
"media/commitfest/js/change_tag.js",
1314
"media/commitfest/css/commitfest.css",
1415
"biome.json"
1516
]

media/commitfest/js/change_tag.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
11
// An input validator for the color picker. Points out low-contrast tag color
22
// choices.
3-
const input = document.getElementById("id_color");
4-
input.addEventListener("input", (event) => {
5-
// Don't do anything if the color code doesn't pass default validity.
6-
input.setCustomValidity("");
7-
if (!input.validity.valid) {
8-
return;
9-
}
3+
const inputs = document.getElementsByClassName("color-picker");
4+
for (let i = 0; i < inputs.length; i++) {
5+
inputs[i].addEventListener("change", (event) => {
6+
// Don't do anything if the color code doesn't pass default validity.
7+
const element = event.target;
8+
element.setCustomValidity("");
9+
if (!element.validity.valid) {
10+
return;
11+
}
1012

11-
// Break the #rrggbb color code into RGB components.
12-
color = parseInt(input.value.substr(1), 16);
13-
red = ((color & 0xFF0000) >> 16) / 255.;
14-
green = ((color & 0x00FF00) >> 8) / 255.;
15-
blue = (color & 0x0000FF) / 255.;
13+
// Break the #rrggbb color code into RGB components.
14+
color = parseInt(element.value.substr(1), 16);
15+
red = ((color & 0xff0000) >> 16) / 255;
16+
green = ((color & 0x00ff00) >> 8) / 255;
17+
blue = (color & 0x0000ff) / 255;
1618

17-
// Compare the contrast ratio against white. All the magic math comes from
18-
// Web Content Accessibility Guidelines (WCAG) 2.2, Technique G18:
19-
//
20-
// https://www.w3.org/WAI/WCAG22/Techniques/general/G18.html
21-
//
22-
function l(val) {
23-
if (val <= 0.04045) {
24-
return val / 12.92;
25-
}
26-
return ((val + 0.055) / 1.055) ** 2.4;
27-
}
19+
// Compare the contrast ratio against white. All the magic math comes from
20+
// Web Content Accessibility Guidelines (WCAG) 2.2, Technique G18:
21+
//
22+
// https://www.w3.org/WAI/WCAG22/Techniques/general/G18.html
23+
//
24+
function l(val) {
25+
if (val <= 0.04045) {
26+
return val / 12.92;
27+
}
28+
return ((val + 0.055) / 1.055) ** 2.4;
29+
}
2830

29-
lum = 0.2126 * l(red) + 0.7152 * l(green) + 0.0722 * l(blue);
30-
contrast = (1 + 0.05) / (lum + 0.05);
31+
lum = 0.2126 * l(red) + 0.7152 * l(green) + 0.0722 * l(blue);
32+
contrast = (1 + 0.05) / (lum + 0.05);
3133

32-
// Complain if we're below WCAG 2.2 recommendations.
33-
if (contrast < 4.5) {
34-
input.setCustomValidity(
35-
"Consider choosing a darker color. "
36-
+ "(Tag text is small and white.)\n\n"
37-
+ "Contrast ratio: " + (Math.trunc(contrast * 10) / 10) + " (< 4.5)"
38-
);
34+
// Complain if we're below WCAG 2.2 recommendations.
35+
if (contrast < 4.5) {
36+
element.setCustomValidity(
37+
"Consider choosing a darker color. " +
38+
"(Tag text is small and white.)\n\n" +
39+
"Contrast ratio: " +
40+
Math.trunc(contrast * 10) / 10 +
41+
" (< 4.5)",
42+
);
3943

40-
// The admin form uses novalidate, so manually display the browser's
41-
// validity popup. (The user can still ignore it if desired.)
42-
input.reportValidity();
43-
}
44-
});
44+
// The admin form uses novalidate, so manually display the browser's
45+
// validity popup. (The user can still ignore it if desired.)
46+
element.reportValidity();
47+
}
48+
});
49+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<input type="color" name="{{ widget.name }}" onchange="this.nextElementSibling.value = this.value" {% if widget.value != None %} value="{{ widget.value|stringformat:'s' }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} >
2-
<input type="text" minlength="7" maxlength="7" onchange="this.previousElementSibling.value = this.value" {% if widget.value != None %} value="{{ widget.value|stringformat:'s' }}"{% endif %}>
1+
<input type="color" class="color-picker" name="{{ widget.name }}" onchange="this.nextElementSibling.value = this.value" {% if widget.value != None %} value="{{ widget.value|stringformat:'s' }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} >
2+
<input type="text" class="color-picker" minlength="7" maxlength="7" onchange="this.previousElementSibling.value = this.value" {% if widget.value != None %} value="{{ widget.value|stringformat:'s' }}"{% endif %}>

0 commit comments

Comments
 (0)