Recursive AI

Update an accessibility tree after an HTML edit in C#

onehtml is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Refresh accessible names after changing HTML. This computes a static accessibility tree in .NET; it does not run JavaScript.

Terminal
dotnet add package RecursiveAI.OneHtml.Accessibility

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Editing;
 
using RecursiveAI.OneHtml.Css;
using RecursiveAI.OneHtml.Accessibility;
 
var doc = Document.Parse("<button>Save</button>");
var styles = DocumentStyles.Create(doc);
var tree = AccessibilityTree.Build(doc, styles);
var edit = doc.Apply([
    Edit.ReplaceText(doc.Root.QuerySelector("button"), "Saved")
]);
if (!edit.Succeeded) throw new Exception(edit.Failure);
// Restyle because CSS decides what is hidden or generated.
var updated = tree.Update(edit, styles.Restyle(edit));
var button = edit.Document!.Root.QuerySelector("button");
Console.WriteLine(updated.Find(button)!.Name.Value);

Unzip the example, then dotnet run.

Text
Saved

The full program

The input is inline: <button>Save</button>. This program also reads the original tree and reports whether the update was incremental.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Editing;
 
using RecursiveAI.OneHtml.Css;
using RecursiveAI.OneHtml.Accessibility;
 
var doc = Document.Parse("<button>Save</button>");
var styles = DocumentStyles.Create(doc);
var tree = AccessibilityTree.Build(doc, styles);
var edit = doc.Apply([
    Edit.ReplaceText(doc.Root.QuerySelector("button"), "Saved")
]);
if (!edit.Succeeded) throw new Exception(edit.Failure);
// Restyle because CSS decides what is hidden or generated.
var updated = tree.Update(edit, styles.Restyle(edit));
var button = edit.Document!.Root.QuerySelector("button");
Console.WriteLine(updated.Find(button)!.Name.Value);
Console.WriteLine($"Old name: {tree.Find(doc.Root.QuerySelector("button"))!.Name.Value}");
Console.WriteLine($"Incremental: {updated.UpdateInfo!.Incremental}");

dotnet run prints:

Text
Saved
Old name: Save
Incremental: True

If the HTML edit fails, stop before restyling; a successful update may rebuild the tree, with the reason in UpdateInfo.FullRebuildReason.

Variations

Set an explicit accessible name

An aria-label changes the accessible name without changing the visible button text.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Editing;
 
using RecursiveAI.OneHtml.Css;
using RecursiveAI.OneHtml.Accessibility;
 
var doc = Document.Parse("<button>Save</button>");
var styles = DocumentStyles.Create(doc);
var tree = AccessibilityTree.Build(doc, styles);
var edit = doc.Apply([
    Edit.SetAttribute(doc.Root.QuerySelector("button"), "aria-label", "Store")
]);
if (!edit.Succeeded) throw new Exception(edit.Failure);
// Restyle because CSS decides what is hidden or generated.
var updated = tree.Update(edit, styles.Restyle(edit));
var button = edit.Document!.Root.QuerySelector("button");
Console.WriteLine(updated.Find(button)!.Name.Value);
Text
Store

The short answer

Build an AccessibilityTree and DocumentStyles from the same document. Apply the HTML edit, pass its result into styles.Restyle, then pass both results to tree.Update. Query the new tree using a node from the edited document. The example changes a button’s text from “Save” to “Saved” and prints the updated accessible name.

This is an explicit update of a static document, not a subscription to browser events. Nothing changes the old tree in place. The full program prints its old name too, so you can see that both snapshots remain available.

How do you update an accessibility tree after a DOM edit?

Use the edit result to connect the old and new snapshots. It records the changes the style and accessibility layers need. The update documentation says the returned tree represents the edited document and matches a fresh build with the same styles and context.

The order matters. Restyle first, then update accessibility with that restyle result. CSS can affect whether an element is hidden and whether generated text contributes to its name. Updating only the document while continuing to read the old tree would leave the old answer in your application.

Names can depend on other elements. A change to text referenced by aria-labelledby, for example, can change a button’s name without editing the button itself. The update path follows these dependencies. Its documented invalidation also includes labels, descriptions, ownership references, parent changes, and removed referenced nodes. You do not have to locate each dependent control yourself.

Check the edit result before restyling. An unsuccessful transaction has no new document to update. A successful update may reuse unaffected names, but it does not guarantee an incremental path for every input. UpdateInfo.Incremental tells you what happened, and FullRebuildReason explains a full rebuild.

What is the difference between the accessibility tree and the DOM?

The DOM records HTML nodes. An accessibility tree describes their roles, names, states, and relationships for accessibility. The Chrome explanation illustrates how a button gets a role and name, and why some DOM containers have no useful accessibility semantics.

In this example, changing the button’s text changes its computed name because the name comes from content. The variation instead adds aria-label="Store"; the name is then “Store” even though the visible text is still “Save.” Reading TextContent alone would miss that distinction.

onehtml lets you find an accessibility node from a DOM node, including inspecting ignored or hidden information. Use node handles from the correct snapshot: the original button belongs to the old tree, while the button selected from edit.Document belongs to the updated one. These are not interchangeable browser node IDs.

Is this the same as Chrome’s live accessibility tree?

No. Chrome’s full tree view receives updates from its renderer as the page changes. This example has no browser renderer and executes no scripts. It computes the static HTML and CSS facts supplied to onehtml. It is useful for checking the accessibility consequences of your own source edits before you publish them, not for observing a live application’s JavaScript behavior.

The update rules also distinguish caller-supplied rendered facts from static computation. Facts belong to a snapshot. Updating a tree that had facts without supplying replacement facts drops them and rebuilds; the overload accepting new facts rebuilds with those facts. Do not carry an old layout capture across an HTML change and assume it still describes the page.

Other documented reasons for a rebuild include a full style recomputation, a prior work limit, CSS counters participating in names, and a bounded dependency walk. A full rebuild is a supported result, not a failed edit. Read the resulting tree the same way in either case.

All onehtml recipes