auditmysite 1.7.0

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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! Excluding intentional specimens from an audit (#645).
//!
//! Sites that teach accessibility ship markup that is broken on purpose. Like
//! axe-core's `exclude`, an exclusion never removes a page or a rule: the page
//! is audited as usual, and only findings whose element lies inside an
//! excluded subtree are dropped before scoring. Every exclusion is reported —
//! which selectors were applied, how many elements each matched (also when
//! that is zero), and how many findings were dropped — so excluding never
//! silently hides a finding.
//!
//! Locating a finding's element works on two paths:
//! - by backend DOM node id — AX-tree rules (via the AX node) and the shared
//!   DOM rules (which carry the id directly);
//! - by selector — JavaScript page rules, contrast and journey findings only
//!   know a CSS path. Such a finding is excluded only if its selector matches
//!   at least one element and **every** match lies inside an excluded
//!   subtree; an ambiguous or unparseable selector keeps the finding.
//!
//! Findings without either (page-level findings) are never excluded.

use std::collections::{BTreeMap, HashMap, HashSet};

use chromiumoxide::cdp::browser_protocol::dom::{
    GetDocumentParams, Node as CdpNode, NodeId, QuerySelectorAllParams,
};
use chromiumoxide::Page;
use fluent_bundle::FluentValue;
use serde::{Deserialize, Serialize};
use tracing::warn;

use crate::accessibility::AXTree;
use crate::audit::interactive_finding::InteractiveFinding;
use crate::wcag::types::{Outcome, Violation};

/// Always honoured, without configuration: marks a whole subtree as excluded.
pub const BUILTIN_EXCLUDE_SELECTOR: &str = "[data-audit-exclude]";

/// The effective selector list: the built-in attribute first, then the
/// user-supplied selectors (trimmed, empty and duplicate entries dropped).
pub fn effective_selectors(user: &[String]) -> Vec<String> {
    let mut out = vec![BUILTIN_EXCLUDE_SELECTOR.to_string()];
    for selector in user.iter().map(|s| s.trim()).filter(|s| !s.is_empty()) {
        if !out.iter().any(|existing| existing == selector) {
            out.push(selector.to_string());
        }
    }
    out
}

// ── Report block (canonical, language-neutral numbers) ───────────────────────

/// Per page: which exclusions were applied and what they removed.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ExclusionReport {
    /// Every applied selector, including those that matched nothing.
    pub selectors: Vec<ExclusionSelectorResult>,
    /// Excluded WCAG finding occurrences (all outcomes), summed over
    /// `rules`. Per rule, the larger count of the two viewport passes.
    pub excluded_occurrences: usize,
    /// Of `excluded_occurrences`, confirmed violations (outcome `fail`).
    pub excluded_violations: usize,
    /// Excluded occurrences per rule, sorted by rule id.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<ExcludedRuleCount>,
    /// Excluded Accessibility-Journey findings.
    #[serde(default)]
    pub excluded_interactive_findings: usize,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ExclusionSelectorResult {
    pub selector: String,
    /// `true` for the always-honoured `[data-audit-exclude]`.
    pub builtin: bool,
    /// Elements the selector matched (the roots of excluded subtrees); the
    /// larger count of the two viewport passes.
    pub matched_elements: usize,
    /// The browser rejected the selector; it excluded nothing.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub invalid: bool,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ExcludedRuleCount {
    /// Rule id as on the findings (`rule_id`, falling back to the criterion).
    pub rule_id: String,
    /// WCAG criterion of the rule (e.g. `"1.1.1"`).
    pub wcag: String,
    pub occurrences: usize,
    pub violations: usize,
}

impl ExclusionReport {
    /// Worth a note in the PDF: a user-supplied selector was applied, or the
    /// built-in attribute matched something. A default run on a page without
    /// `data-audit-exclude` stays quiet there (the JSON still lists it).
    pub fn is_noteworthy(&self) -> bool {
        self.selectors
            .iter()
            .any(|s| !s.builtin || s.matched_elements > 0)
    }
}

// ── Per-pass scope ────────────────────────────────────────────────────────────

/// What one selector matched in one viewport pass.
#[derive(Debug, Clone, PartialEq)]
pub struct SelectorMatch {
    pub selector: String,
    pub builtin: bool,
    pub matched_elements: usize,
    pub invalid: bool,
}

/// The excluded subtrees of the currently loaded page (one viewport pass).
#[derive(Debug, Clone, Default)]
pub struct ExclusionScope {
    pub matches: Vec<SelectorMatch>,
    /// Backend node ids of every node inside an excluded subtree.
    backend_ids: HashSet<i64>,
}

impl ExclusionScope {
    /// Something to exclude: at least one selector matched.
    pub fn is_active(&self) -> bool {
        self.matches.iter().any(|m| m.matched_elements > 0)
    }

    fn contains(&self, backend_id: i64) -> bool {
        self.backend_ids.contains(&backend_id)
    }

    /// Selectors that matched something — used for the in-page selector check.
    fn active_selectors(&self) -> Vec<&str> {
        self.matches
            .iter()
            .filter(|m| m.matched_elements > 0 && !m.invalid)
            .map(|m| m.selector.as_str())
            .collect()
    }

    #[cfg(test)]
    fn with_backend_ids(matches: Vec<SelectorMatch>, ids: impl IntoIterator<Item = i64>) -> Self {
        Self {
            matches,
            backend_ids: ids.into_iter().collect(),
        }
    }
}

/// Resolve the excluded subtrees on the loaded page.
///
/// One `Runtime.evaluate` counts every selector's matches (and detects
/// invalid selectors). Only if something matched does it fetch the DOM once
/// and collect the backend ids of every matched subtree (children, shadow
/// roots, frame and template content included).
pub async fn resolve_scope(page: &Page, selectors: &[String]) -> ExclusionScope {
    let counts = count_matches(page, selectors).await;
    let mut matches: Vec<SelectorMatch> = selectors
        .iter()
        .zip(counts)
        .map(|(selector, count)| SelectorMatch {
            selector: selector.clone(),
            builtin: selector == BUILTIN_EXCLUDE_SELECTOR,
            matched_elements: count.unwrap_or(0),
            invalid: count.is_none(),
        })
        .collect();

    let mut scope = ExclusionScope::default();
    if !matches.iter().any(|m| m.matched_elements > 0) {
        scope.matches = matches;
        return scope;
    }

    let root = match page
        .execute(GetDocumentParams {
            depth: Some(-1),
            pierce: Some(true),
        })
        .await
    {
        Ok(response) => response.result.root.clone(),
        Err(e) => {
            // Without the DOM no subtree can be resolved; report the
            // selectors as matching nothing rather than claiming exclusions.
            warn!("Exclusion: DOM.getDocument failed: {e}");
            for m in &mut matches {
                m.matched_elements = 0;
            }
            scope.matches = matches;
            return scope;
        }
    };
    let mut by_node_id: HashMap<NodeId, &CdpNode> = HashMap::new();
    index_nodes(&root, &mut by_node_id);

    for m in &mut matches {
        if m.matched_elements == 0 {
            continue;
        }
        let params = QuerySelectorAllParams::new(root.node_id, m.selector.clone());
        match page.execute(params).await {
            Ok(response) => {
                let node_ids = &response.result.node_ids;
                m.matched_elements = node_ids.len();
                for id in node_ids {
                    if let Some(node) = by_node_id.get(id) {
                        collect_subtree(node, &mut scope.backend_ids);
                    }
                }
            }
            Err(e) => {
                warn!(
                    "Exclusion: DOM.querySelectorAll failed for {}: {e}",
                    m.selector
                );
                m.matched_elements = 0;
            }
        }
    }
    scope.matches = matches;
    install_in_page_marker(page, &scope).await;
    scope
}

/// Expose the excluded subtree roots to the page's own rule scripts as
/// `window.__amsIsExcluded(el)`, so capped JavaScript rules do not spend
/// their cap on excluded elements (see `CSS_SELECTOR_JS`). The roots live in
/// a closure-held `WeakSet` — no DOM attribute is written, so no other rule
/// sees a changed document. The marker lives until the next navigation; each
/// viewport pass reloads the page and installs it afresh.
async fn install_in_page_marker(page: &Page, scope: &ExclusionScope) {
    let active = scope.active_selectors();
    if active.is_empty() {
        return;
    }
    let js = format!(
        r#"(function(excl) {{
  var roots = new WeakSet();
  excl.forEach(function(s) {{
    try {{ document.querySelectorAll(s).forEach(function(el) {{ roots.add(el); }}); }} catch (e) {{}}
  }});
  var isExcluded = function(el) {{
    var cur = el;
    while (cur) {{
      if (roots.has(cur)) return true;
      if (cur.parentElement) {{ cur = cur.parentElement; continue; }}
      var root = cur.getRootNode ? cur.getRootNode() : null;
      cur = (root && root.host) ? root.host : null;
    }}
    return false;
  }};
  Object.defineProperty(window, '__amsIsExcluded', {{
    value: isExcluded, configurable: true, writable: true, enumerable: false
  }});
  return true;
}})({})"#,
        serde_json::to_string(&active).unwrap_or_else(|_| "[]".to_string())
    );
    if let Err(e) = page.evaluate(js).await {
        warn!("Exclusion: installing the in-page marker failed: {e}");
    }
}

