All contributions

PuerkitoBio/goquery

goquery

A Go library for traversing and changing HTML documents in the style of jQuery, built on the golang.org/x/net/html parser.

Language
Go
GitHub stars
14,995
Merged changes
2

What we changed

RemoveClass and ToggleClass remove a class even when the source repeats it side by side. A new NewDocumentFromReaderWithOptions constructor passes HTML parser options through, such as turning scripting off so selectors can reach inside <noscript>.

What this work shows

  • The golang.org/x/net/html parser
  • The HTML spec: <noscript> and scripting
  • Backward-compatible API design in Go
  • Table-driven tests in Go
What was broken

RemoveClass left a repeated class behind

For class="a a", after RemoveClass("a") the element still had class a. A single strings.ReplaceAll pass missed the second occurrence, because adjacent classes share the space between them. ToggleClass had the same bug, so the next toggle switched the class off instead of on.

How we fixed it

Removing until none are left

A removeClassFromClasses helper removes one occurrence at a time and rescans from the start until none are left. RemoveClass and ToggleClass both use it, and tests cover adjacent, tripled, leading, trailing and interleaved repeats.

#603

What was broken

Selectors could not reach inside <noscript>

The HTML parser assumes scripting is on, and the spec then parses <noscript> content as raw text where no selector can find anything. The html package has a ParseOptionEnableScripting option, but goquery called html.Parse and there was no way to pass it.

How we fixed it

A constructor with parser options

We added NewDocumentFromReaderWithOptions, which accepts parser options. The document keeps them and applies them to HTML added later too, for example through AppendHtml, AfterHtml or WrapHtml, while the existing API stays unchanged.

#602

Merged changes 2

  1. #602 Expose html parse options (addresses #139, noscript) Merged 30 September 2026
  2. #603 RemoveClass leaves a class behind when the source repeats it Merged 7 September 2026

What it taught us

A library for HTML from the web must cope with messy source where the same class repeats several times. We add new API narrowly and without breaking compatibility, and state its reach plainly.

More about this technology: Golang

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.