Recursive AI

Get rendered HTML tab order in C#

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

Call GetTabOrder() to list the stops for Tab without moving focus. The Image package brings in InteractivePage and supplies the fonts used for layout.

Terminal
dotnet add package RecursiveAI.OneRender.Image

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

C#
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
 
const string html = """
    <form>
      <input id=email aria-label=Email>
      <button id=save>Save</button>
    </form>
    """;
var fonts = FontProviders.CreateDefault();
var page = InteractivePage.Create(
    html, new HtmlPageOptions(fonts));
foreach (var stop in page.GetTabOrder())
    Console.WriteLine(
        stop.GetAttribute("id"));

Prints the IDs in forward Tab order:

Text
email
save

Unzip the example, then dotnet run.

The full program

The download adds an early Cancel button, a disabled button, a programmatic-only focus target, and a hidden button. Each line pairs the predicted tab stop with the element that actually receives focus.

C#
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
 
const string html = """
    <form>
      <input id=email aria-label=Email>
      <button id=save>Save</button>
      <button id=cancel tabindex=1>Cancel</button>
      <button id=locked disabled>Locked</button>
      <button id=details tabindex=-1>Details</button>
      <button id=hidden style="display:none">Hidden</button>
    </form>
    """;
var fonts = FontProviders.CreateDefault();
var page = InteractivePage.Create(
    html, new HtmlPageOptions(fonts));
var stops = page.GetTabOrder();
 
foreach (var expected in stops)
{
    page.MoveFocus(forward: true);
    var focused = page.FocusedElement;
    if (!focused.Equals(expected))
        throw new InvalidOperationException("Unexpected focus order.");
 
    Console.WriteLine(
        $"{expected.GetAttribute("id")} -> {focused.GetAttribute("id")}");
}

dotnet run prints:

Text
cancel -> cancel
email -> email
save -> save

cancel comes first because of its positive tabindex. The disabled, hidden, and negative-tabindex buttons do not enter the sequential order. With no eligible controls, GetTabOrder() returns an empty array.

Variations

Move backward with Shift+Tab. Append this to the full program. The starting point is Save, so the previous stop is Email:

C#
page.MoveFocus(forward: false);
Console.WriteLine(page.FocusedElement.GetAttribute("id"));
Text
email

Hit testing and events · Edit text in a rendered HTML form


The short answer

Create an InteractivePage, then call GetTabOrder() to copy its current forward Tab stops as DOM nodes. Use MoveFocus(forward: true) and read FocusedElement when you want to test what happens on each step. The comparison above starts with a fresh page: on an already focused page, Tab starts from the current focus, not from the head of the array.

What is tab order in web accessibility?

Tab order is the sequence in which links and controls receive keyboard focus. It does not list every element a screen reader can read. For a form, look for a sequence that follows the task: enter the email address, then move to Save. An early Cancel button in the example makes the difference between tree order and Tab order visible; it isn’t a recommended way to arrange the form. WCAG’s Focus Order guidance explains when the order matters for meaning and operation.

GetTabOrder() inspects the OneRender page as it is now, not a browser session. Use it to check the controls for a particular rendered state. If the host changes the form, inspect it again; the returned array is a snapshot. For a browser deployment, also try the keyboard flow in the browser, where scripting can change what is focusable or move focus itself.

How do you set tab order in HTML?

Start with the order of the controls in the HTML. In OneRender, eligible elements with positive tabindex come first, smallest value first, with ties in tree order. Then the engine visits naturally focusable elements and elements with tabindex="0" in tree order. For the sample form, removing the positive value puts Cancel back after Save. When layout makes a form look out of order, compare the rendered positions with the Tab sequence before changing any tabindex values.

What does tabindex="-1" do?

It skips an element during Tab navigation without necessarily making it unfocusable. The Details button above can still be a target of Focus, even though it doesn’t show up in GetTabOrder(). By contrast, a disabled form control is skipped even if given a nonnegative tabindex.

A named radio group is another case where counting HTML inputs overstates the number of stops. OneRender keeps the checked member as the stop; without a checked member it uses the first eligible radio in the direction of travel. The reported forward list is therefore not always the reverse of a Shift+Tab walk.

All onerender recipes