/// Match count per selector; `None` for a selector the browser rejects.
async fn count_matches(page: &Page, selectors: &[String]) -> Vec<Option<usize>> {
    let js = format!(
        r#"(function(sels) {{
  return sels.map(function(s) {{
    try {{ return document.querySelectorAll(s).length; }} catch (e) {{ return -1; }}
  }});
}})({})"#,
        serde_json::to_string(selectors).unwrap_or_else(|_| "[]".to_string())
    );
    let values: Vec<i64> = match page.evaluate(js).await {
        Ok(result) => result.into_value().unwrap_or_default(),
        Err(e) => {
            warn!("Exclusion: selector count failed: {e}");
            Vec::new()
        }
    };
    selectors
        .iter()
        .enumerate()
        .map(|(i, _)| match values.get(i) {
            Some(n) if *n >= 0 => Some(*n as usize),
            Some(_) => None,
            // Evaluation failed as a whole: nothing matched, nothing excluded.
            None => Some(0),
        })
        .collect()
}

fn child_nodes(node: &CdpNode) -> impl Iterator<Item = &CdpNode> {
    node.children
        .iter()
        .flatten()
        .chain(node.shadow_roots.iter().flatten())
        .chain(node.pseudo_elements.iter().flatten())
        .chain(node.content_document.as_deref())
        .chain(node.template_content.as_deref())
}

