auditmysite 0.26.0

Resource-efficient WCAG 2.1 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
//! WCAG 3.3.2 Labels or Instructions
//!
//! Labels or instructions are provided when content requires user input.
//! Level A

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

/// Rule metadata for 3.3.2
pub const INSTRUCTIONS_RULE: RuleMetadata = RuleMetadata {
    id: "3.3.2",
    name: "Labels or Instructions",
    level: WcagLevel::A,
    severity: Severity::High,
    description: "Labels or instructions are provided when content requires user input",
    help_url: "https://www.w3.org/WAI/WCAG21/Understanding/labels-or-instructions.html",
    axe_id: "label",
    tags: &["wcag2a", "wcag332", "cat.forms"],
};

/// Check for labels and instructions on form controls
pub fn check_instructions(tree: &AXTree) -> WcagResults {
    let mut results = WcagResults::new();

    for node in tree.iter() {
        if node.ignored {
            continue;
        }

        results.nodes_checked += 1;
        let role_lower = node.role.as_deref().unwrap_or("").to_lowercase();

        // Check form inputs
        if is_form_input(&role_lower) {
            let has_label = has_accessible_label(node);
            let has_placeholder_text = has_placeholder(node);
            let has_instructions = has_instructions_or_hint(node);

            if !has_label {
                let violation = Violation::new(
                    INSTRUCTIONS_RULE.id,
                    INSTRUCTIONS_RULE.name,
                    INSTRUCTIONS_RULE.level,
                    Severity::Critical,
                    format!("Form control '{}' has no accessible label", role_lower),
                    &node.node_id,
                )
                .with_role(node.role.clone())
                .with_name(node.name.clone())
                .with_fix("Add a <label> element, aria-label, or aria-labelledby attribute")
                .with_help_url(INSTRUCTIONS_RULE.help_url);

                results.add_violation(violation);
                continue;
            }

            // Check if placeholder is used as the only label
            if !has_label && has_placeholder_text && !has_instructions {
                let violation = Violation::new(
                    INSTRUCTIONS_RULE.id,
                    INSTRUCTIONS_RULE.name,
                    INSTRUCTIONS_RULE.level,
                    Severity::Medium,
                    "Placeholder used as only label",
                    &node.node_id,
                )
                .with_role(node.role.clone())
                .with_name(node.name.clone())
                .with_fix("Add a visible <label> element. Placeholder should supplement, not replace, labels")
                .with_help_url(INSTRUCTIONS_RULE.help_url);

                results.add_violation(violation);
            }

            // Check for required fields without indication
            if is_required(node) && !indicates_required(node) {
                let violation = Violation::new(
                    INSTRUCTIONS_RULE.id,
                    INSTRUCTIONS_RULE.name,
                    INSTRUCTIONS_RULE.level,
                    Severity::Medium,
                    "Required field not clearly indicated",
                    &node.node_id,
                )
                .with_role(node.role.clone())
                .with_name(node.name.clone())
                .with_fix("Add visual indicator (e.g., asterisk *) and screen reader text for required fields")
                .with_help_url(INSTRUCTIONS_RULE.help_url);

                results.add_violation(violation);
            }

            // Check for inputs with format requirements
            if needs_format_instructions(&role_lower, node) && !has_format_hint(node) {
                let violation = Violation::new(
                    INSTRUCTIONS_RULE.id,
                    INSTRUCTIONS_RULE.name,
                    INSTRUCTIONS_RULE.level,
                    Severity::Low,
                    format!("Input '{}' may require format instructions", role_lower),
                    &node.node_id,
                )
                .with_role(node.role.clone())
                .with_name(node.name.clone())
                .with_fix("Consider adding format instructions (e.g., 'DD/MM/YYYY' for dates)")
                .with_help_url(INSTRUCTIONS_RULE.help_url);

                results.add_violation(violation);
            }

            // If no violations found for this input, count as pass
            if has_label {
                results.passes += 1;
            }
        }

        // Check fieldsets without legends.
        // Chrome exposes many generic HTML elements (<details>, <address>, <hgroup>, ...)
        // as role="group" — these are NOT form groups and must be excluded.
        // Only flag actual form grouping: explicit role="radiogroup" or an htmlTag of
        // FIELDSET, or a group that contains form controls.
        if role_lower == "radiogroup" || (role_lower == "group" && is_form_group(node, tree)) {
            if !has_group_label(node) {
                let violation = Violation::new(
                    INSTRUCTIONS_RULE.id,
                    INSTRUCTIONS_RULE.name,
                    INSTRUCTIONS_RULE.level,
                    Severity::Medium,
                    "Form group has no legend or label",
                    &node.node_id,
                )
                .with_role(node.role.clone())
                .with_fix("Use <fieldset> with <legend>, or add aria-labelledby to the group")
                .with_help_url(INSTRUCTIONS_RULE.help_url);

                results.add_violation(violation);
            } else {
                results.passes += 1;
            }
        }
    }

    results
}

