auditmysite 1.2.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
//! Accessibility Tree (AXTree) data structures
//!
//! Represents Chrome's Accessibility Tree as extracted via CDP.
//! The AXTree provides semantic information about page elements.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// The complete Accessibility Tree for a page
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AXTree {
    /// All nodes in the tree, indexed by node_id
    pub nodes: HashMap<String, AXNode>,
    /// The root node ID
    pub root_id: Option<String>,
}

impl AXTree {
    /// Create a new empty AXTree
    pub fn new() -> Self {
        Self {
            nodes: HashMap::new(),
            root_id: None,
        }
    }

    /// Build tree from a list of nodes
    pub fn from_nodes(nodes: Vec<AXNode>) -> Self {
        let mut tree = Self::new();

        for node in nodes {
            // First node is typically the root
            if tree.root_id.is_none() {
                tree.root_id = Some(node.node_id.clone());
            }
            tree.nodes.insert(node.node_id.clone(), node);
        }

        tree
    }

    /// Get a node by ID
    pub fn get_node(&self, node_id: &str) -> Option<&AXNode> {
        self.nodes.get(node_id)
    }

    /// Get the root node
    pub fn root(&self) -> Option<&AXNode> {
        self.root_id.as_ref().and_then(|id| self.nodes.get(id))
    }

    /// Iterate over all auditable nodes (excludes browser-generated artifacts).
    /// This is the default iterator that all WCAG rules should use.
    pub fn iter(&self) -> impl Iterator<Item = &AXNode> {
        self.nodes.values().filter(|n| !n.is_browser_generated())
    }

    /// Iterate over ALL nodes including browser-generated artifacts.
    /// Only use this for tree traversal, parent/child lookups, or non-WCAG analysis.
    pub fn iter_all(&self) -> impl Iterator<Item = &AXNode> {
        self.nodes.values()
    }

    /// Get all nodes with a specific role (excludes browser-generated nodes)
    pub fn nodes_with_role(&self, role: &str) -> Vec<&AXNode> {
        self.iter()
            .filter(|n| n.role.as_deref() == Some(role))
            .collect()
    }

    /// Get all image nodes
    pub fn images(&self) -> Vec<&AXNode> {
        self.iter()
            .filter(|n| matches!(n.role.as_deref(), Some("image") | Some("img")))
            .collect()
    }

    /// Get all heading nodes
    pub fn headings(&self) -> Vec<&AXNode> {
        self.iter()
            .filter(|n| matches!(n.role.as_deref(), Some("heading")))
            .collect()
    }

    /// Get all form control nodes (excluding buttons, which are checked separately)
    pub fn form_controls(&self) -> Vec<&AXNode> {
        self.iter()
            .filter(|n| {
                matches!(
                    n.role.as_deref(),
                    Some("textbox")
                        | Some("checkbox")
                        | Some("radio")
                        | Some("combobox")
                        | Some("listbox")
                        | Some("spinbutton")
                        | Some("slider")
                        | Some("searchbox")
                )
            })
            .collect()
    }

    /// Get all link nodes
    pub fn links(&self) -> Vec<&AXNode> {
        self.nodes_with_role("link")
    }

    /// Count total nodes
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    /// Check if tree is empty
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }
}

impl Default for AXTree {
    fn default() -> Self {
        Self::new()
    }
}

/// A single node in the Accessibility Tree
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AXNode {
    /// Unique identifier for this node
    pub node_id: String,
    /// Whether this node is ignored for accessibility
    #[serde(default)]
    pub ignored: bool,
    /// Reasons why this node is ignored
    #[serde(default)]
    pub ignored_reasons: Vec<AXProperty>,
    /// The accessibility role (e.g., "button", "heading", "image")
    pub role: Option<String>,
    /// The accessible name (what screen readers announce)
    pub name: Option<String>,
    /// Source of the accessible name
    pub name_source: Option<NameSource>,
    /// The accessible description
    pub description: Option<String>,
    /// The accessible value (for form controls)
    pub value: Option<String>,
    /// Additional properties
    #[serde(default)]
    pub properties: Vec<AXProperty>,
    /// Child node IDs
    #[serde(default)]
    pub child_ids: Vec<String>,
    /// Parent node ID
    pub parent_id: Option<String>,
    /// Backend DOM node ID (for correlation with DOM)
    pub backend_dom_node_id: Option<i64>,
}