fn index_nodes<'a>(node: &'a CdpNode, map: &mut HashMap<NodeId, &'a CdpNode>) {
    map.insert(node.node_id, node);
    for child in child_nodes(node) {
        index_nodes(child, map);
    }
}

fn collect_subtree(node: &CdpNode, out: &mut HashSet<i64>) {
    out.insert(*node.backend_node_id.inner());
    for child in child_nodes(node) {
        collect_subtree(child, out);
    }
}

// ── Filtering ─────────────────────────────────────────────────────────────────

/// How a finding's element can be located.
#[derive(Debug, Clone, PartialEq)]
enum Locator<'a> {
    Backend(i64),
    Selector(&'a str),
    /// The rule's own script found the element inside an excluded subtree.
    MarkedInPage,
    PageLevel,
}

fn locate<'a>(finding: &'a Violation, ax_tree: &AXTree) -> Locator<'a> {
    if finding.in_excluded_subtree {
        return Locator::MarkedInPage;
    }
    if let Some(id) = finding.backend_node_id.or_else(|| {
        ax_tree
            .get_node(&finding.node_id)
            .and_then(|n| n.backend_dom_node_id)
    }) {
        return Locator::Backend(id);
    }
    match finding.selector.as_deref().map(str::trim) {
        Some(sel) if !sel.is_empty() => Locator::Selector(sel),
        _ => Locator::PageLevel,
    }
}