/// Check if role is a form input
fn is_form_input(role: &str) -> bool {
    matches!(
        role,
        "textbox"
            | "searchbox"
            | "combobox"
            | "listbox"
            | "spinbutton"
            | "slider"
            | "checkbox"
            | "radio"
            | "switch"
            | "textarea"
    )
}

/// Check if node has an accessible label
fn has_accessible_label(node: &AXNode) -> bool {
    if let Some(name) = &node.name {
        if !name.trim().is_empty() {
            return true;
        }
    }
    false
}

/// Check if node has placeholder
fn has_placeholder(node: &AXNode) -> bool {
    node.get_property_str("placeholder")
        .map(|s| !s.is_empty())
        .unwrap_or(false)
}

/// Check if node has instructions or hint text
fn has_instructions_or_hint(node: &AXNode) -> bool {
    if let Some(desc) = &node.description {
        if !desc.trim().is_empty() {
            return true;
        }
    }
    false
}

/// Check if field is marked as required
fn is_required(node: &AXNode) -> bool {
    node.get_property_bool("required").unwrap_or(false)
}

/// Check if required status is indicated
fn indicates_required(node: &AXNode) -> bool {
    if let Some(name) = &node.name {
        let name_lower = name.to_lowercase();
        let required_terms = [
            "required",
            // German
            "pflichtfeld",
            "pflicht",
            "erforderlich",
            // French
            "obligatoire",
            "requis",
            "champ obligatoire",
            // Spanish
            "obligatorio",
            "requerido",
            "campo requerido",
            // Italian
            "obbligatorio",
            "richiesto",
            // Portuguese
            "obrigatório",
            "campo obrigatório",
            // Dutch
            "verplicht",
            "vereist",
            // Swedish
            "obligatoriskt",
            "krävs",
            // Polish
            "wymagane",
            // Turkish
            "zorunlu",
        ];
        if required_terms
            .iter()
            .any(|t| name_lower.contains(t) || name_lower.contains('*'))
        {
            return true;
        }
    }

    if let Some(desc) = &node.description {
        let desc_lower = desc.to_lowercase();
        if desc_lower.contains("required")
            || desc_lower.contains("pflichtfeld")
            || desc_lower.contains("obligatoire")
            || desc_lower.contains("obligatorio")
            || desc_lower.contains("obbligatorio")
            || desc_lower.contains("obrigatório")
            || desc_lower.contains("verplicht")
        {
            return true;
        }
    }

    false
}

