auditmysite 1.7.3

Resource-efficient WCAG 2.2 AA Accessibility Checker in Rust
Documentation
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
401
402
403
404
use crate::audit::normalized::{NormalizedReport, SeverityCounts};
use crate::output::report_model::{
    CriticalityTier, DiagnosisBlock, FindingCluster, FindingCriticalityGroup, FindingGroup,
    FindingSeverityTier, FindingSummary,
};

pub(super) fn build_finding_summary(
    locale: &str,
    counts: &SeverityCounts,
    audit_summary: &crate::audit::summary::AuditSummary,
) -> FindingSummary {
    let en = locale == "en";
    let cross_impact_notes = audit_summary
        .cross_impacts
        .iter()
        .map(|c| format!("{}: {}", c.dimensions, c.description))
        .collect();
    let issue_pattern_label = match &audit_summary.issue_pattern {
        crate::audit::summary::IssuePattern::Minimal => {
            if en { "No findings" } else { "Keine Befunde" }.into()
        }
        crate::audit::summary::IssuePattern::SingleDominant => if en {
            "Single dominant issue"
        } else {
            "Einzelnes dominantes Problem"
        }
        .into(),
        crate::audit::summary::IssuePattern::Clustered => if en {
            "Clustered problems"
        } else {
            "Geclusterte Probleme"
        }
        .into(),
        crate::audit::summary::IssuePattern::Scattered => if en {
            "Scattered issues"
        } else {
            "Verteilte Einzelprobleme"
        }
        .into(),
    };
    FindingSummary {
        total: counts.total,
        critical: counts.critical,
        high: counts.high,
        medium: counts.medium,
        low: counts.low,
        verdict: audit_summary.verdict_intro.clone(),
        dominant_issue_note: audit_summary.dominant_issue_note.clone(),
        cross_impact_notes,
        issue_pattern_label,
    }
}

pub(super) fn build_severity_tiers(
    locale: &str,
    findings: &[FindingGroup],
) -> Vec<FindingSeverityTier> {
    use crate::wcag::Severity;

    let en = locale == "en";
    [
        (Severity::Critical, if en { "Critical" } else { "Kritisch" }),
        (Severity::High, if en { "High" } else { "Hoch" }),
        (Severity::Medium, if en { "Medium" } else { "Mittel" }),
        (Severity::Low, if en { "Low" } else { "Niedrig" }),
    ]
    .iter()
    .filter_map(|(sev, label)| {
        let tier_findings: Vec<FindingGroup> = findings
            .iter()
            .filter(|f| f.severity == *sev)
            .cloned()
            .collect();
        if tier_findings.is_empty() {
            return None;
        }
        let total_occurrences = tier_findings.iter().map(|f| f.occurrence_count).sum();
        Some(FindingSeverityTier {
            severity: *sev,
            label: (*label).to_string(),
            findings: tier_findings,
            total_occurrences,
        })
    })
    .collect()
}

pub(super) fn build_criticality_groups(
    locale: &str,
    findings: &[FindingGroup],
) -> Vec<FindingCriticalityGroup> {
    let en = locale == "en";

    [CriticalityTier::Mandatory, CriticalityTier::Optimization]
        .iter()
        .filter_map(|tier| {
            let tier_findings: Vec<FindingGroup> = findings
                .iter()
                .filter(|f| f.criticality_tier == *tier)
                .cloned()
                .collect();
            if tier_findings.is_empty() {
                return None;
            }
            let by_severity = build_severity_tiers(locale, &tier_findings);
            let total_findings = tier_findings.len();
            let total_occurrences = tier_findings.iter().map(|f| f.occurrence_count).sum();
            let (label, eyebrow, intro) = match tier {
                CriticalityTier::Mandatory => {
                    if en {
                        (
                            "Legally relevant barriers (WCAG A/AA)".to_string(),
                            "SECTION 1 · WCAG COMPLIANCE".to_string(),
                            "Accessibility issues under WCAG Level A/AA. Remediating these issues is recommended for compliance with legal standards (like BFSG).".to_string(),
                        )
                    } else {
                        (
                            "Gesetzlich relevante Barrieren (WCAG A/AA)".to_string(),
                            "BEREICH 1 · WCAG-KONFORMITÄT".to_string(),
                            "Barrierefreiheits-Verstöße nach WCAG Level A/AA. Diese Befunde beschreiben Barrieren, deren Behebung für die Erfüllung gesetzlicher Vorgaben (wie dem BFSG) empfohlen ist.".to_string(),
                        )
                    }
                }
                CriticalityTier::Optimization => {
                    if en {
                        (
                            "Additional quality & SEO recommendations".to_string(),
                            "SECTION 2 · OPTIMIZATION".to_string(),
                            "SEO, AI visibility, UX signals, dark mode and further quality benchmarks. These improve findability and user experience.".to_string(),
                        )
                    } else {
                        (
                            "Ergänzende Qualitäts- & SEO-Empfehlungen".to_string(),
                            "BEREICH 2 · OPTIMIERUNG".to_string(),
                            "Suchmaschinenoptimierung (SEO), KI-Sichtbarkeit, UX-Signale, Dark-Mode-Unterstützung und weitere Qualitätskriterien. Diese verbessern die Auffindbarkeit und das allgemeine Nutzererlebnis.".to_string(),
                        )
                    }
                }
            };
            Some(FindingCriticalityGroup {
                tier: *tier,
                label,
                eyebrow,
                intro,
                by_severity,
                total_findings,
                total_occurrences,
            })
        })
        .collect()
}

