Recursive AI
LibraryC#Available

onehtml

HTML and CSS for .NET, in pure C#. Parse HTML the way a browser does, edit it without disturbing a byte the edit didn’t touch, and check pages for accessibility without a browser.

dotnet add package RecursiveAI.OneHtml

A quick look

Parse, query and edit. An edit changes only the bytes it touches, the result is re-parsed and verified, and the old snapshot stays valid.

How editing works
Program.csC#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Editing;
Document doc = Document.Parse("<p>Hello <a href=/x>world</a>");
Node link = doc.Root.QuerySelector("a[href]");
EditResult edit = doc.Apply(new[] { Edit.SetAttribute(link, "title", "A link") });
byte[] output = edit.Document!.ToLosslessBytes();

New in 1.6

A page’s landmarks and headings without building the accessibility tree, and styles that cost less to create. From the 1.6.0 changelog:

  • PageOutline.Create(document, styles) in RecursiveAI.OneHtml.Accessibility returns the landmarks and headings of a page without building the accessibility tree: the same elements, in the same order, with the same roles, levels, depths, languages and names as NavigationLists over AccessibilityTree.Build. … On an 8 KB page with 236 elements, after parsing and creating the styles, finding the landmarks and headings took about 120–160 µs and 160 KB (the first read of the styles included), against about 360–400 µs and 520–540 KB for building the tree and its navigation lists (best of seven rounds on a loaded machine, indicative).
  • Computing styles for the first time is about twice as fast. … On an 8 KB page with 236 elements, computing the style of every element once went from about 355 to about 140 µs, … (best of seven rounds on a loaded machine, indicative). Computed styles are unchanged: every element of 17,291 crawled pages and 2,500 test pages has the same value for every property as before.
  • DocumentStyles.Create no longer rebuilds the user-agent rules for every document. They are flattened, indexed and summarised once per compatibility mode, are immutable, and every document and thread shares them; a document adds only its own rules on top. On an 8 KB page with 236 elements, Create went from about 480–520 µs and 1.4 MB to about 5–50 µs and 14–26 KB (the rest is the page’s own style sheet), … (best of seven rounds on a loaded machine, indicative).
  • font-size: 0 (a unitless zero) is accepted, as CSS Values 4 §6.1 says a zero <length> needs no unit. Before, the declaration was dropped and the element kept its inherited size …

The section with no name and the hidden heading are left out, as they are from the tree’s navigation lists.

branch.htmlHTML
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Harbor Street Library</title>
<header><a href="/">Harbor Street Library</a>
  <nav aria-label="Main"><a href="/books/">Books</a> <a href="/events/">Events</a></nav>
</header>
<main>
  <h1>Opening hours</h1>
  <section aria-labelledby="hol"><h2 id="hol">Holiday hours</h2></section>
  <section><h2>Parking</h2></section>
  <aside><h3 hidden>Old hours</h3><h3>Café</h3></aside>
  <form aria-label="Search the catalog"><input type="search"></form>
</main>
<footer lang="es"><h2>Visítenos</h2></footer>
Program.csOneHtml.Accessibility 1.6.0
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Accessibility;
using RecursiveAI.OneHtml.Css;
 
Document doc = Document.Parse(File.ReadAllBytes("branch.html"));
PageOutline outline = PageOutline.Create(doc, DocumentStyles.Create(doc));
 
foreach (OutlineLandmark l in outline.Landmarks)
    Console.WriteLine($"{new string(' ', l.Depth * 2)}{l.Role} \"{l.Name}\" <{l.Node.LocalName}>");
foreach (OutlineHeading h in outline.Headings)
    Console.WriteLine($"h{h.Level} {h.Name} ({h.Lang})");
OutputConsole
banner "" <header>
  navigation "Main" <nav>
main "" <main>
  region "Holiday hours" <section>
  complementary "" <aside>
  form "Search the catalog" <form>
contentinfo "" <footer>
h1 Opening hours (en)
h2 Holiday hours (en)
h2 Parking (en)
h3 Café (en)
h2 Visítenos (es)
What it does

What it does

Parses like a browser

The WHATWG parser: encoding sniffing, the tokenizer and every insertion mode. It passes the html5lib tokenizer and tree-construction tests.

Edits that leave the rest alone

Lossless editing of HTML, inline styles and stylesheets. Untouched bytes stay identical, and every result is re-parsed and verified.

The accessibility tree Chromium exposes

WAI-ARIA 1.2, HTML-AAM and AccName 1.2, with Chromium’s choices written down. A reading view and navigation lists show what a screen-reader user meets.

Accessibility checks without a browser

One rule catalogue covering the WCAG success criteria, suggested edits where exactly one fix exists, and axe-compatible and SARIF 2.1.0 output.

Performance

2.97× AngleSharp’s parsing speed

Parsing a 100-page sample (36 MB) of a crawled corpus from bytes, end to end, takes 116.1 ms per sweep, against 345.4 ms for AngleSharp 1.8.6 and 286.7 ms for HtmlAgilityPack 1.13.1. Measured with BenchmarkDotNet on .NET 10, on the code released as 1.0.0.

The method and harness

The packages

Go further