/// Check if input type typically needs format instructions
fn needs_format_instructions(role: &str, node: &AXNode) -> bool {
    let name = node.name.as_deref().unwrap_or("").to_lowercase();

    let format_sensitive = [
        // English
        "date",
        "phone",
        "tel",
        "zip",
        "postal",
        "credit card",
        "ssn",
        "social security",
        "passport",
        "account",
        "routing",
        // German
        "datum",
        "telefon",
        "postleitzahl",
        "plz",
        "kreditkarte",
        "reisepass",
        "kontonummer",
        // French
        "téléphone",
        "code postal",
        "carte de crédit",
        "passeport",
        "numéro de compte",
        // Spanish
        "teléfono",
        "código postal",
        "tarjeta de crédito",
        "pasaporte",
        "número de cuenta",
        // Italian
        "telefono",
        "codice postale",
        "carta di credito",
        "passaporto",
        "numero di conto",
        // Portuguese
        "telefone",
        "código postal",
        "cartão de crédito",
        "passaporte",
        // Dutch
        "telefoon",
        "postcode",
        "creditcard",
        "paspoort",
        "rekeningnummer",
        // Swedish
        "telefon",
        "postnummer",
        "kreditkort",
        "pass",
        // Polish
        "telefon",
        "kod pocztowy",
        "karta kredytowa",
        "paszport",
        // Turkish
        "telefon",
        "posta kodu",
        "kredi kartı",
        "pasaport",
    ];

    format_sensitive.iter().any(|&term| name.contains(term)) || role == "spinbutton"
}

/// Check if format hint is provided
fn has_format_hint(node: &AXNode) -> bool {
    let format_patterns = [
        "format:",
        "example:",
        "e.g.",
        "(",
        "mm/dd",
        "yyyy",
        // German
        "z.b.",
        "bsp.",
        "beispiel:",
        // French
        "ex.",
        "exemple:",
        "par ex.",
        // Spanish
        "ej.",
        "ejemplo:",
        "p.ej.",
        // Italian
        "es.",
        "esempio:",
        // Portuguese
        "ex.:",
        "exemplo:",
        // Dutch
        "bijv.",
        "voorbeeld:",
        // Swedish/Norwegian
        "t.ex.",
        // Polish
        "np.",
        "przykład:",
    ];

    if let Some(name) = &node.name {
        let name_lower = name.to_lowercase();
        if format_patterns.iter().any(|p| name_lower.contains(p)) {
            return true;
        }
    }

    if let Some(desc) = &node.description {
        let desc_lower = desc.to_lowercase();
        if format_patterns.iter().any(|p| desc_lower.contains(p)) {
            return true;
        }
    }

    has_placeholder(node)
}

/// A node is a real form group only when it has fieldset semantics OR
/// contains at least one descendant that is a form control. This filters
/// out <details>, <address>, <hgroup> and other generic groupings that
/// Chrome exposes as role="group" but are not form-related.
fn is_form_group(node: &AXNode, tree: &AXTree) -> bool {
    // Explicit fieldset: htmlTag check
    if let Some(tag) = node.get_property_str("htmlTag") {
        let tag_up = tag.to_uppercase();
        if tag_up == "FIELDSET" {
            return true;
        }
        // Common non-form groups exposed as role="group" by Chrome
        if matches!(
            tag_up.as_str(),
            "DETAILS" | "ADDRESS" | "HGROUP" | "FIGURE" | "ARTICLE" | "SECTION" | "ASIDE"
        ) {
            return false;
        }
    }

    // Fallback: group counts as a form group only if it contains a form control
    has_form_control_descendant(node, tree, 0)
}

fn has_form_control_descendant(node: &AXNode, tree: &AXTree, depth: usize) -> bool {
    if depth > 8 {
        return false;
    }
    for child_id in &node.child_ids {
        if let Some(child) = tree.nodes.get(child_id) {
            let role = child.role.as_deref().unwrap_or("").to_lowercase();
            if is_form_input(&role) {
                return true;
            }
            if has_form_control_descendant(child, tree, depth + 1) {
                return true;
            }
        }
    }
    false
}