pub(super) fn build_thematic_clusters(
    locale: &str,
    findings: &[FindingGroup],
) -> Vec<FindingCluster> {
    use std::collections::BTreeMap;

    let en = locale == "en";
    let mut by_dimension: BTreeMap<String, Vec<&FindingGroup>> = BTreeMap::new();
    for f in findings {
        let dim = f.dimension.as_deref().unwrap_or("Accessibility");
        by_dimension.entry(dim.to_string()).or_default().push(f);
    }

    let mut clusters: Vec<FindingCluster> = by_dimension
        .into_iter()
        .filter(|(_, groups)| groups.len() >= 2)
        .map(|(dimension, groups)| {
            let worst_severity = groups
                .iter()
                .map(|g| g.severity)
                .max()
                .unwrap_or(crate::wcag::Severity::Low);
            let occurrence_total: usize = groups.iter().map(|g| g.occurrence_count).sum();
            let label = dimension_cluster_label(en, &dimension, groups.len());
            let severity_label = match worst_severity {
                crate::wcag::Severity::Critical => {
                    if en { "Critical" } else { "Kritisch" }.to_string()
                }
                crate::wcag::Severity::High => if en { "High" } else { "Hoch" }.to_string(),
                crate::wcag::Severity::Medium => if en { "Medium" } else { "Mittel" }.to_string(),
                crate::wcag::Severity::Low => if en { "Low" } else { "Niedrig" }.to_string(),
            };
            FindingCluster {
                label,
                dimension: dimension.clone(),
                finding_count: groups.len(),
                occurrence_total,
                severity_label,
                finding_titles: groups.iter().map(|g| g.title.clone()).collect(),
            }
        })
        .collect();

    clusters.sort_by_key(|c| std::cmp::Reverse(c.occurrence_total));
    clusters
}

fn dimension_cluster_label(en: bool, dimension: &str, count: usize) -> String {
    let count_label = if en {
        format!("{count} findings")
    } else {
        format!("{count} Befunde")
    };
    match dimension {
        "Accessibility" => {
            if en {
                format!("Accessibility barriers ({count_label})")
            } else {
                format!("Barrierefreiheits-Barrieren ({count_label})")
            }
        }
        "SEO" => {
            if en {
                format!("SEO issues ({count_label})")
            } else {
                format!("SEO-Probleme ({count_label})")
            }
        }
        "Performance" => {
            if en {
                format!("Performance issues ({count_label})")
            } else {
                format!("Performance-Probleme ({count_label})")
            }
        }
        "Security" => {
            if en {
                format!("Security gaps ({count_label})")
            } else {
                format!("Sicherheitslücken ({count_label})")
            }
        }
        "Mobile" => {
            if en {
                format!("Mobile issues ({count_label})")
            } else {
                format!("Mobile-Probleme ({count_label})")
            }
        }
        other => format!("{other} ({count_label})"),
    }
}

