---
title: "onehtml for Go"
url: https://recursiveai.org/onehtml/go/
description: "An HTML parser for Go that parses as a browser does, with CSS selectors, computed styles, lossless editing, the accessibility tree and WCAG checks. No cgo, no unsafe, nothing outside the standard library. MIT or Apache-2.0."
---

# onehtml for Go

Parse HTML exactly as a browser does, query it with CSS selectors, compute styles, edit it without disturbing a byte the edit didn’t touch, and check it for accessibility, in pure Go.

```sh
go get recursiveai.org/go/onehtml
```

- [Getting started](https://recursiveai.org/onehtml/go/docs/getting-started/)
- [Docs](https://recursiveai.org/onehtml/go/docs/)
- [Cookbook](https://recursiveai.org/onehtml/go/cookbook/)
- [GitHub](https://github.com/recursive-ai-oss/onehtml-go)
- [pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onehtml)

## A quick look

Select elements, read their computed styles, and write an edit back into the original bytes. The result is parsed again to check it.

[Getting started](https://recursiveai.org/onehtml/go/docs/getting-started/)

*main.go · Go*

```
doc, err := onehtml.Parse(r)
links, err := doc.Select("nav a[href^='https://']")
styles := doc.Style()
for _, a := range links {
    fmt.Println(a.TextContent(), styles.ComputedStyle(a).Get("color"))
}
 
res := onehtml.Rewrite(doc, []onehtml.Edit{
    onehtml.SetAttribute(links[0], "rel", "noopener")})
os.Stdout.Write(res.Document)
```

## New in 1.1.1

The declared `display` before blockification, and fixes that make `steps()`, `object-position` and the `background-*` lists read back as browsers report them. From the changelog:

[Changelog](https://recursiveai.org/onehtml/go/docs/changelog/)

> - `ComputedStyle.SpecifiedDisplay`: the element’s `display` as declared, in the short form `Get` uses, before blockification. A float, an absolutely positioned element, the root element and a flex or grid item compute to a block-level `display` (CSS Display 3 section 2.7), so `Get("display")` on a float declared `inline-flex` reads `flex`; `SpecifiedDisplay` still reads `inline-flex`, which tells a caller the inner display type.
> - `steps()` keeps `jump-start`, `jump-none` and `jump-both` as written when serialized (`steps(2, jump-start)`, no longer `steps(2, start)`); `jump-end` and `end` still read as `steps(2)`.
> - `object-position` takes a unitless `0` as the offset in its three- and four-value forms (`top 0 right 1px` is `calc(100% - 1px) 0px`, no longer refused and reset to `50% 50%`).
> - `background-size` computes as browsers report it: a single value is the value and `auto` (`10px` is `10px auto`), two `auto`s are `auto`, and lengths are in px (`1em` is `16px auto`). A negative or malformed size is invalid.
> - The `background-*` longhand lists (`background-position-x` and `-y`, `-size`, `-repeat`, `-clip`, `-origin`, `-attachment`, `-blend-mode`) read back as long as the `background-image` list: a longer list is cut, and a shorter one repeats from its start (`background-position: left, right` with one image reads `0%` for `background-position-x`).

## New in 1.1

Registered custom properties, real font metrics for `ex`, `ch`, `cap` and `ic`, and a report of declarations that are invalid at computed-value time. From the changelog:

[Changelog](https://recursiveai.org/onehtml/go/docs/changelog/)

> - `Styles.Registrations` and `PropertyRegistration`: the document’s `@property` rules (CSS Properties and Values API 1). A registered custom property now takes its initial value when nothing sets it, does not inherit when `inherits` is false, and has a value that does not match its syntax treated as unset; a matching value is computed to its canonical form (`calc(1px + 2px)` is `3px`). `ResolveAnimations` reports a registered custom property that a keyframe sets, so a caller can interpolate it by its syntax.
> - `font` takes the system font keywords (`caption`, `icon`, `menu`, `message-box`, `small-caption`, `status-bar`), as Chrome reports them on Linux: a 16px Arial in the normal style and weight.
> - The `table` element’s `border` attribute sets the four border widths (1px when the value does not parse).
> - `WithFontMetrics`, `FontQuery`, `FontMetrics` and `FontMetricsFunc`: the caller measures the primary font, and the `ex`, `ch`, `cap` and `ic` units (and the root-relative `rex`, `rch`, `rcap` and `ric`) take its x-height, zero advance, cap height and ideograph advance. Without it, or for a measurement the function leaves at zero, the estimates stay (half an em, 0.7 em, one em). `font-size` in `ex` is relative to the parent’s font.
> - `ComputedStyle.InvalidDeclarations` and `Styles.InvalidDeclarations`, with `InvalidDeclaration`: the declarations that won the cascade but are invalid at computed-value time (a `var()` that does not fit the property, a shorthand that does not expand after substitution, a custom property that refers to nothing or does not fit its registered syntax), with their source.

## Upgrading from 1.0

Nothing is removed or deprecated. Some computed values now read back as browsers report them, so code that compares serialized values sees new strings.

[The full list under Fixed](https://recursiveai.org/onehtml/go/docs/changelog/)

> Several computed values now read back in the forms browsers report, so code that compared serialized values from 1.0.0 sees new strings: multi-word `font-family` names are quoted, `font-stretch` is a percentage, a numeric `line-height` reads as px through `Get` (descendants still inherit the number), animation times are in seconds, `object-position` and `background-position-x`/`-y` keywords become percentages, and px lengths are written with six significant digits. Details under Fixed.

This rule, read back with `ComputedStyle(p).Get` on each version:

*style · CSS*

```
p {
  font: condensed 16px/1.5 "Open Sans", sans-serif;
  animation: fade 100ms 200ms;
  object-position: right bottom;
  width: calc(40px / 3);
}
```

| Property | v1.0.0 | v1.1.0 |
|---|---|---|
| `font-family` | `Open Sans, sans-serif` | `"Open Sans", sans-serif` |
| `font-stretch` | `condensed` | `75%` |
| `line-height` | `1.5` | `24px` |
| `animation-delay` | empty | `0.2s` |
| `object-position` | `right bottom` | `100% 100%` |
| `width` | `13.333333px` | `13.3333px` |

## The package

- **Version**: v1.1.1. Go 1.24 or newer.
- **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.
- **CSS**: Selectors Level 4; the cascade with origins, layers, `@scope`, nesting, `@media`, `@property`, `var()` and `calc()`, with computed values as `getComputedStyle` gives them. No layout, no painting.
- **Accessibility**: The accessibility tree, a reading view and navigation lists, and a checker for WCAG 2.0, 2.1 and 2.2 with JSON, axe-compatible JSON, SARIF and EARL exports.
- **Safety**: No panic and no hang on any bytes; limits are checked before the work they guard. Opens no files, makes no network requests, never runs script. No cgo, no `unsafe`, no dependencies outside the standard library.
- **License**: MIT or Apache-2.0.
