auditmysite 1.7.2

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
//! Accordion pattern (issue #32).
//!
//! Detects accordion structures: button with `aria-expanded` that toggles a
//! controlled region. Native disclosure widgets (`<details>`/`<summary>`)
//! also count when summary has the toggle semantics.

use crate::accessibility::AXTree;
use crate::cli::WcagLevel;
use crate::wcag::types::{Severity, Violation};

use super::disclosure_menu::NATIVE_DISCLOSURE_ROLES;
use super::{JourneyCandidate, JourneyKind, PatternAnalysis, PatternConfidence, PatternKind};

pub(super) fn detect(tree: &AXTree, out: &mut PatternAnalysis) {
    let mut triggers = 0usize;
    let mut with_controls = 0usize;
    let mut non_button_triggers = 0usize;

    for node in tree.iter() {
        let expanded = node.get_property_bool("expanded");
        if expanded.is_none() {
            continue;
        }
        // Skip dialog/menu/combobox — those have their own patterns.
        // Also skip Details (the <details> container): it carries expanded state
        // but is not itself the clickable trigger — DisclosureTriangle (<summary>) is.
        let role = node.role.as_deref().unwrap_or("");
        if matches!(
            role,
            "dialog" | "alertdialog" | "menu" | "combobox" | "listbox" | "Details"
        ) {
            continue;
        }
        if is_expandable_composite_item(node, tree) {
            continue;
        }

        triggers += 1;

        // aria-controls is a node reference in the AX tree (idrefList), not a plain
        // string — use has_property() which matches regardless of value type.
        let has_controls = node.has_property("controls");
        if has_controls {
            with_controls += 1;
        }

        // Accordion triggers must be buttons.
        // DisclosureTriangle is Chrome's AX-tree role for <summary>, which has
        // implicit ARIA role "button" per the HTML-ARIA spec — treat it as compliant.
        // DisclosureTriangleGrouped is the same <summary> inside <details name>.
        let is_button_like = role == "button" || NATIVE_DISCLOSURE_ROLES.contains(&role);
        if !is_button_like {
            non_button_triggers += 1;
            out.violations.push(
                Violation::new(
                    "4.1.2",
                    "Name, Role, Value",
                    WcagLevel::A,
                    Severity::Medium,
                    format!(
                        "Accordion trigger has aria-expanded but role is \"{role}\" — should be a button so keyboard users can activate it with Enter/Space."
                    ),
                    &node.node_id,
                )
                .with_fix(
                    "Use a native <button> as the accordion trigger, or set role=\"button\" with tabindex=\"0\" and a keydown handler.",
                )
                .with_rule_id("accordion-trigger-not-button")
                .with_help_url("https://www.w3.org/WAI/ARIA/apg/patterns/accordion/"),
            );
        }

        // Trigger without aria-controls is a warning (not strictly required
        // but strongly recommended for screen readers).
        // Only check when the trigger is currently expanded: Chrome CDP does not
        // resolve the `controls` AX property when the target element is hidden
        // (display:none / aria-hidden), so the check is unreliable for collapsed
        // triggers even when aria-controls is correctly set in the DOM.
        // Nav/banner exception remains for disclosure menus that never expand
        // into a visible AX node.
        // A native <summary> needs no aria-controls: the controlled region is
        // the enclosing <details>, and the relationship lives in the nesting.
        if role == "button"
            && expanded == Some(true)
            && !has_controls
            && !in_nav_or_banner(node, tree)
        {
            out.violations.push(
                Violation::new(
                    "4.1.2",
                    "Name, Role, Value",
                    WcagLevel::A,
                    Severity::Low,
                    "Accordion trigger has aria-expanded but no aria-controls — screen readers cannot identify the controlled region.",
                    &node.node_id,
                )
                .with_fix(
                    "Add aria-controls=\"<id>\" pointing to the collapsible region.",
                )
                .with_rule_id("accordion-no-controls")
                .with_help_url("https://www.w3.org/WAI/ARIA/apg/patterns/accordion/"),
            );
        }
    }

    if triggers == 0 {
        return;
    }

    let confidence = if non_button_triggers == 0 && with_controls == triggers {
        PatternConfidence::Strong
    } else {
        PatternConfidence::Partial
    };
    out.add_recognized(
        "Accordion",
        format!(
            "{} accordion {}; {} with aria-controls; {} non-button triggers.",
            triggers,
            if triggers == 1 { "trigger" } else { "triggers" },
            with_controls,
            non_button_triggers
        ),
        confidence,
    );

    // Emit journey candidates for interactive accordion verification.
    // Include both button and DisclosureTriangle (<summary>) triggers.
    // Skip nav/banner contexts (handled by DisclosureMenu).
    //
    // AccordionToggle and DisclosureToggle run the same journey. A trigger that
    // DisclosureMenu already offered would otherwise be clicked through twice
    // and report every finding twice. A menu button (`aria-haspopup`) that
    // DisclosureMenu offered as MenuOpen is skipped as well: ARIA APG treats it
    // as the Menu Button pattern, and the disclosure journey would judge it by
    // expectations it isn't meant to meet (plan 53, 2026-09-27).
    let already_offered: Vec<i64> = out
        .journey_candidates
        .iter()
        .filter(|c| {
            matches!(
                c.required_journey,
                JourneyKind::DisclosureToggle | JourneyKind::MenuOpen
            )
        })
        .filter_map(|c| c.trigger_backend_id)
        .collect();
    for node in tree.iter() {
        if node.get_property_bool("expanded").is_none() {
            continue;
        }
        let role = node.role.as_deref().unwrap_or("");
        if matches!(
            role,
            "dialog" | "alertdialog" | "menu" | "combobox" | "listbox" | "Details"
        ) {
            continue;
        }
        if role != "button" && !NATIVE_DISCLOSURE_ROLES.contains(&role) {
            continue;
        }
        if in_nav_or_banner(node, tree) {
            continue;
        }
        let has_controls = node.has_property("controls");
        if let Some(bid) = node.backend_dom_node_id {
            if already_offered.contains(&bid) {
                continue;
            }
            out.journey_candidates.push(JourneyCandidate {
                pattern_kind: PatternKind::Accordion,
                trigger_backend_id: Some(bid),
                controlled_backend_id: None,
                confidence: if has_controls { 0.85 } else { 0.7 },
                required_journey: JourneyKind::AccordionToggle,
            });
        }
    }
}

