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
//! WCAG 2.1.1 Keyboard Accessibility
//!
//! Ensures that all functionality is operable through a keyboard interface.
//! Level A - Critical for users who cannot use a mouse.
use crate::accessibility::AXTree;
use crate::cli::WcagLevel;
use crate::wcag::types::{FindingKind, RuleMetadata, Severity, Violation, WcagResults};
/// Rule metadata for 2.1.1
pub const KEYBOARD_RULE: RuleMetadata = RuleMetadata {
id: "2.1.1",
name: "Keyboard",
level: WcagLevel::A,
severity: Severity::Critical,
description: "All functionality must be operable through a keyboard interface",
help_url: "https://www.w3.org/WAI/WCAG21/Understanding/keyboard.html",
axe_id: "keyboard",
tags: &["wcag2a", "wcag211", "cat.keyboard"],
};
/// Rule metadata for the "focusable without interactive role" sub-check.
/// Same WCAG criterion as `KEYBOARD_RULE` but a distinct `axe_id` — this lets
/// `output::explanations::get_explanation` resolve a customer-facing
/// explanation specific to this case instead of falling back to
/// `KEYBOARD_RULE`'s generic 2.1.1 text, which is written around "add
/// tabindex + a keydown handler" and does not fit here: the element is
/// already focusable, what it lacks is a role (#571).
pub const FOCUSABLE_NO_ROLE_RULE: RuleMetadata = RuleMetadata {
id: "2.1.1",
name: "Keyboard",
level: WcagLevel::A,
severity: Severity::Critical,
description: "All functionality must be operable through a keyboard interface",
help_url: "https://www.w3.org/WAI/WCAG21/Understanding/keyboard.html",
axe_id: "focusable-no-role",
tags: &["wcag2a", "wcag211", "cat.keyboard"],
};
/// Rule metadata for 2.1.2
pub const NO_KEYBOARD_TRAP_RULE: RuleMetadata = RuleMetadata {
id: "2.1.2",
name: "No Keyboard Trap",
level: WcagLevel::A,
severity: Severity::Critical,
description: "If keyboard focus can be moved to a component, focus can be moved away",
help_url: "https://www.w3.org/WAI/WCAG21/Understanding/no-keyboard-trap.html",
axe_id: "keyboard-trap",
tags: &["wcag2a", "wcag212", "cat.keyboard"],
};
/// Check for keyboard accessibility issues
///
/// Positive-tabindex detection used to live here too, but `tabindex` is not
/// an AX property (dead code, #QA-030) and the concern is really about focus
/// *order*, not keyboard operability — it is now the sole responsibility of
/// `focus_order.rs`'s DOM-based `check_positive_tabindex_with_page` (2.4.3).
pub fn check_keyboard(tree: &AXTree) -> WcagResults {
let mut results = WcagResults::new();
for node in tree.iter() {
if node.ignored {
continue;
}
results.nodes_checked += 1;
// Check for non-interactive elements made focusable without proper role.
// Reported as Warning (issue #569): the AX tree only shows a structural
// mismatch (focusable, but no interactive role) — whether the element is
// actually operable via keyboard depends on JS event handling that this
// static check cannot observe, so this is a review hint, not a confirmed
// violation.
if is_focusable_without_interactive_role(node) {
let violation = Violation::new(
KEYBOARD_RULE.id,
KEYBOARD_RULE.name,
KEYBOARD_RULE.level,
Severity::Low,
"Focusable element without interactive role — may not be operable via keyboard; verify manually",
&node.node_id,
)
.with_role(node.role.clone())
.with_name(node.name.clone())
.with_fix("Add an appropriate ARIA role or use a native interactive element")
.with_help_url(KEYBOARD_RULE.help_url)
.with_rule_id(FOCUSABLE_NO_ROLE_RULE.axe_id)
.as_warning();
results.add_violation(violation);
}
// Check for interactive ARIA role on non-focusable element (issue #34).
// Reported as Warning: the AX tree `focusable` attribute may not
// reflect JS-added tab handling, so a definitive violation requires
// interactive testing.
if has_interactive_role_but_not_focusable(node) {
let role = node.role.as_deref().unwrap_or("interactive");
let warning = Violation::new(
KEYBOARD_RULE.id,
KEYBOARD_RULE.name,
KEYBOARD_RULE.level,
Severity::High,
format!(
"Element has role=\"{role}\" but appears not keyboard-focusable — verify that tabindex or JS event handling is present"
),
&node.node_id,
)
.with_role(node.role.clone())
.with_name(node.name.clone())
.with_fix(
"Add tabindex=\"0\" to make the element focusable, or use the native HTML element (e.g. <button>, <a>).",
)
.with_help_url(KEYBOARD_RULE.help_url)
.with_rule_id(KEYBOARD_RULE.axe_id)
.as_warning();
results.add_violation(warning);
}
// Check for potential keyboard traps (modal dialogs). Reported as Warning
// (issue #569): the AX tree only confirms a modal dialog exists — it does
// NOT confirm focus is actually trapped, only that a keyboard test is
// needed to rule that out. A flat Violation here overclaims what was
// measured (compare the sibling `has_interactive_role_but_not_focusable`
// check above, which uses the same Warning + reduced-but-not-zeroed
// severity treatment for the same reason).
if is_potential_keyboard_trap(node) {
let violation = Violation::new(
NO_KEYBOARD_TRAP_RULE.id,
NO_KEYBOARD_TRAP_RULE.name,
NO_KEYBOARD_TRAP_RULE.level,
Severity::High,
"Modal dialog detected — verify with keyboard that focus can be moved away (Escape, Tab, Shift+Tab)",
&node.node_id,
)
.with_role(node.role.clone())
.with_name(node.name.clone())
.with_fix("Ensure focus can be moved away using standard keyboard navigation")
.with_help_url(NO_KEYBOARD_TRAP_RULE.help_url)
.with_rule_id(NO_KEYBOARD_TRAP_RULE.axe_id)
.as_warning();
results.add_violation(violation);
}
}
// 2.1.2 No Keyboard Trap — structural check only detects modal `modal=true` dialogs.
// JavaScript focus management in custom widgets requires manual Tab-key verification.
results.add_violation(
Violation::new(
NO_KEYBOARD_TRAP_RULE.id,
NO_KEYBOARD_TRAP_RULE.name,
NO_KEYBOARD_TRAP_RULE.level,
Severity::Medium,
"Keyboard trap behavior cannot be verified automatically. \
Navigate the page using only the Tab key to confirm focus is never permanently trapped \
in dialogs, carousels, or custom JavaScript widgets.",
"page",
)
.with_fix(
"Ensure every focusable region has a keyboard escape path (Escape key, visible close button reachable by Tab, or documented keyboard shortcut).",
)
.with_help_url(NO_KEYBOARD_TRAP_RULE.help_url)
.with_rule_id(NO_KEYBOARD_TRAP_RULE.axe_id)
.with_kind(FindingKind::NotTestable),
);
results
}
/// Check if element is focusable but lacks interactive role
fn is_focusable_without_interactive_role(node: &crate::accessibility::AXNode) -> bool {
// `tabindex` is not an AX property (#QA-030); `focusable` is the real,
// Chrome-computed property that already reflects tabindex's effect on
// focusability, so it fully covers what the removed tabindex read
// attempted (see the equivalent fix in `focus_visible.rs`).
let is_focusable = node.get_property_bool("focusable").unwrap_or(false);
let non_interactive_roles = [
"generic",
"group",
"region",
"article",
"section",
"paragraph",
"statictext",
"none",
"presentation",
];
is_focusable
&& node
.role
.as_deref()
.map(|r| non_interactive_roles.contains(&r.to_lowercase().as_str()))
.unwrap_or(true)
}
/// Check if element has an interactive ARIA role but is not focusable.
/// Catches `<div role="button">` without `tabindex` — keyboard-unreachable widgets.
/// Native interactive elements (`<button>`, `<a>`, `<input>`) are auto-focusable
/// even without tabindex, so this only flags ARIA-roled non-native elements.
fn has_interactive_role_but_not_focusable(node: &crate::accessibility::AXNode) -> bool {
let role = match node.role.as_deref() {
Some(r) => r.to_lowercase(),
None => return false,
};
let interactive_roles = [
"button",
"link",
"checkbox",
"radio",
"switch",
"menuitem",
"menuitemcheckbox",
"menuitemradio",
"tab",
"option",
"treeitem",
];
if !interactive_roles.contains(&role.as_str()) {
return false;
}
// Native interactive elements are focusable=true automatically.
// Only flag when role is interactive but element is not focusable.
let is_focusable = node.get_property_bool("focusable").unwrap_or(false);
!is_focusable
}
/// Check for potential keyboard traps
fn is_potential_keyboard_trap(node: &crate::accessibility::AXNode) -> bool {
let role = node.role.as_deref().unwrap_or("").to_lowercase();
if role == "dialog" || role == "alertdialog" {
if let Some(true) = node.get_property_bool("modal") {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
use crate::accessibility::{AXNode, AXProperty, AXValue};
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_keyboard_rule_metadata() {
assert_eq!(KEYBOARD_RULE.id, "2.1.1");
assert_eq!(KEYBOARD_RULE.level, WcagLevel::A);
}
// Positive-tabindex detection moved to focus_order.rs's DOM-based
// check_positive_tabindex_with_page (2.4.3) — see that module's tests.
fn create_node_with_focusable(id: &str, role: &str, focusable: bool) -> AXNode {
let mut node = create_test_node(id, role, None);
node.properties.push(AXProperty {
name: "focusable".to_string(),
value: AXValue::Bool(focusable),
});
node
}
#[test]
fn test_role_button_without_focusable_flagged() {
// <div role="button"> without tabindex → focusable=false → warning (heuristic)
let tree = AXTree::from_nodes(vec![create_node_with_focusable("1", "button", false)]);
let results = check_keyboard(&tree);
assert!(
results
.warnings
.iter()
.any(|v| v.message.contains("not keyboard-focusable")),
"Expected warning for role=button without focusable; got warnings: {:?}",
results
.warnings
.iter()
.map(|v| &v.message)
.collect::<Vec<_>>()
);
}
#[test]
fn test_native_button_with_focusable_passes() {
// Native <button> is focusable=true → no violation
let tree = AXTree::from_nodes(vec![create_node_with_focusable("1", "button", true)]);
let results = check_keyboard(&tree);
assert!(!results
.violations
.iter()
.any(|v| v.message.contains("not keyboard-focusable")));
}
#[test]
fn test_non_interactive_role_not_flagged() {
// role="generic" without focusable is not a #34 violation (different rule covers that)
let tree = AXTree::from_nodes(vec![create_node_with_focusable("1", "generic", false)]);
let results = check_keyboard(&tree);
assert!(!results
.violations
.iter()
.any(|v| v.message.contains("not keyboard-focusable")));
}
#[test]
fn test_focusable_without_interactive_role_is_warning() {
// Issue #569: structural inference only (focusable + non-interactive
// role) — whether the element is actually unusable via keyboard needs
// manual verification, so this must be a Warning, not a Violation.
let tree = AXTree::from_nodes(vec![create_node_with_focusable("1", "generic", true)]);
let results = check_keyboard(&tree);
let finding = results
.warnings
.iter()
.find(|v| {
v.message
.contains("Focusable element without interactive role")
})
.expect("expected a warning-kind finding");
assert_eq!(finding.kind, FindingKind::Warning);
// Regression for #571: this finding must carry the distinct
// "focusable-no-role" rule_id, not the shared "keyboard" id, so
// `output::explanations::get_explanation` resolves the role-specific
// explanation instead of KEYBOARD_RULE's generic tabindex+keydown text.
assert_eq!(
finding.rule_id.as_deref(),
Some(FOCUSABLE_NO_ROLE_RULE.axe_id)
);
assert!(!results.violations.iter().any(|v| v
.message
.contains("Focusable element without interactive role")));
}
#[test]
fn test_modal_dialog_keyboard_trap_is_warning() {
// Issue #569: the AX tree only confirms a modal dialog exists, not that
// focus is actually trapped — reclassified from Violation to Warning
// (severity kept at High, following the sibling
// has_interactive_role_but_not_focusable precedent of not zeroing out
// severity just because a finding is a Warning).
let mut node = create_test_node("1", "dialog", None);
node.properties.push(AXProperty {
name: "modal".to_string(),
value: AXValue::Bool(true),
});
let tree = AXTree::from_nodes(vec![node]);
let results = check_keyboard(&tree);
let finding = results
.warnings
.iter()
.find(|v| v.rule == NO_KEYBOARD_TRAP_RULE.id && v.node_id == "1")
.expect("expected a warning-kind finding for the modal dialog");
assert_eq!(finding.kind, FindingKind::Warning);
assert_eq!(finding.severity, Severity::High);
assert!(!results
.violations
.iter()
.any(|v| v.rule == NO_KEYBOARD_TRAP_RULE.id && v.node_id == "1"));
}
}