impl ExclusionScope {
    /// Decide without asking the page: true for a finding located — by
    /// backend node id or by its rule's in-page mark — inside an excluded
    /// subtree. Selector-only findings answer false here; they go through
    /// [`filter_findings`].
    pub fn excludes_located(&self, finding: &Violation, ax_tree: &AXTree) -> bool {
        match locate(finding, ax_tree) {
            Locator::Backend(id) => self.contains(id),
            Locator::MarkedInPage => true,
            Locator::Selector(_) | Locator::PageLevel => false,
        }
    }
}

/// Split `findings` into (kept, excluded). `selector_inside` answers, per
/// selector, whether every element it matches lies in an excluded subtree.
fn partition_findings(
    findings: Vec<Violation>,
    scope: &ExclusionScope,
    ax_tree: &AXTree,
    selector_inside: &HashMap<String, bool>,
) -> (Vec<Violation>, Vec<Violation>) {
    findings
        .into_iter()
        .partition(|f| match locate(f, ax_tree) {
            Locator::Backend(id) => !scope.contains(id),
            Locator::Selector(sel) => !selector_inside.get(sel).copied().unwrap_or(false),
            Locator::MarkedInPage => false,
            Locator::PageLevel => true,
        })
}

/// For each selector: does it match at least one element, all of them inside
/// an excluded subtree? Invalid or unmatched selectors answer `false`.
async fn selectors_inside(
    page: &Page,
    scope: &ExclusionScope,
    selectors: Vec<String>,
) -> HashMap<String, bool> {
    if selectors.is_empty() {
        return HashMap::new();
    }
    let excl = scope.active_selectors();
    let js = format!(
        r#"(function(sels, excl) {{
  function inside(el) {{
    var cur = el;
    while (cur) {{
      for (var i = 0; i < excl.length; i++) {{
        try {{ if (cur.matches && cur.matches(excl[i])) return true; }} catch (e) {{}}
      }}
      if (cur.parentElement) {{ cur = cur.parentElement; continue; }}
      var root = cur.getRootNode ? cur.getRootNode() : null;
      cur = (root && root.host) ? root.host : null;
    }}
    return false;
  }}
  return sels.map(function(s) {{
    var list;
    try {{ list = document.querySelectorAll(s); }} catch (e) {{ return false; }}
    if (!list.length) return false;
    for (var i = 0; i < list.length; i++) {{ if (!inside(list[i])) return false; }}
    return true;
  }});
}})({}, {})"#,
        serde_json::to_string(&selectors).unwrap_or_else(|_| "[]".to_string()),
        serde_json::to_string(&excl).unwrap_or_else(|_| "[]".to_string()),
    );
    let answers: Vec<bool> = match page.evaluate(js).await {
        Ok(result) => result.into_value().unwrap_or_default(),
        Err(e) => {
            warn!("Exclusion: selector check failed: {e}");
            Vec::new()
        }
    };
    selectors
        .into_iter()
        .enumerate()
        .map(|(i, sel)| (sel, answers.get(i).copied().unwrap_or(false)))
        .collect()
}

/// Drop the findings located inside the excluded subtrees; returns
/// `(kept, excluded)`. A no-op when nothing matched.
pub async fn filter_findings(
    page: &Page,
    scope: &ExclusionScope,
    ax_tree: &AXTree,
    findings: Vec<Violation>,
) -> (Vec<Violation>, Vec<Violation>) {
    if !scope.is_active() || findings.is_empty() {
        return (findings, Vec::new());
    }
    let mut pending: Vec<String> = findings
        .iter()
        .filter_map(|f| match locate(f, ax_tree) {
            Locator::Selector(sel) => Some(sel.to_string()),
            _ => None,
        })
        .collect();
    pending.sort();
    pending.dedup();
    let inside = selectors_inside(page, scope, pending).await;
    partition_findings(findings, scope, ax_tree, &inside)
}

/// Drop the journey findings whose selector lies inside the excluded
/// subtrees; returns the number dropped. Findings without a selector stay.
pub async fn filter_interactive_findings(
    page: &Page,
    scope: &ExclusionScope,
    findings: &mut Vec<InteractiveFinding>,
) -> usize {
    if !scope.is_active() {
        return 0;
    }
    let mut pending: Vec<String> = findings
        .iter()
        .filter_map(|f| f.values.selector.as_deref().map(str::trim))
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect();
    pending.sort();
    pending.dedup();
    if pending.is_empty() {
        return 0;
    }
    let inside = selectors_inside(page, scope, pending).await;
    let before = findings.len();
    findings.retain(|f| {
        !f.values
            .selector
            .as_deref()
            .map(str::trim)
            .and_then(|s| inside.get(s).copied())
            .unwrap_or(false)
    });
    before - findings.len()
}

