From fec93ba50781fcd8ab76b1f4daccd6054f9e3111 Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Thu, 22 Oct 2026 23:55:00 -0600 Subject: [PATCH 1/2] feat(a11y): a live tree, and text inputs assistive tech reads and fills The accessibility tree is rebuilt after every redraw, once the frame's changes are resolved. Upstream rebuilds it on a poll whose document `has_changes`, which returns `changed_nodes.is_empty()`; `changed_nodes` only records node creation and is never cleared, so after the first node the tree was never rebuilt, and an app built on dioxus-native's launcher (Diaryx) kept its first screen in the tree forever. Leaf's app worked round it by rebuilding after each redraw itself; that call is now redundant. A text input's node has the editor's text as its value (a password's as one dot a character), its placeholder, and the Focus and SetValue actions unless it is disabled (and SetValue unless read-only). `View::accessibility_action` carries them out, and BlitzApplication calls it before falling back to the click events: Focus focuses the node; SetValue focuses the input, selects all of it (`BaseDocument::select_all_in_text_input`) and commits the new text as an IME commit, so the page hears an `input` event as from typing. Behavioural-change: the accessibility tree follows the document after its first build, rebuilt after every redraw while assistive tech listens. Behavioural-change: text inputs expose their value, placeholder and the Focus and SetValue actions, and BlitzApplication carries those actions out. --- FORK.md | 7 ++ packages/blitz-dom/src/accessibility.rs | 24 +++++ packages/blitz-dom/src/document.rs | 19 ++++ packages/blitz-shell/src/application.rs | 6 +- packages/blitz-shell/src/window.rs | 57 ++++++++++-- .../tests/accessibility_text_input.rs | 92 +++++++++++++++++++ tests/blitz-tests/tests/all.rs | 1 + 7 files changed, 194 insertions(+), 12 deletions(-) create mode 100644 tests/blitz-tests/tests/accessibility_text_input.rs diff --git a/FORK.md b/FORK.md index ee95ef1363..e0a8753d6a 100644 --- a/FORK.md +++ b/FORK.md @@ -79,6 +79,13 @@ page: - A hook through which the embedder changes each tree built before the adapter sees it (`View::set_accessibility_tree_hook`): leaf's editor becomes a text area there, with its value, text runs and selection. +- The tree rebuilt after every redraw, where the changes are resolved: + upstream rebuilds it on a poll that `has_changes`, which is never true + after the first node is made, so the tree kept the first screen. +- A text input's text as its value (a password's as dots), its placeholder, + and the `Focus` and `SetValue` actions, carried out by + `View::accessibility_action`: `SetValue` focuses the input, selects all of + it and commits the new text, which the page hears as an `input` event. Events and layout, found by diaryx-org/unit's gallery: diff --git a/packages/blitz-dom/src/accessibility.rs b/packages/blitz-dom/src/accessibility.rs index 684fc53ed3..72c01458bc 100644 --- a/packages/blitz-dom/src/accessibility.rs +++ b/packages/blitz-dom/src/accessibility.rs @@ -164,6 +164,30 @@ impl BaseDocument { write_aria_states(element_data, role, &mut builder); + // A text field's text is its editor's, not the page's: no text + // node holds it. It is the field's value, which assistive tech + // may replace, and the field may be focused. + if let Some(input) = element_data.text_input_data() { + let text = input.editor.raw_text(); + if element_data.attr(local_name!("type")) == Some("password") { + builder.set_value("•".repeat(text.chars().count())); + } else { + builder.set_value(text); + } + if let Some(placeholder) = element_data.attr(local_name!("placeholder")) { + builder.set_placeholder(placeholder); + } + if element_data.attr(local_name!("readonly")).is_some() { + builder.set_read_only(); + } + if !builder.is_disabled() { + builder.add_action(Action::Focus); + if !builder.is_read_only() { + builder.add_action(Action::SetValue); + } + } + } + if let Some(aria_selected) = element_data.attr(local_name!("aria-selected")) && let Ok(aria_selected) = aria_selected.parse() { diff --git a/packages/blitz-dom/src/document.rs b/packages/blitz-dom/src/document.rs index 14bb060aa3..1fc17ede65 100644 --- a/packages/blitz-dom/src/document.rs +++ b/packages/blitz-dom/src/document.rs @@ -632,6 +632,25 @@ impl BaseDocument { self.nodes.get_mut(node_id) } + /// Selects all the text of the text input `node_id`, as Select All does, + /// so that the text committed next replaces it. Whether `node_id` is a + /// text input. + pub fn select_all_in_text_input(&mut self, node_id: NodeId) -> bool { + let Some(input) = self + .nodes + .get_mut(node_id) + .and_then(|node| node.element_data_mut()) + .and_then(|el| el.text_input_data_mut()) + else { + return false; + }; + input + .editor + .driver(&mut self.font_ctx.lock().unwrap(), &mut self.layout_ctx) + .select_all(); + true + } + pub fn get_focussed_node_id(&self) -> Option { self.focus_node_id .or(self.try_root_element().map(|el| el.id)) diff --git a/packages/blitz-shell/src/application.rs b/packages/blitz-shell/src/application.rs index 668509a66f..5c6459a098 100644 --- a/packages/blitz-shell/src/application.rs +++ b/packages/blitz-shell/src/application.rs @@ -87,8 +87,10 @@ impl BlitzApplication { accesskit_xplat::WindowEvent::ActionRequested(request) => { // A click is carried out as a click, through the // window's own events. - for event in window.accessibility_action_events(request) { - window.handle_winit_event(event); + if !window.accessibility_action(request) { + for event in window.accessibility_action_events(request) { + window.handle_winit_event(event); + } } } } diff --git a/packages/blitz-shell/src/window.rs b/packages/blitz-shell/src/window.rs index b3c76e82b6..228804c4a9 100644 --- a/packages/blitz-shell/src/window.rs +++ b/packages/blitz-shell/src/window.rs @@ -9,8 +9,8 @@ use anyrender::WindowRenderer; use blitz_dom::Document; use blitz_paint::paint_scene; use blitz_traits::events::{ - BlitzPointerEvent, BlitzPointerId, BlitzWheelDelta, BlitzWheelEvent, MouseEventButton, - MouseEventButtons, PointerCoords, PointerDetails, UiEvent, + BlitzImeEvent, BlitzPointerEvent, BlitzPointerId, BlitzWheelDelta, BlitzWheelEvent, + MouseEventButton, MouseEventButtons, PointerCoords, PointerDetails, UiEvent, }; use blitz_traits::shell::Viewport; use winit::dpi::{LogicalPosition, PhysicalInsets, PhysicalPosition}; @@ -371,14 +371,8 @@ impl View { if let Some(waker) = &self.waker { let cx = std::task::Context::from_waker(waker); if self.doc.poll(Some(cx)) { - #[cfg(feature = "accessibility")] - { - let inner = self.doc.inner(); - if inner.has_changes() { - self.accessibility.update_tree(&inner); - } - } - + // The accessibility tree is rebuilt by the redraw this asks + // for, once the changes are resolved. self.request_redraw(); return true; } @@ -432,6 +426,13 @@ impl View { drop(inner); + // The tree assistive tech reads, as the document now stands: a change + // reaches the document through a poll, an event handler or the + // embedder, and every one is resolved here. A no-op while no + // assistive tech is listening. + #[cfg(feature = "accessibility")] + self.build_accessibility_tree(); + if !is_blocked && is_visible && is_animating { self.request_redraw(); } @@ -601,6 +602,42 @@ impl View { } } + /// Carries out what assistive tech asks of a node that is not something + /// a pointer does: `Focus` focuses it, and `SetValue` on a text input + /// focuses it and replaces its text, which the page hears as typing (an + /// `input` event). Whether the request was one of these and was carried + /// out; a click is [`View::accessibility_action_events`]'s. + #[cfg(feature = "accessibility")] + pub fn accessibility_action(&mut self, request: &accesskit::ActionRequest) -> bool { + let node = blitz_traits::node_id::NodeId::from_u64(request.target_node.0); + let done = match (&request.action, &request.data) { + (accesskit::Action::Focus, _) => { + let mut doc = self.doc.inner_mut(); + doc.get_node(node).is_some_and(|n| n.is_element()) && { + doc.set_focus_to(node); + true + } + } + (accesskit::Action::SetValue, Some(accesskit::ActionData::Value(value))) => { + let selected = { + let mut doc = self.doc.inner_mut(); + doc.set_focus_to(node); + doc.get_focussed_node_id() == Some(node) && doc.select_all_in_text_input(node) + }; + if selected { + self.doc + .handle_ui_event(UiEvent::Ime(BlitzImeEvent::Commit(value.to_string()))); + } + selected + } + _ => false, + }; + if done { + self.request_redraw(); + } + done + } + #[cfg(target_arch = "wasm32")] const RESIZE_DEBOUNCE_MS: u32 = 100; diff --git a/tests/blitz-tests/tests/accessibility_text_input.rs b/tests/blitz-tests/tests/accessibility_text_input.rs new file mode 100644 index 0000000000..f6c4dbfc03 --- /dev/null +++ b/tests/blitz-tests/tests/accessibility_text_input.rs @@ -0,0 +1,92 @@ +use accesskit::{Action, Node as AccessKitNode, Role}; +use blitz_dom::{Document, DocumentConfig}; +use blitz_html::{HtmlDocument, HtmlProvider}; +use blitz_traits::events::{BlitzImeEvent, UiEvent}; +use blitz_traits::shell::{ColorScheme, Viewport}; +use std::sync::Arc; +use test_that::prelude::*; + +#[test] +fn a_text_input_has_its_text_as_value_and_can_be_set() -> TestResult<()> { + let mut document = HtmlDocument::from_html( + r#""#, + default_document_config(), + ); + document.resolve(0.0); + + let tree_update = document.build_accessibility_tree(); + let (_, input) = find(&tree_update.nodes, Role::TextInput); + verify_that!(input.value(), some(eq("Ada")))?; + verify_that!(input.placeholder(), some(eq("Your name")))?; + verify_that!(input.supports_action(Action::Focus), eq(true))?; + verify_that!(input.supports_action(Action::SetValue), eq(true)) +} + +#[test] +fn a_password_shows_only_how_long_it_is() -> TestResult<()> { + let mut document = HtmlDocument::from_html( + r#""#, + default_document_config(), + ); + document.resolve(0.0); + + let tree_update = document.build_accessibility_tree(); + let (_, input) = find(&tree_update.nodes, Role::PasswordInput); + verify_that!(input.value(), some(eq("••••••"))) +} + +#[test] +fn a_disabled_or_read_only_input_cannot_be_set() -> TestResult<()> { + let mut document = HtmlDocument::from_html( + r#""#, + default_document_config(), + ); + document.resolve(0.0); + + let tree_update = document.build_accessibility_tree(); + let (_, disabled) = find(&tree_update.nodes, Role::TextInput); + verify_that!(disabled.supports_action(Action::Focus), eq(false))?; + verify_that!(disabled.supports_action(Action::SetValue), eq(false))?; + let (_, read_only) = find(&tree_update.nodes, Role::MultilineTextInput); + verify_that!(read_only.is_read_only(), eq(true))?; + verify_that!(read_only.supports_action(Action::Focus), eq(true))?; + verify_that!(read_only.supports_action(Action::SetValue), eq(false)) +} + +/// What `View::accessibility_action` does for `SetValue`: focus the input, +/// select all of it, and commit the new text over the selection. +#[test] +fn text_committed_over_a_selection_of_all_replaces_the_value() -> TestResult<()> { + let mut document = HtmlDocument::from_html( + r#""#, + default_document_config(), + ); + document.resolve(0.0); + let (id, _) = find(&document.build_accessibility_tree().nodes, Role::TextInput); + let node = blitz_dom::NodeId::from_u64(id.0); + + document.set_focus_to(node); + verify_that!(document.select_all_in_text_input(node), eq(true))?; + document.handle_ui_event(UiEvent::Ime(BlitzImeEvent::Commit("Grace".into()))); + document.resolve(0.0); + + let tree_update = document.build_accessibility_tree(); + let (_, input) = find(&tree_update.nodes, Role::TextInput); + verify_that!(input.value(), some(eq("Grace"))) +} + +fn find(nodes: &[(accesskit::NodeId, AccessKitNode)], role: Role) -> (accesskit::NodeId, AccessKitNode) { + nodes + .iter() + .find(|(_, node)| node.role() == role) + .cloned() + .unwrap_or_else(|| panic!("no {role:?} in the tree")) +} + +fn default_document_config() -> DocumentConfig { + DocumentConfig { + viewport: Some(Viewport::new(800, 600, 1.0, ColorScheme::Light)), + html_parser_provider: Some(Arc::new(HtmlProvider) as _), + ..Default::default() + } +} diff --git a/tests/blitz-tests/tests/all.rs b/tests/blitz-tests/tests/all.rs index 75a36236a3..9b17585c15 100644 --- a/tests/blitz-tests/tests/all.rs +++ b/tests/blitz-tests/tests/all.rs @@ -5,6 +5,7 @@ mod accessibility_description; mod accessibility_hidden; mod accessibility_label; mod accessibility_roles; +mod accessibility_text_input; mod animations; mod anonymous_block_cache_invalidation; mod anonymous_block_leak; From 53125eb7b9aaaa3de58cb586bf9a8edcc131ed9d Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Fri, 23 Oct 2026 00:10:52 -0600 Subject: [PATCH 2/2] style(a11y): format the text input test --- tests/blitz-tests/tests/accessibility_text_input.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/blitz-tests/tests/accessibility_text_input.rs b/tests/blitz-tests/tests/accessibility_text_input.rs index f6c4dbfc03..7ea61ac1d1 100644 --- a/tests/blitz-tests/tests/accessibility_text_input.rs +++ b/tests/blitz-tests/tests/accessibility_text_input.rs @@ -75,7 +75,10 @@ fn text_committed_over_a_selection_of_all_replaces_the_value() -> TestResult<()> verify_that!(input.value(), some(eq("Grace"))) } -fn find(nodes: &[(accesskit::NodeId, AccessKitNode)], role: Role) -> (accesskit::NodeId, AccessKitNode) { +fn find( + nodes: &[(accesskit::NodeId, AccessKitNode)], + role: Role, +) -> (accesskit::NodeId, AccessKitNode) { nodes .iter() .find(|(_, node)| node.role() == role)