Recursive AI
onehtmlGoAvailable

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.

go get 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
main.goGo
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
  • 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 autos 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
  • 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

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:

styleCSS
p {
  font: condensed 16px/1.5 "Open Sans", sans-serif;
  animation: fade 100ms 200ms;
  object-position: right bottom;
  width: calc(40px / 3);
}
Propertyv1.0.0v1.1.0
font-familyOpen Sans, sans-serif"Open Sans", sans-serif
font-stretchcondensed75%
line-height1.524px
animation-delayempty0.2s
object-positionright bottom100% 100%
width13.333333px13.3333px

The package