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.
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 worksusing 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)inRecursiveAI.OneHtml.Accessibilityreturns 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 asNavigationListsoverAccessibilityTree.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.Createno 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,Createwent 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.
<!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>
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})");
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
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.
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 harnessThe packages
- RecursiveAI.OneHtml
The HTML parser, the DOM as immutable snapshots with a source map, Selectors Level 4, a CSS parser, the cascade for every known property, and lossless editing of HTML and CSS. Your own grammar can vet declarations as they enter the cascade.
- .Accessibility
RecursiveAI.OneHtml.builds the accessibility tree, recomputes it after edits, and adds a reading view, navigation lists and tree diffs. Layout facts come from a Chrome capture or straight from your own renderer.Accessibility - Rendered facts
onerender lays a page out and gives the checker its rendered facts, with no browser: check accessibility with rendered facts.
- .Validation
RecursiveAI.OneHtml.checks pages against WCAG 2.0, 2.1 and 2.2 AA profiles and every W3C-approved ACT rule, with native JSON, axe-compatible JSON and SARIF output.Accessibility. Validation - Conformance
All 37 approved ACT rules implemented with 0 failing cases. A round trip with no edits is byte-identical on 18,330 of 18,330 corpus pages and stylesheets.
- Safe by design
Every input is treated as hostile. The libraries never read files or the network, and scripts are inert text.
- Version
1.6.0. All three packages share one version.
- New in 1.3 to 1.5
Styles that move in place as hover, focus and form state change, with no allocation once warmed up: restyle on hover, focus and form state. Styles for
::selection,::placeholderand range slider parts.@keyframesas Chrome reports them. - .NET
.NET 10. NativeAOT and trimming are supported, with no
unsafecode. MIT or Apache-2.0.