Process existing HTML
Use web::html::parse when your Go application already has the HTML. The content might come from os.ReadFile, a database, an HTTP response fetched by the host, or another service. Ferret can parse, query, and modify that content without fetching it again.
This workflow does not require CDP. Configure the HTML module with the in-process memory driver and pass the HTML through a session parameter.
Parse supplied HTML
Keep the FQL source stable and reference the supplied content as @html:
web::html::parse accepts an FQL string or binary value. In Go, ferret.WithSessionParam converts a string to an FQL string and []byte to an FQL binary value. Do not interpolate the HTML into the FQL source.
Run the workflow from Go
Create a module and install Ferret, the HTML module, and the small HTML parser used at the end of the example:
mkdir ferret-existing-html && cd ferret-existing-htmlgo mod init example.com/ferret-existing-htmlgo get github.com/MontFerret/ferret/v2 \ github.com/MontFerret/contrib/modules/web/html \ golang.org/x/net/html
The following program runs two plans against the same document. The first passes a Go string and decodes structured output. The second passes []byte, modifies the document, and decodes the returned HTML string.
Resolve the complete module graph and run the program:
go mod tidygo run .
The engine owns runtime-wide configuration, each plan owns compiled code and its VM pool, and each session owns one execution. The example closes all three levels. A real application can reuse the engine and plans while creating a new session for each input document.
The execute helper accepts the concrete *ferret.Plan returned by both Engine.Compile and Engine.Load.
Decode the output format
Session.Run returns an Output with two fields:
Output.Contentcontains encoded bytes, not a raw Ferret runtime value.Output.ContentTypeidentifies the codec that produced those bytes.
JSON is the default output format. Objects and arrays therefore decode into Go structs, slices, maps, or other JSON targets with json.Unmarshal.
A returned FQL string is also JSON-encoded. For example, returned HTML begins and ends with JSON quotes and may contain escaped characters in Output.Content. Decode it into a Go string before writing it to a file, returning it as HTML, or passing it to another library.
The example checks Output.ContentType before using json.Unmarshal. Keep that check when the application allows configurable output codecs; do not assume every result is JSON.
Work with the returned markup
page.innerHTML serializes the current in-memory document, including mutations such as the data-processed attribute in the example. HTML parsing and serialization can normalize the markup, including tag structure, whitespace, quoting, and character escaping. Treat the result as equivalent parsed HTML rather than a byte-for-byte copy of the input.
Once decoded into a Go string, the markup is ordinary host data. The example passes it to golang.org/x/net/html, but the same boundary works with another parser, sanitizer, renderer, or storage layer.
When a browser-backed document is needed
The memory driver is the default for supplied HTML because parsing, querying, mutation, and serialization all run in process. Configure the CDP driver only when the supplied document must support browser-backed operations. CDP requires a running browser endpoint and is not needed for the workflow in this guide.