/// A tree item, or a row of a grid/treegrid, carries `aria-expanded` as part
/// of its own composite widget: the tree or grid owns the keyboard contract
/// (arrow keys, Enter), so the item is no accordion trigger (#655).
fn is_expandable_composite_item(node: &crate::accessibility::AXNode, tree: &AXTree) -> bool {
    match node.role.as_deref() {
        Some("treeitem") => true,
        Some("row") => {
            let mut current = node.parent_id.as_deref();
            while let Some(parent) = current.and_then(|id| tree.get_node(id)) {
                if matches!(parent.role.as_deref(), Some("grid" | "treegrid")) {
                    return true;
                }
                current = parent.parent_id.as_deref();
            }
            false
        }
        _ => false,
    }
}

fn in_nav_or_banner(node: &crate::accessibility::AXNode, tree: &AXTree) -> bool {
    let mut current = node.parent_id.as_deref();
    while let Some(id) = current {
        if let Some(parent) = tree.get_node(id) {
            match parent.role.as_deref() {
                Some("navigation") | Some("banner") => return true,
                _ => {}
            }
            current = parent.parent_id.as_deref();
        } else {
            break;
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::accessibility::{AXNode, AXProperty, AXTree, AXValue};

    fn trigger(id: &str, role: &str, controls: Option<&str>) -> AXNode {
        trigger_with_expanded(id, role, controls, false)
    }

    fn trigger_with_expanded(
        id: &str,
        role: &str,
        controls: Option<&str>,
        expanded: bool,
    ) -> AXNode {
        let mut n = AXNode {
            node_id: id.into(),
            ignored: false,
            ignored_reasons: vec![],
            role: Some(role.into()),
            name: Some("Toggle".into()),
            name_source: None,
            description: None,
            value: None,
            properties: vec![AXProperty {
                name: "expanded".into(),
                value: AXValue::Bool(expanded),
            }],
            child_ids: vec![],
            parent_id: None,
            backend_dom_node_id: None,
        };
        if let Some(target) = controls {
            n.properties.push(AXProperty {
                name: "controls".into(),
                value: AXValue::String(target.into()),
            });
        }
        n
    }

    #[test]
    fn test_button_with_controls_strong() {
        let tree = AXTree::from_nodes(vec![trigger("1", "button", Some("panel-1"))]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert_eq!(a.recognized[0].confidence, PatternConfidence::Strong);
        assert!(a.violations.is_empty());
    }

    #[test]
    fn test_non_button_trigger_violation() {
        let tree = AXTree::from_nodes(vec![trigger("1", "generic", Some("panel-1"))]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert!(a
            .violations
            .iter()
            .any(|v| v.message.contains("should be a button")));
    }

    #[test]
    fn test_disclosure_triangle_no_false_positive() {
        // Chrome exposes <summary> as DisclosureTriangle in the AX tree.
        // It has an implicit ARIA role of "button" per the HTML-ARIA spec
        // and must not be reported as a non-button accordion trigger.
        let tree = AXTree::from_nodes(vec![trigger("1", "DisclosureTriangle", None)]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert!(
            !a.violations
                .iter()
                .any(|v| v.message.contains("should be a button")),
            "DisclosureTriangle (<summary>) must not be flagged as non-button trigger"
        );
    }

    /// `<summary>` in `<details name>` has its own role. It was reported as a
    /// non-button trigger (Medium, 4.1.2) on every exclusive accordion.
    #[test]
    fn grouped_summary_is_no_false_non_button_trigger() {
        let tree = AXTree::from_nodes(vec![trigger("1", "DisclosureTriangleGrouped", None)]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert!(a.violations.is_empty(), "{:?}", a.violations);
    }

    /// An open `<details>` has no aria-controls, and needs none.
    #[test]
    fn open_native_summary_needs_no_aria_controls() {
        for role in NATIVE_DISCLOSURE_ROLES {
            let tree = AXTree::from_nodes(vec![trigger_with_expanded("1", role, None, true)]);
            let mut a = PatternAnalysis::default();
            detect(&tree, &mut a);
            assert!(a.violations.is_empty(), "{role}: {:?}", a.violations);
        }
    }

    /// AccordionToggle and DisclosureToggle run the same journey: a trigger
    /// DisclosureMenu already offered gets no second candidate.
    #[test]
    fn trigger_already_offered_by_disclosure_menu_is_not_offered_twice() {
        for role in ["button", "DisclosureTriangle", "DisclosureTriangleGrouped"] {
            let mut n = trigger("1", role, None);
            n.backend_dom_node_id = Some(7);
            let tree = AXTree::from_nodes(vec![n]);
            let analysis = crate::patterns::analyze(&tree);
            let offered: Vec<_> = analysis
                .journey_candidates
                .iter()
                .filter(|c| c.trigger_backend_id == Some(7))
                .collect();
            assert_eq!(offered.len(), 1, "{role}: {offered:?}");
            assert_eq!(offered[0].required_journey, JourneyKind::DisclosureToggle);
        }
    }

    /// A menu button is tested by the menu journey only — no accordion
    /// candidate next to its MenuOpen one.
    #[test]
    fn menu_button_is_offered_to_the_menu_journey_only() {
        let mut n = trigger("1", "button", None);
        n.backend_dom_node_id = Some(7);
        n.properties.push(AXProperty {
            name: "hasPopup".into(),
            value: AXValue::String("menu".into()),
        });
        let tree = AXTree::from_nodes(vec![n]);
        let analysis = crate::patterns::analyze(&tree);
        let offered: Vec<_> = analysis
            .journey_candidates
            .iter()
            .filter(|c| c.trigger_backend_id == Some(7))
            .map(|c| c.required_journey)
            .collect();
        assert_eq!(offered, vec![JourneyKind::MenuOpen]);
    }

    /// Without a DisclosureToggle candidate for the trigger, the accordion
    /// still offers its own.
    #[test]
    fn accordion_still_offers_triggers_nobody_else_offered() {
        let mut n = trigger("1", "DisclosureTriangle", None);
        n.backend_dom_node_id = Some(7);
        let tree = AXTree::from_nodes(vec![n]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert_eq!(a.journey_candidates.len(), 1);
        assert_eq!(
            a.journey_candidates[0].required_journey,
            JourneyKind::AccordionToggle
        );
    }

    fn with_parent(mut n: AXNode, parent: &str) -> AXNode {
        n.parent_id = Some(parent.into());
        n
    }

    fn container(id: &str, role: &str, parent: Option<&str>) -> AXNode {
        let mut n = trigger(id, role, None);
        n.properties.clear();
        n.parent_id = parent.map(Into::into);
        n
    }

    /// A treegrid parent row marks its expanded state with aria-expanded,
    /// exactly as the WAI-ARIA treegrid pattern prescribes (#655).
    #[test]
    fn expandable_row_in_treegrid_or_grid_is_no_accordion_trigger() {
        for grid_role in ["treegrid", "grid"] {
            let tree = AXTree::from_nodes(vec![
                container("g", grid_role, None),
                container("rg", "rowgroup", Some("g")),
                with_parent(trigger("r", "row", None), "rg"),
            ]);
            let mut a = PatternAnalysis::default();
            detect(&tree, &mut a);
            assert!(a.violations.is_empty(), "{grid_role}: {:?}", a.violations);
            assert!(a.recognized.is_empty(), "{grid_role}: {:?}", a.recognized);
        }
    }

    #[test]
    fn expandable_treeitem_is_no_accordion_trigger() {
        let tree = AXTree::from_nodes(vec![
            container("t", "tree", None),
            with_parent(trigger("i", "treeitem", None), "t"),
        ]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert!(a.violations.is_empty(), "{:?}", a.violations);
    }

    /// Outside a grid, an expandable row keeps being reported.
    #[test]
    fn expandable_row_outside_grid_is_still_flagged() {
        let tree = AXTree::from_nodes(vec![
            container("t", "table", None),
            with_parent(trigger("r", "row", None), "t"),
        ]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert!(a
            .violations
            .iter()
            .any(|v| v.message.contains("should be a button")));
    }

    #[test]
    fn test_details_container_skipped() {
        // The <details> element itself carries expanded state but is the container,
        // not the trigger — it must not be counted as an accordion trigger.
        let tree = AXTree::from_nodes(vec![trigger("1", "Details", None)]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert!(
            a.violations.is_empty(),
            "Details role (the <details> container) must not produce violations"
        );
    }

    #[test]
    fn test_collapsed_button_without_controls_no_violation() {
        // When collapsed (expanded=false), Chrome CDP doesn't resolve the `controls`
        // property for hidden targets — the check is unreliable, so no violation is emitted.
        let tree = AXTree::from_nodes(vec![trigger("1", "button", None)]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert!(
            a.violations
                .iter()
                .all(|v| !v.message.contains("aria-controls")),
            "collapsed trigger should not emit aria-controls violation"
        );
    }

    #[test]
    fn test_expanded_button_without_controls_low_violation() {
        // When expanded (expanded=true), the controlled panel should be in the AX tree.
        // A missing `controls` property then means aria-controls is truly absent.
        let tree = AXTree::from_nodes(vec![trigger_with_expanded("1", "button", None, true)]);
        let mut a = PatternAnalysis::default();
        detect(&tree, &mut a);
        assert!(
            a.violations
                .iter()
                .any(|v| v.message.contains("aria-controls")),
            "expanded trigger without controls should emit aria-controls violation"
        );
    }
}