Recursive AI

Edit HTML attributes without reformatting 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.

Use SetAttribute with Rewrite to patch the source instead of serializing the whole tree.

Terminal
go get recursiveai.org/go/onehtml

Requires Go 1.24 or later.

GO
package main
 
import (
	"fmt"
	"recursiveai.org/go/onehtml"
)
 
func main() {
	html := `<IMG  id='logo' alt='Old'>` +
		"\n<P>caf&eacute;</P>"
	doc, err := onehtml.ParseBytes([]byte(html))
	if err != nil {
		panic(err)
	}
	logo := doc.ElementByID("logo")
	if !logo.Valid() {
		panic("#logo not found")
	}
	edits := []onehtml.Edit{
		onehtml.SetAttribute(logo, "alt", "Shop"),
	}
	result := onehtml.Rewrite(doc, edits)
	if !result.Applied() {
		panic(result.Reasons)
	}
	fmt.Println(string(result.Document))
}

If an edit can’t be applied, Applied() is false and Reasons explains what happened. The example prints the changed HTML; it does not overwrite its input.

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 ..

go run . prints:

Text
<IMG  id='logo' alt='Shop'>
<P>caf&eacute;</P>

Variations

To remove an attribute instead, build a new edit against the same original doc. Add this at the end of main to print a second result. It removes id and the extra space before it in the <IMG> tag; the <P> line is unchanged.

GO
edits = []onehtml.Edit{
    onehtml.RemoveAttribute(logo, "id"),
}
result = onehtml.Rewrite(doc, edits)
if !result.Applied() {
    panic(result.Reasons)
}
fmt.Println(string(result.Document))

The added code prints:

Text
<IMG alt='Old'>
<P>caf&eacute;</P>

The short answer

Parse the original bytes, find the element by ID or selector, and submit SetAttribute to onehtml.Rewrite. Check Applied() and use result.Document as the new file contents.

How do I modify an element in an HTML file with Go?

Read the file as bytes and select the element that owns the attribute. For a known image ID, ElementByID("logo") is enough. Decide which element you mean before making the edit: a page-wide text substitution can also match text in a comment, a script, or another tag. SetAttribute takes the attribute name and its text value; pass Shop, not the HTML fragment alt='Shop'.

Check the result before saving it. The example prints the new document rather than overwriting the input. In a file-editing tool, write the returned bytes to a separate destination first so you can review the diff before replacing the original. The library does not write a file for you. You can put several edits in the slice: if one cannot be applied, the rewrite does not return a partially changed document.

How do I edit HTML without changing its formatting?

Keep the original bytes available at parse time and save the bytes from Rewrite, not the output of an HTML serializer. The parsed tree tells you what the markup means; it does not require the serializer to preserve choices such as uppercase tag names, line breaks, or entity spelling. In this example, the new value is Shop, but the space after IMG, the single quotes, and the spelling caf&eacute; on the next line all survive. Nothing has to regenerate that second line.

For a strict diff, distinguish the region an edit affects from the rest of the file. Removing the id in the variation changes spacing in the edited start tag. The rewrite checks its output by parsing it again, and it can widen a region when a narrow edit does not verify. result.Ranges tells you which regions it rewrote. That gives you something concrete to inspect when you expected a one-attribute diff and the changed area is larger.

All onehtml Go recipes