impl AXNode {
    /// Check if this node has an accessible name
    pub fn has_name(&self) -> bool {
        self.name.as_ref().is_some_and(|n| !n.trim().is_empty())
    }

    /// Check if this node is focusable
    pub fn is_focusable(&self) -> bool {
        self.get_property_bool("focusable").unwrap_or(false)
    }

    /// Check if this node is interactive
    pub fn is_interactive(&self) -> bool {
        matches!(
            self.role.as_deref(),
            Some("button")
                | Some("link")
                | Some("textbox")
                | Some("checkbox")
                | Some("radio")
                | Some("combobox")
                | Some("menuitem")
                | Some("tab")
        ) || self.is_focusable()
    }

    /// Get heading level (1-6) if this is a heading
    pub fn heading_level(&self) -> Option<u8> {
        if self.role.as_deref() != Some("heading") {
            return None;
        }

        self.get_property_int("level").map(|l| l.clamp(1, 6) as u8)
    }

    /// Get a boolean property value
    pub fn get_property_bool(&self, name: &str) -> Option<bool> {
        self.properties
            .iter()
            .find(|p| p.name == name)
            .and_then(|p| p.value.as_bool())
    }

    /// Get an integer property value
    pub fn get_property_int(&self, name: &str) -> Option<i64> {
        self.properties
            .iter()
            .find(|p| p.name == name)
            .and_then(|p| p.value.as_int())
    }

    /// Get a string property value
    pub fn get_property_str(&self, name: &str) -> Option<&str> {
        self.properties
            .iter()
            .find(|p| p.name == name)
            .and_then(|p| p.value.as_str())
    }

    /// Returns true if a property with this name exists, regardless of value type.
    /// Use this for relationship attributes (controls, owns, …) whose CDP values are
    /// node references rather than plain strings.
    pub fn has_property(&self, name: &str) -> bool {
        self.properties.iter().any(|p| p.name == name)
    }

    /// Get the first idref from a relationship property.
    /// Handles both `AXValue::String` (test fixtures / enriched properties) and
    /// `AXValue::Node` (real CDP idref/idrefList traffic).
    pub fn get_property_idref(&self, name: &str) -> Option<&str> {
        self.properties
            .iter()
            .find(|p| p.name == name)
            .and_then(|p| match &p.value {
                AXValue::String(s) => Some(s.as_str()),
                AXValue::Node { related_nodes } => related_nodes.first()?.idref.as_deref(),
                _ => None,
            })
    }

    /// Get all idrefs from a multi-target relationship property (e.g. aria-owns).
    /// Handles both `AXValue::String` (space-separated IDs) and `AXValue::Node`.
    pub fn get_property_idrefs(&self, name: &str) -> Vec<&str> {
        self.properties
            .iter()
            .find(|p| p.name == name)
            .map(|p| match &p.value {
                AXValue::String(s) => s.split_whitespace().collect(),
                AXValue::Node { related_nodes } => related_nodes
                    .iter()
                    .filter_map(|n| n.idref.as_deref())
                    .collect(),
                _ => vec![],
            })
            .unwrap_or_default()
    }

    /// Check if the node has a specific role
    pub fn has_role(&self, role: &str) -> bool {
        self.role.as_deref() == Some(role)
    }

    /// Returns true if this node is a browser-generated artifact that no WCAG
    /// rule should evaluate. These roles are injected by the rendering engine
    /// and do not represent author content — flagging them is always a false
    /// positive.
    pub fn is_browser_generated(&self) -> bool {
        matches!(
            self.role.as_deref(),
            Some("StaticText")
                | Some("InlineTextBox")
                | Some("LineBreak")
                | Some("ListMarker")
                | Some("LayoutTable")
                | Some("LayoutTableRow")
                | Some("LayoutTableCell")
        )
    }