/// Check if group has a label
fn has_group_label(node: &AXNode) -> bool {
    if let Some(name) = &node.name {
        if !name.trim().is_empty() {
            return true;
        }
    }
    false
}

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

    fn create_input(id: &str, role: &str, name: Option<&str>) -> AXNode {
        AXNode {
            node_id: id.to_string(),
            ignored: false,
            ignored_reasons: vec![],
            role: Some(role.to_string()),
            name: name.map(String::from),
            name_source: None,
            description: None,
            value: None,
            properties: vec![],
            child_ids: vec![],
            parent_id: None,
            backend_dom_node_id: None,
        }
    }

    fn create_input_with_required(
        id: &str,
        role: &str,
        name: Option<&str>,
        required: bool,
    ) -> AXNode {
        let mut node = create_input(id, role, name);
        if required {
            node.properties.push(AXProperty {
                name: "required".to_string(),
                value: AXValue::Bool(true),
            });
        }
        node
    }

    #[test]
    fn test_instructions_rule_metadata() {
        assert_eq!(INSTRUCTIONS_RULE.id, "3.3.2");
        assert_eq!(INSTRUCTIONS_RULE.level, WcagLevel::A);
    }

    #[test]
    fn test_is_form_input() {
        assert!(is_form_input("textbox"));
        assert!(is_form_input("checkbox"));
        assert!(is_form_input("combobox"));
        assert!(!is_form_input("button"));
        assert!(!is_form_input("link"));
    }

    #[test]
    fn test_input_without_label() {
        let tree = AXTree::from_nodes(vec![create_input("1", "textbox", None)]);
        let results = check_instructions(&tree);
        assert!(results
            .violations
            .iter()
            .any(|v| v.message.contains("no accessible label")));
    }

    #[test]
    fn test_input_with_label() {
        let tree = AXTree::from_nodes(vec![create_input("1", "textbox", Some("Email address"))]);
        let results = check_instructions(&tree);
        assert!(!results
            .violations
            .iter()
            .any(|v| v.message.contains("no accessible label")));
    }

    #[test]
    fn test_required_without_indication() {
        let tree = AXTree::from_nodes(vec![create_input_with_required(
            "1",
            "textbox",
            Some("Name"),
            true,
        )]);
        let results = check_instructions(&tree);
        assert!(results
            .violations
            .iter()
            .any(|v| v.message.contains("Required field")));
    }

    #[test]
    fn test_required_with_indication() {
        let tree = AXTree::from_nodes(vec![create_input_with_required(
            "1",
            "textbox",
            Some("Name (required)"),
            true,
        )]);
        let results = check_instructions(&tree);
        assert!(!results
            .violations
            .iter()
            .any(|v| v.message.contains("Required field not clearly indicated")));
    }

    fn group_with_html_tag(id: &str, tag: &str, name: Option<&str>) -> AXNode {
        let mut n = create_input(id, "group", name);
        n.properties.push(AXProperty {
            name: "htmlTag".into(),
            value: AXValue::String(tag.into()),
        });
        n
    }

    #[test]
    fn test_details_not_flagged_as_form_group() {
        let tree = AXTree::from_nodes(vec![group_with_html_tag("1", "DETAILS", None)]);
        let results = check_instructions(&tree);
        assert!(
            !results
                .violations
                .iter()
                .any(|v| v.message.contains("Form group has no legend")),
            "<details> must not be flagged as a form group missing a legend"
        );
    }

    #[test]
    fn test_address_not_flagged_as_form_group() {
        let tree = AXTree::from_nodes(vec![group_with_html_tag("1", "ADDRESS", None)]);
        let results = check_instructions(&tree);
        assert!(!results
            .violations
            .iter()
            .any(|v| v.message.contains("Form group has no legend")));
    }

    #[test]
    fn test_fieldset_without_legend_still_flagged() {
        // A FIELDSET htmlTag with no name must still trigger the form-group rule.
        let tree = AXTree::from_nodes(vec![group_with_html_tag("1", "FIELDSET", None)]);
        let results = check_instructions(&tree);
        assert!(results
            .violations
            .iter()
            .any(|v| v.message.contains("Form group has no legend")));
    }

    #[test]
    fn test_generic_group_with_form_control_flagged() {
        // A generic "group" that contains a form control must be flagged when
        // it has no accessible name.
        let mut group = create_input("g", "group", None);
        group.child_ids.push("input".into());
        let input = create_input("input", "textbox", Some("Email"));
        let tree = AXTree::from_nodes(vec![group, input]);
        let results = check_instructions(&tree);
        assert!(results
            .violations
            .iter()
            .any(|v| v.message.contains("Form group has no legend")));
    }
}