// ── Tally → report ────────────────────────────────────────────────────────────

/// Collects what each viewport pass excluded, then folds it into the report.
#[derive(Debug, Default)]
pub struct ExclusionTally {
    /// Per selector: largest match count over the passes.
    selectors: Vec<SelectorMatch>,
    /// Per (rule id, criterion): (occurrences, violations) per pass.
    desktop: BTreeMap<(String, String), (usize, usize)>,
    mobile: BTreeMap<(String, String), (usize, usize)>,
    interactive: usize,
}

#[derive(Debug, Clone, Copy)]
pub enum Pass {
    Desktop,
    Mobile,
}

impl ExclusionTally {
    pub fn record_scope(&mut self, scope: &ExclusionScope) {
        for m in &scope.matches {
            match self.selectors.iter_mut().find(|s| s.selector == m.selector) {
                Some(existing) => {
                    existing.matched_elements = existing.matched_elements.max(m.matched_elements);
                    existing.invalid |= m.invalid;
                }
                None => self.selectors.push(m.clone()),
            }
        }
    }

    pub fn record_findings(&mut self, pass: Pass, excluded: &[Violation]) {
        let map = match pass {
            Pass::Desktop => &mut self.desktop,
            Pass::Mobile => &mut self.mobile,
        };
        for f in excluded {
            let key = (
                f.rule_id.clone().unwrap_or_else(|| f.rule.clone()),
                f.rule.clone(),
            );
            let entry = map.entry(key).or_default();
            entry.0 += 1;
            if f.kind == Outcome::Fail {
                entry.1 += 1;
            }
        }
    }

    pub fn record_interactive(&mut self, count: usize) {
        self.interactive += count;
    }

    pub fn into_report(self) -> ExclusionReport {
        let mut keys: Vec<&(String, String)> =
            self.desktop.keys().chain(self.mobile.keys()).collect();
        keys.sort();
        keys.dedup();
        let rules: Vec<ExcludedRuleCount> = keys
            .into_iter()
            .map(|key| {
                let d = self.desktop.get(key).copied().unwrap_or_default();
                let m = self.mobile.get(key).copied().unwrap_or_default();
                ExcludedRuleCount {
                    rule_id: key.0.clone(),
                    wcag: key.1.clone(),
                    occurrences: d.0.max(m.0),
                    violations: d.1.max(m.1),
                }
            })
            .collect();
        ExclusionReport {
            selectors: self
                .selectors
                .into_iter()
                .map(|s| ExclusionSelectorResult {
                    selector: s.selector,
                    builtin: s.builtin,
                    matched_elements: s.matched_elements,
                    invalid: s.invalid,
                })
                .collect(),
            excluded_occurrences: rules.iter().map(|r| r.occurrences).sum(),
            excluded_violations: rules.iter().map(|r| r.violations).sum(),
            rules,
            excluded_interactive_findings: self.interactive,
        }
    }
}

// ── Batch aggregate ──────────────────────────────────────────────────────────

/// Audit exclusions summed over the pages of a batch.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct BatchExclusionSummary {
    pub selectors: Vec<BatchExclusionSelector>,
    /// Pages on which at least one selector matched an element.
    pub pages_with_matches: usize,
    pub excluded_occurrences: usize,
    pub excluded_violations: usize,
    pub excluded_interactive_findings: usize,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct BatchExclusionSelector {
    pub selector: String,
    pub builtin: bool,
    /// Pages on which the selector matched at least one element.
    pub pages_matched: usize,
    /// Matched elements summed over all pages.
    pub matched_elements: usize,
    /// The browser rejected the selector on at least one page.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub invalid: bool,
}

