Recursive AI

onehtml

All onehtml Go docs

A pure-Go HTML and CSS library that parses HTML exactly as a browser does, matches Selectors Level 4, computes the CSS cascade, edits a page without disturbing a byte the edit didn't touch, and builds the accessibility tree a browser gives screen readers. It also checks pages against every WCAG level, from the standard library alone.

Why onehtml

  • Browser-exact parsing. The WHATWG tokenizer and tree builder in full, fragments and parse errors included, with the Encoding Standard's detection and decoders. Every node and attribute knows its byte range in the original input.
  • Selectors and cascade. Selectors Level 4 compiled once and matched without recursion; origins, layers, @scope, nesting, @media, var() and calc(), with computed values serialized as getComputedStyle gives them. No layout, no painting.
  • Lossless editing. Rewrite splices edits into the original bytes and verifies the result by parsing it again; with no edits the output is byte-identical to the input.
  • Accessibility. The accessibility tree (WAI-ARIA 1.2, HTML-AAM, Accessible Name and Description 1.2), a reading view and navigation lists, and a checker for WCAG 2.0, 2.1 and 2.2 with suggested fixes and JSON, axe-compatible JSON, SARIF and EARL exports.
  • Safe on hostile input. No panic and no hang on any bytes; limits are checked before the work they guard, and nothing recurses on input depth.
  • Never fetches, never executes. The library opens no files and makes no network requests; linked stylesheets come from the caller and scripts are inert text. Standard library only, no cgo, no unsafe.

Install

go get recursiveai.org/go/onehtml

Go 1.24 or newer.

Quick start

GO
doc, err := onehtml.Parse(strings.NewReader(`<style>a { color: green }</style>
<nav><a href="https://example.com/">Home</a></nav>`))
if err != nil {
	log.Fatal(err)
}
links, err := doc.Select("nav a[href^='https://']")
if err != nil {
	log.Fatal(err)
}
styles := doc.Style()
for _, a := range links {
	href, _ := a.Get("href")
	fmt.Println(a.TextContent(), href, styles.ComputedStyle(a).Get("color"))
}
 
res := onehtml.Rewrite(doc, []onehtml.Edit{onehtml.SetAttribute(links[0], "rel", "noopener")})
if !res.Applied() {
	log.Fatal(res.Reasons)
}
fmt.Println(string(res.Document)) // the original bytes, changed only at the edit

The same example runs as Example_quickStart in the package documentation.

Where to go

Licence

Licensed under either of MIT or Apache-2.0 at your option (LICENSE-MIT, LICENSE-APACHE). Third-party data is listed in THIRD-PARTY-NOTICES.md. Copyright Recursive AI, LLC.