pub(super) fn build_diagnosis_block(
    locale: &str,
    normalized: &NormalizedReport,
    audit_summary: &crate::audit::summary::AuditSummary,
) -> DiagnosisBlock {
    use std::collections::BTreeMap;
    let en = locale == "en";

    let section_title = if en {
        "System diagnosis".to_string()
    } else {
        "Systemdiagnose".to_string()
    };

    let (pattern_label, pattern_description) = match &audit_summary.issue_pattern {
        crate::audit::summary::IssuePattern::Minimal => (
            if en { "No findings" } else { "Keine Befunde" }.to_string(),
            if en {
                "No measurable accessibility barriers detected."
            } else {
                "Keine messbaren Barrierefreiheits-Barrieren erkannt."
            }
            .to_string(),
        ),
        crate::audit::summary::IssuePattern::SingleDominant => (
            if en {
                "Single dominant problem"
            } else {
                "Einzelnes dominantes Problem"
            }
            .to_string(),
            if en {
                "One rule type accounts for the majority of all critical/high findings. \
                 Fixing this one root cause will have the largest single impact."
            } else {
                "Ein Regeltyp verursacht den Großteil aller kritischen/hohen Findings. \
                 Die Behebung dieser einen Ursache hat den größten Einzeleffekt."
            }
            .to_string(),
        ),
        crate::audit::summary::IssuePattern::Clustered => (
            if en { "Clustered problems" } else { "Geclusterte Probleme" }.to_string(),
            if en {
                "Issues are grouped in related problem areas. \
                 Addressing one cluster reduces several findings at once."
            } else {
                "Probleme konzentrieren sich in zusammenhängenden Bereichen. \
                 Die Behebung eines Clusters reduziert mehrere Findings gleichzeitig."
            }
            .to_string(),
        ),
        crate::audit::summary::IssuePattern::Scattered => (
            if en {
                "Distributed individual issues"
            } else {
                "Verteilte Einzelprobleme"
            }
            .to_string(),
            if en {
                "Issues are spread across many independent rules. \
                 No single root cause dominates — each finding requires individual attention."
            } else {
                "Probleme verteilen sich über viele unabhängige Regeln. \
                 Keine einzelne Ursache dominiert — jedes Finding erfordert individuelle Aufmerksamkeit."
            }
            .to_string(),
        ),
    };

    let mut dim_map: BTreeMap<String, (usize, crate::wcag::Severity)> = BTreeMap::new();
    for f in &normalized.findings {
        let dim = f.dimension.clone();
        let entry = dim_map
            .entry(dim)
            .or_insert((0, crate::wcag::Severity::Low));
        entry.0 += f.occurrence_count;
        if f.severity > entry.1 {
            entry.1 = f.severity;
        }
    }
    let mut category_breakdown: Vec<(String, usize, String)> = dim_map
        .into_iter()
        .map(|(dim, (count, sev))| {
            let sev_label = match sev {
                crate::wcag::Severity::Critical => {
                    if en { "Critical" } else { "Kritisch" }.to_string()
                }
                crate::wcag::Severity::High => if en { "High" } else { "Hoch" }.to_string(),
                crate::wcag::Severity::Medium => if en { "Medium" } else { "Mittel" }.to_string(),
                crate::wcag::Severity::Low => if en { "Low" } else { "Niedrig" }.to_string(),
            };
            (dim, count, sev_label)
        })
        .collect();
    category_breakdown.sort_by_key(|c| std::cmp::Reverse(c.1));

    let dominant_issue = audit_summary
        .dominant_issue
        .as_ref()
        .map(|d| d.title.clone());

    let clusters = {
        let mut by_dim: BTreeMap<String, Vec<(String, usize, crate::wcag::Severity)>> =
            BTreeMap::new();
        for f in &normalized.findings {
            // Stored title is canonical English (#406); re-derive the localized
            // title (taxonomy + SEO headings) for non-English reports.
            let title = crate::output::builder::actions::localized_finding_text(locale, f).0;
            by_dim.entry(f.dimension.clone()).or_default().push((
                title,
                f.occurrence_count,
                f.severity,
            ));
        }
        let mut result: Vec<FindingCluster> = by_dim
            .into_iter()
            .filter(|(_, items)| items.len() >= 2)
            .map(|(dim, items)| {
                let worst = items
                    .iter()
                    .map(|(_, _, s)| *s)
                    .max()
                    .unwrap_or(crate::wcag::Severity::Low);
                let total_occ: usize = items.iter().map(|(_, c, _)| c).sum();
                let sev_label = match worst {
                    crate::wcag::Severity::Critical => {
                        if en { "Critical" } else { "Kritisch" }.to_string()
                    }
                    crate::wcag::Severity::High => if en { "High" } else { "Hoch" }.to_string(),
                    crate::wcag::Severity::Medium => {
                        if en { "Medium" } else { "Mittel" }.to_string()
                    }
                    crate::wcag::Severity::Low => if en { "Low" } else { "Niedrig" }.to_string(),
                };
                FindingCluster {
                    label: dimension_cluster_label(en, &dim, items.len()),
                    dimension: dim.clone(),
                    finding_count: items.len(),
                    occurrence_total: total_occ,
                    severity_label: sev_label,
                    finding_titles: items.into_iter().map(|(t, _, _)| t).collect(),
                }
            })
            .collect();
        result.sort_by_key(|c| std::cmp::Reverse(c.occurrence_total));
        result
    };

    DiagnosisBlock {
        section_title,
        pattern_label,
        pattern_description,
        is_systematic: audit_summary.is_systematic,
        category_breakdown,
        dominant_issue,
        verdict_intro: audit_summary.verdict_intro.clone(),
        clusters,
    }
}