    /// Get the required property (for form validation)
    pub fn is_required(&self) -> bool {
        self.get_property_bool("required").unwrap_or(false)
    }

    /// Get the invalid property (for form validation)
    pub fn is_invalid(&self) -> bool {
        self.get_property_bool("invalid").unwrap_or(false)
    }
}

/// Source of an accessible name
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum NameSource {
    /// Name from attribute (aria-label, alt, title)
    Attribute,
    /// Name from associated label element
    RelatedElement,
    /// Name from content/children
    Contents,
    /// Name from placeholder
    Placeholder,
    /// Name from title attribute
    Title,
}

/// A property of an AXNode
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AXProperty {
    /// Property name
    pub name: String,
    /// Property value
    pub value: AXValue,
}

/// Value of an AXProperty
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AXValue {
    /// Boolean value
    Bool(bool),
    /// Integer value
    Int(i64),
    /// Float value
    Float(f64),
    /// String value
    String(String),
    /// Related node
    Node { related_nodes: Vec<RelatedNode> },
    /// List of values
    List(Vec<AXValue>),
}

impl AXValue {
    /// Get as boolean if applicable
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            AXValue::Bool(b) => Some(*b),
            _ => None,
        }
    }

    /// Get as integer if applicable
    pub fn as_int(&self) -> Option<i64> {
        match self {
            AXValue::Int(i) => Some(*i),
            AXValue::Float(f) => Some(*f as i64),
            _ => None,
        }
    }

    /// Get as string if applicable
    pub fn as_str(&self) -> Option<&str> {
        match self {
            AXValue::String(s) => Some(s),
            _ => None,
        }
    }
}

/// A related node reference
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelatedNode {
    /// The related node's backend DOM node ID
    pub backend_dom_node_id: Option<i64>,
    /// The related node's IDREF
    pub idref: Option<String>,
    /// Text content of the related node
    pub text: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_node(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,
        }
    }

    #[test]
    fn test_axtree_from_nodes() {
        let nodes = vec![
            create_test_node("1", "WebArea", Some("Page")),
            create_test_node("2", "heading", Some("Title")),
            create_test_node("3", "image", None),
        ];

        let tree = AXTree::from_nodes(nodes);
        assert_eq!(tree.len(), 3);
        assert_eq!(tree.root_id, Some("1".to_string()));
    }

    #[test]
    fn test_axtree_images() {
        let nodes = vec![
            create_test_node("1", "WebArea", Some("Page")),
            create_test_node("2", "image", Some("Logo")),
            create_test_node("3", "image", None),
            create_test_node("4", "heading", Some("Title")),
        ];

        let tree = AXTree::from_nodes(nodes);
        let images = tree.images();
        assert_eq!(images.len(), 2);
    }

    #[test]
    fn test_axtree_headings() {
        let nodes = vec![
            create_test_node("1", "WebArea", Some("Page")),
            create_test_node("2", "heading", Some("Title")),
            create_test_node("3", "heading", Some("Subtitle")),
        ];

        let tree = AXTree::from_nodes(nodes);
        let headings = tree.headings();
        assert_eq!(headings.len(), 2);
    }

    #[test]
    fn test_axnode_has_name() {
        let node_with_name = create_test_node("1", "image", Some("Logo"));
        let node_without_name = create_test_node("2", "image", None);
        let node_empty_name = create_test_node("3", "image", Some("  "));

        assert!(node_with_name.has_name());
        assert!(!node_without_name.has_name());
        assert!(!node_empty_name.has_name());
    }

    #[test]
    fn test_axnode_heading_level() {
        let mut heading = create_test_node("1", "heading", Some("Title"));
        heading.properties.push(AXProperty {
            name: "level".to_string(),
            value: AXValue::Int(2),
        });

        assert_eq!(heading.heading_level(), Some(2));

        let non_heading = create_test_node("2", "paragraph", Some("Text"));
        assert_eq!(non_heading.heading_level(), None);
    }
}