Recursive AI

Find CSS declarations invalid at computed-value time in Go

onehtml-go is a free, open-source HTML parser for Go from Recursive AI, with CSS selectors, computed styles, lossless editing and accessibility checks. MIT or Apache-2.0, no dependencies.

A CSS declaration can win the cascade and still fail when var() is resolved. Find those failures with Styles.InvalidDeclarations(). Requires Go 1.24 or later.

Terminal
go get recursiveai.org/go/onehtml
GO
package main
 
import (
	"fmt"
	"log"
	"strings"
 
	"recursiveai.org/go/onehtml"
)
 
func main() {
	const html = `<style>
p {
  --pad: 4px;
  padding: var(--pad) var(--missing);
  width: var(--pad) var(--pad);
}
</style>
<p id="card">Card</p>`
	doc, err := onehtml.Parse(strings.NewReader(html))
	if err != nil {
		log.Fatal(err)
	}
	for node, decl := range doc.Style().InvalidDeclarations() {
		id, _ := node.Get("id") // Optional label.
		fmt.Printf("%s %s: %s (%s)\n",
			id, decl.Property, decl.Value,
			decl.Source.Selector)
	}
}

Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onehtml, then go run .; or unzip the example and go run ..

Text
card padding: var(--pad) var(--missing) (p)
card width: var(--pad) var(--pad) (p)

The report includes the element ID, property, original value, and selector. An empty report does not mean the source has no CSS errors: declarations rejected during parsing are not listed.

Variations

To check only the card, replace the document-wide loop with this loop. doc is the document from the example.

GO
node := doc.ElementByID("card")
style := doc.Style().ComputedStyle(node)
for _, decl := range style.InvalidDeclarations() {
    fmt.Println(decl.Property)
}

This prints padding and width, one per line. If no element has the requested ID, ElementByID returns the zero onehtml.Node, whose Valid() method returns false.

The short answer

Range over doc.Style().InvalidDeclarations() to get the affected node and the failed declaration. Read Property, Value, and Source.Selector to find the rule to fix. This is a computed-style diagnostic, not a CSS syntax checker: it tells you what failed after the cascade picked a winner for an element.

What does “invalid at computed-value time” mean in CSS?

A declaration using var() can look valid until the engine substitutes the variable. In the example, padding fails because --missing has no value or fallback. width fails for a different reason: both variables resolve, leaving 4px 4px, which is not a valid single width. A variable can be valid in its own right and still make the property that consumes it invalid.

The CSS Custom Properties specification explains how these failures differ from parse-time ones. A declaration discarded while parsing never has a chance to win. A declaration that fails at computed-value time has already won; CSS doesn’t go back and try a lower-priority declaration for that property. It behaves as if unset: an inherited property takes its inherited value, and a non-inherited one takes its initial value.

For a shorthand such as padding, one failing substitution can invalidate the whole shorthand. The Go report uses the shorthand’s own name rather than repeating it for each side of the box.

How do you fix a CSS variable error with a fallback?

First distinguish a missing variable from a value of the wrong type or shape. A fallback inside var() can fix a missing variable: replacing var(--missing) with var(--missing, 8px) lets this padding declaration resolve to 4px 8px. The fallback must be valid where it lands. It would not fix width: two valid lengths are still invalid there, so that declaration needs just one var(--pad).

CSS
padding: var(--pad) var(--missing, 8px);
width: var(--pad);

With both changes, the example’s loop prints no rows. A fallback supplied as a separate, earlier width declaration is not the same fix. By the time the later var() declaration fails, the cascade has already selected it.

Why does a CSS variable work on one element but not another?

The same rule can see different custom property values on different elements. A selector tells you where the rule came from; the node tells you which instance actually failed. Keep both in reports for repeated components. For a page-wide check, consume the whole Styles.InvalidDeclarations() iterator. It computes styles as it walks the document. If you stop after the first failure, later elements are not checked by that walk. For an inspector where you’ve already selected an element, use the single-style method shown in the variation.

All onehtml Go recipes