impl BatchExclusionSummary {
    /// `None` when no page carries an exclusion record.
    pub fn aggregate<'a>(pages: impl IntoIterator<Item = &'a ExclusionReport>) -> Option<Self> {
        let mut summary: Option<Self> = None;
        for page in pages {
            let s = summary.get_or_insert_with(Self::default);
            if page.selectors.iter().any(|sel| sel.matched_elements > 0) {
                s.pages_with_matches += 1;
            }
            s.excluded_occurrences += page.excluded_occurrences;
            s.excluded_violations += page.excluded_violations;
            s.excluded_interactive_findings += page.excluded_interactive_findings;
            for sel in &page.selectors {
                let entry = match s
                    .selectors
                    .iter_mut()
                    .position(|e| e.selector == sel.selector)
                {
                    Some(i) => &mut s.selectors[i],
                    None => {
                        s.selectors.push(BatchExclusionSelector {
                            selector: sel.selector.clone(),
                            builtin: sel.builtin,
                            ..Default::default()
                        });
                        s.selectors.last_mut().expect("just pushed")
                    }
                };
                entry.matched_elements += sel.matched_elements;
                if sel.matched_elements > 0 {
                    entry.pages_matched += 1;
                }
                entry.invalid |= sel.invalid;
            }
        }
        summary
    }

    /// Same rule as [`ExclusionReport::is_noteworthy`].
    pub fn is_noteworthy(&self) -> bool {
        self.selectors
            .iter()
            .any(|s| !s.builtin || s.matched_elements > 0)
    }
}

// ── Presentation (PDF) ────────────────────────────────────────────────────────

/// Localized note for the batch cover's audit frame, or `None` when nothing
/// is worth mentioning.
pub fn batch_exclusion_note(
    summary: &BatchExclusionSummary,
    total_pages: usize,
    i18n: &crate::i18n::I18n,
) -> Option<String> {
    if !summary.is_noteworthy() {
        return None;
    }
    let mut parts: Vec<String> = summary
        .selectors
        .iter()
        .filter(|s| !s.builtin || s.matched_elements > 0)
        .map(|s| {
            if s.invalid && s.matched_elements == 0 {
                i18n.t_args(
                    "exclusion-selector-invalid",
                    &[("selector", FluentValue::from(s.selector.clone()))],
                )
            } else if s.matched_elements == 0 {
                i18n.t_args(
                    "exclusion-selector-unmatched",
                    &[("selector", FluentValue::from(s.selector.clone()))],
                )
            } else {
                i18n.t_args(
                    "batch-exclusion-selector-matched",
                    &[
                        ("selector", FluentValue::from(s.selector.clone())),
                        ("count", FluentValue::from(s.matched_elements as i64)),
                        ("pages", FluentValue::from(s.pages_matched as i64)),
                        ("total", FluentValue::from(total_pages as i64)),
                    ],
                )
            }
        })
        .collect();
    parts.push(findings_text(
        i18n,
        summary.excluded_occurrences,
        None,
        summary.excluded_interactive_findings,
    ));
    Some(parts.join("; "))
}

fn findings_text(
    i18n: &crate::i18n::I18n,
    occurrences: usize,
    rules: Option<usize>,
    interactive: usize,
) -> String {
    let mut text = match rules {
        Some(rules) => i18n.t_args(
            "exclusion-findings",
            &[("occurrences", occurrences as i64), ("rules", rules as i64)],
        ),
        None => i18n.t_args(
            "batch-exclusion-findings",
            &[("occurrences", occurrences as i64)],
        ),
    };
    if interactive > 0 {
        text.push_str(&i18n.t_args(
            "exclusion-interactive",
            &[("interactive", interactive as i64)],
        ));
    }
    text
}

