@stackoverflow/stacks 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,220 +0,0 @@
1
- import { calcAPCA } from "apca-w3";
2
- import axe from "axe-core";
3
- import type { Check, Rule } from "axe-core";
4
-
5
- // Augment Axe types to include the color utilities we use in this file
6
- // https://github.com/dequelabs/axe-core/blob/develop/lib/commons/color/color.js
7
- type Color = {
8
- red: number;
9
- green: number;
10
- blue: number;
11
- alpha: number;
12
- toHexString: () => string;
13
- };
14
-
15
- type ConformanceLevel = "bronze" | "silver" | "custom";
16
-
17
- type ConformanceThresholdFn = (
18
- fontSize: string,
19
- fontWeight: string
20
- ) => number | null;
21
-
22
- declare module "axe-core" {
23
- const commons: {
24
- color: {
25
- getForegroundColor: (
26
- node: HTMLElement,
27
- _: unknown,
28
- bgColor: Color | null
29
- ) => Color | null;
30
- getBackgroundColor: (node: HTMLElement) => Color | null;
31
- };
32
- };
33
- }
34
-
35
- const getAPCASilverPlusThreshold = (
36
- fontSize: string,
37
- fontWeight: string
38
- ): number | null => {
39
- const silverPlusAPCALookupTable = [
40
- // See https://readtech.org/ARC/tests/visual-readability-contrast/?tn=criterion (May 22, 2022)
41
- // font size in px | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 weights
42
- [10, -1, -1, -1, -1, -1, -1, -1, -1, -1],
43
- [12, -1, -1, -1, -1, -1, -1, -1, -1, -1],
44
- [14, -1, -1, -1, 100, 100, 90, 75, -1, -1],
45
- [15, -1, -1, -1, 100, 100, 90, 70, -1, -1],
46
- [16, -1, -1, -1, 90, 75, 70, 60, 60, -1],
47
- [18, -1, -1, 100, 75, 70, 60, 55, 55, 55],
48
- [21, -1, -1, 90, 70, 60, 55, 50, 50, 50],
49
- [24, -1, -1, 75, 60, 55, 50, 45, 45, 45],
50
- [28, -1, 100, 70, 55, 50, 45, 43, 43, 43],
51
- [32, -1, 90, 65, 50, 45, 43, 40, 40, 40],
52
- [36, -1, 75, 60, 45, 43, 40, 38, 38, 38],
53
- [48, 90, 60, 55, 43, 40, 38, 35, 35, 35],
54
- [60, 75, 55, 50, 40, 38, 35, 33, 33, 33],
55
- [72, 60, 50, 45, 38, 35, 33, 30, 30, 30],
56
- [96, 50, 45, 40, 35, 33, 30, 25, 25, 25],
57
- ];
58
-
59
- const size = parseFloat(fontSize);
60
- const weight = parseFloat(fontWeight);
61
-
62
- // Go over the table backwards to find the first matching font size and then the weight.
63
- // The value null is returned when the combination of font size and weight does not have
64
- // any elegible APCA luminosity silver compliant thresholds (represented by -1 in the table).
65
- silverPlusAPCALookupTable.reverse();
66
- for (const [rowSize, ...rowWeights] of silverPlusAPCALookupTable) {
67
- if (size >= rowSize) {
68
- for (const [idx, keywordWeight] of [
69
- 900, 800, 700, 600, 500, 400, 300, 200, 100,
70
- ].entries()) {
71
- if (weight >= keywordWeight) {
72
- const threshold = rowWeights[rowWeights.length - 1 - idx];
73
- return threshold === -1 ? null : threshold;
74
- }
75
- }
76
- }
77
- }
78
-
79
- return null;
80
- };
81
-
82
- const getAPCABronzeThreshold = (fontSize: string): number | null => {
83
- const size = parseFloat(fontSize);
84
-
85
- // APCA Bronze Level Conformance
86
- // https://readtech.org/ARC/tests/visual-readability-contrast/?tn=criterion
87
- switch (true) {
88
- case size >= 32:
89
- return 45;
90
- case size >= 16:
91
- return 60;
92
- default:
93
- return 75;
94
- }
95
- };
96
-
97
- const generateColorContrastAPCAConformanceCheck = (
98
- conformanceLevel: string,
99
- conformanceThresholdFn: ConformanceThresholdFn
100
- ): Check => ({
101
- id: `color-contrast-apca-${conformanceLevel}-conformance`,
102
- metadata: {
103
- impact: "serious",
104
- messages: {
105
- pass:
106
- "Element has sufficient APCA " +
107
- conformanceLevel +
108
- " level lightness contrast (Lc) of ${data.apcaContrast}Lc (foreground color: ${data.fgColor}, background color: ${data.bgColor}, font size: ${data.fontSize}, font weight: ${data.fontWeight}). Expected minimum APCA contrast of ${data.apcaThreshold}}",
109
- fail: {
110
- default:
111
- "Element has insufficient APCA " +
112
- conformanceLevel +
113
- " level contrast of ${data.apcaContrast}Lc (foreground color: ${data.fgColor}, background color: ${data.bgColor}, font size: ${data.fontSize}, font weight: ${data.fontWeight}). Expected minimum APCA lightness contrast of ${data.apcaThreshold}Lc",
114
- increaseFont:
115
- "Element has insufficient APCA " +
116
- conformanceLevel +
117
- " level contrast of ${data.apcaContrast}Lc (foreground color: ${data.fgColor}, background color: ${data.bgColor}, font size: ${data.fontSize}, font weight: ${data.fontWeight}). Increase font size and/or font weight to meet APCA conformance minimums",
118
- },
119
- incomplete: "Unable to determine APCA lightness contrast (Lc)",
120
- },
121
- },
122
- evaluate: function (node: HTMLElement) {
123
- const nodeStyle = window.getComputedStyle(node);
124
- const fontSize = nodeStyle.getPropertyValue("font-size");
125
- const fontWeight = nodeStyle.getPropertyValue("font-weight");
126
-
127
- const bgColor: Color | null =
128
- axe.commons.color.getBackgroundColor(node);
129
- const fgColor: Color | null = axe.commons.color.getForegroundColor(
130
- node,
131
- false,
132
- bgColor
133
- );
134
-
135
- // missing data to determine APCA contrast for this node
136
- if (!bgColor || !fgColor || !fontSize || !fontWeight) {
137
- return undefined;
138
- }
139
-
140
- const toRGBA = (color: Color) => {
141
- return `rgba(${color.red}, ${color.green}, ${color.blue}, ${color.alpha})`;
142
- };
143
-
144
- const apcaContrast = Math.abs(
145
- calcAPCA(toRGBA(fgColor), toRGBA(bgColor))
146
- );
147
- const apcaThreshold = conformanceThresholdFn(fontSize, fontWeight);
148
-
149
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
150
- // @ts-ignore
151
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
152
- this.data({
153
- fgColor: fgColor.toHexString(),
154
- bgColor: bgColor.toHexString(),
155
- fontSize: `${((parseFloat(fontSize) * 72) / 96).toFixed(
156
- 1
157
- )}pt (${fontSize}px)`,
158
- fontWeight: fontWeight,
159
- apcaContrast: Math.round(apcaContrast * 100) / 100,
160
- apcaThreshold: apcaThreshold,
161
- messageKey: apcaThreshold === null ? "increaseFont" : "default",
162
- });
163
-
164
- return apcaThreshold ? apcaContrast >= apcaThreshold : false;
165
- },
166
- });
167
-
168
- const generateColorContrastAPCARule = (conformanceLevel: string): Rule => ({
169
- id: `color-contrast-apca-${conformanceLevel}`,
170
- impact: "serious",
171
- matches: "color-contrast-matches",
172
- metadata: {
173
- description: `Ensures the contrast between foreground and background colors meets APCA ${conformanceLevel} level conformance minimums thresholds`,
174
- tags: ["apca", "wcag3", `apca-${conformanceLevel}`],
175
- help: "Elements must meet APCA conformance minimums thresholds",
176
- helpUrl:
177
- "https://readtech.org/ARC/tests/visual-readability-contrast/?tn=criterion",
178
- },
179
- all: [`color-contrast-apca-${conformanceLevel}-conformance`],
180
- tags: ["apca", "wcag3", `apca-${conformanceLevel}`],
181
- });
182
-
183
- const colorContrastAPCASilverConformanceCheck =
184
- generateColorContrastAPCAConformanceCheck(
185
- "silver",
186
- getAPCASilverPlusThreshold
187
- );
188
-
189
- const colorContrastAPCABronzeConformanceCheck =
190
- generateColorContrastAPCAConformanceCheck("bronze", getAPCABronzeThreshold);
191
-
192
- const colorContrastAPCASilverRule = generateColorContrastAPCARule("silver");
193
- const colorContrastAPCABronzeRule = generateColorContrastAPCARule("bronze");
194
-
195
- const registerAxeAPCA = (
196
- conformanceLevel: ConformanceLevel,
197
- customConformanceThresholdFn?: ConformanceThresholdFn
198
- ) => {
199
- axe.configure({
200
- rules: [generateColorContrastAPCARule(conformanceLevel)],
201
- checks: [
202
- generateColorContrastAPCAConformanceCheck(
203
- conformanceLevel,
204
- customConformanceThresholdFn ||
205
- (conformanceLevel === "silver"
206
- ? getAPCASilverPlusThreshold
207
- : getAPCABronzeThreshold)
208
- ),
209
- ],
210
- });
211
- };
212
-
213
- export {
214
- colorContrastAPCASilverRule,
215
- colorContrastAPCASilverConformanceCheck,
216
- colorContrastAPCABronzeRule,
217
- colorContrastAPCABronzeConformanceCheck,
218
- };
219
-
220
- export default registerAxeAPCA;