/// Localized one-paragraph note for the methodology section, or `None` when
/// the report has nothing worth mentioning (see
/// [`ExclusionReport::is_noteworthy`]).
pub fn exclusion_note(report: &ExclusionReport, i18n: &crate::i18n::I18n) -> Option<String> {
    if !report.is_noteworthy() {
        return None;
    }
    let mut parts: Vec<String> = report
        .selectors
        .iter()
        .filter(|s| !s.builtin || s.matched_elements > 0)
        .map(|s| {
            let key = if s.invalid {
                "exclusion-selector-invalid"
            } else if s.matched_elements == 0 {
                "exclusion-selector-unmatched"
            } else {
                "exclusion-selector-matched"
            };
            i18n.t_args(
                key,
                &[
                    ("selector", FluentValue::from(s.selector.clone())),
                    ("count", FluentValue::from(s.matched_elements as i64)),
                ],
            )
        })
        .collect();
    parts.push(findings_text(
        i18n,
        report.excluded_occurrences,
        Some(report.rules.len()),
        report.excluded_interactive_findings,
    ));
    Some(parts.join("; "))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::WcagLevel;
    use crate::wcag::types::Severity;

    fn finding(rule_id: &str, node_id: &str) -> Violation {
        Violation::new(
            "1.1.1",
            "Non-text",
            WcagLevel::A,
            Severity::High,
            "m",
            node_id,
        )
        .with_rule_id(rule_id)
    }

    fn active_scope(ids: impl IntoIterator<Item = i64>) -> ExclusionScope {
        ExclusionScope::with_backend_ids(
            vec![SelectorMatch {
                selector: BUILTIN_EXCLUDE_SELECTOR.to_string(),
                builtin: true,
                matched_elements: 1,
                invalid: false,
            }],
            ids,
        )
    }

    #[test]
    fn effective_selectors_always_start_with_the_builtin_and_dedup() {
        let sel = effective_selectors(&[
            " [data-specimen] ".to_string(),
            "".to_string(),
            "[data-audit-exclude]".to_string(),
            "[data-specimen]".to_string(),
        ]);
        assert_eq!(sel, vec!["[data-audit-exclude]", "[data-specimen]"]);
    }

    #[test]
    fn backend_id_inside_scope_is_excluded_outside_kept() {
        let scope = active_scope([10, 11]);
        let mut inside = finding("image-alt", "document");
        inside.backend_node_id = Some(11);
        let mut outside = finding("image-alt", "document");
        outside.backend_node_id = Some(42);
        let (kept, excluded) = partition_findings(
            vec![inside, outside],
            &scope,
            &AXTree::default(),
            &HashMap::new(),
        );
        assert_eq!(kept.len(), 1);
        assert_eq!(kept[0].backend_node_id, Some(42));
        assert_eq!(excluded.len(), 1);
        assert_eq!(excluded[0].backend_node_id, Some(11));
    }

    #[test]
    fn a_known_backend_id_outside_wins_over_a_selector_answer() {
        let scope = active_scope([10]);
        let mut f = finding("x", "document").with_selector("div.specimen");
        f.backend_node_id = Some(99);
        let inside: HashMap<String, bool> = [("div.specimen".to_string(), true)].into();
        let (kept, excluded) = partition_findings(vec![f], &scope, &AXTree::default(), &inside);
        assert_eq!((kept.len(), excluded.len()), (1, 0));
    }

    #[test]
    fn selector_findings_follow_the_in_page_answer() {
        let scope = active_scope([]);
        let a = finding("click-events-have-key-events", "div#a").with_selector("div#a");
        let b = finding("click-events-have-key-events", "div#b").with_selector("div#b");
        let c = finding("click-events-have-key-events", "div#c").with_selector("div#c");
        let inside: HashMap<String, bool> = [
            ("div#a".to_string(), true),
            ("div#b".to_string(), false),
            // div#c: no answer (e.g. evaluation failed) → kept
        ]
        .into();
        let (kept, excluded) =
            partition_findings(vec![a, b, c], &scope, &AXTree::default(), &inside);
        let kept: Vec<_> = kept.iter().filter_map(|f| f.selector.as_deref()).collect();
        assert_eq!(kept, vec!["div#b", "div#c"]);
        assert_eq!(excluded[0].selector.as_deref(), Some("div#a"));
    }

    #[test]
    fn page_level_findings_are_never_excluded() {
        let scope = active_scope([1, 2, 3]);
        let (kept, excluded) = partition_findings(
            vec![finding("document-title", "page")],
            &scope,
            &AXTree::default(),
            &HashMap::new(),
        );
        assert_eq!((kept.len(), excluded.len()), (1, 0));
    }

    #[test]
    fn inactive_scope_reports_nothing_active() {
        let scope = ExclusionScope {
            matches: vec![SelectorMatch {
                selector: ".x".into(),
                builtin: false,
                matched_elements: 0,
                invalid: false,
            }],
            ..Default::default()
        };
        assert!(!scope.is_active());
        assert!(scope.active_selectors().is_empty());
    }

    #[test]
    fn tally_takes_the_larger_pass_per_rule_and_max_matches() {
        let mut tally = ExclusionTally::default();
        let m = |n| ExclusionScope {
            matches: vec![
                SelectorMatch {
                    selector: BUILTIN_EXCLUDE_SELECTOR.into(),
                    builtin: true,
                    matched_elements: 0,
                    invalid: false,
                },
                SelectorMatch {
                    selector: "[data-specimen]".into(),
                    builtin: false,
                    matched_elements: n,
                    invalid: false,
                },
            ],
            ..Default::default()
        };
        tally.record_scope(&m(2));
        tally.record_scope(&m(3));
        let warn = finding("image-alt", "1").as_warning();
        tally.record_findings(
            Pass::Desktop,
            &[finding("image-alt", "1"), finding("image-alt", "2"), warn],
        );
        tally.record_findings(Pass::Mobile, &[finding("image-alt", "1")]);
        tally.record_findings(Pass::Mobile, &[finding("label", "3")]);
        tally.record_interactive(2);
        let report = tally.into_report();

        assert_eq!(report.selectors[1].matched_elements, 3);
        assert_eq!(report.selectors[0].matched_elements, 0);
        let alt = report
            .rules
            .iter()
            .find(|r| r.rule_id == "image-alt")
            .unwrap();
        assert_eq!((alt.occurrences, alt.violations), (3, 2));
        assert_eq!(report.excluded_occurrences, 4);
        assert_eq!(report.excluded_violations, 3);
        assert_eq!(report.excluded_interactive_findings, 2);
        assert!(report.is_noteworthy());
    }

    #[test]
    fn default_run_without_matches_is_not_noteworthy() {
        let report = ExclusionReport {
            selectors: vec![ExclusionSelectorResult {
                selector: BUILTIN_EXCLUDE_SELECTOR.into(),
                builtin: true,
                matched_elements: 0,
                invalid: false,
            }],
            ..Default::default()
        };
        assert!(!report.is_noteworthy());
    }

    #[test]
    fn note_names_unmatched_selectors_in_both_languages() {
        let report = ExclusionReport {
            selectors: vec![
                ExclusionSelectorResult {
                    selector: BUILTIN_EXCLUDE_SELECTOR.into(),
                    builtin: true,
                    matched_elements: 0,
                    invalid: false,
                },
                ExclusionSelectorResult {
                    selector: ".nothing".into(),
                    builtin: false,
                    matched_elements: 0,
                    invalid: false,
                },
                ExclusionSelectorResult {
                    selector: "[data-specimen]".into(),
                    builtin: false,
                    matched_elements: 2,
                    invalid: false,
                },
            ],
            excluded_occurrences: 5,
            excluded_violations: 4,
            rules: vec![ExcludedRuleCount {
                rule_id: "image-alt".into(),
                wcag: "1.1.1".into(),
                occurrences: 5,
                violations: 4,
            }],
            excluded_interactive_findings: 0,
        };
        let en = exclusion_note(&report, &crate::i18n::I18n::new("en").unwrap()).unwrap();
        assert!(en.contains(".nothing"), "{en}");
        assert!(en.contains("[data-specimen]"), "{en}");
        assert!(!en.contains(BUILTIN_EXCLUDE_SELECTOR), "{en}");
        assert!(en.contains('5'), "{en}");
        assert!(
            !en.chars().any(|c| "äöüÄÖÜß".contains(c)),
            "EN note must not contain German: {en}"
        );
        let de = exclusion_note(&report, &crate::i18n::I18n::new("de").unwrap()).unwrap();
        assert_ne!(en, de);
        assert!(de.contains(".nothing"), "{de}");
    }
}