Documentation
¶
Overview ¶
Package engine is a pure-Go (CGO=0) web rendering engine: it fetches a URL, parses the HTML into a DOM, applies a real CSS subset (cascade, inheritance, var(), @media, dark-mode, gradients), runs the page's JavaScript against a real DOM binding, lays the content out with a full box model (block, inline, float, flex, grid, table, position), and paints anti-aliased text, backgrounds, gradients, box-shadows, images and SVG to an image.RGBA. A settle-then-render loop re-lays-out after scripts mutate the DOM, so script-driven changes are reflected in the output; set Engine.DisableJS to render the static, no-JavaScript document instead.
This file adds a hyperlink hit-map on top of the laid-out box tree: given a rendered page it reports the painted rectangle and resolved href of every <a href> anchor, so a consumer (e.g. the wasmdesk browserproxy) can turn a click at pixel (x, y) into a navigation target. It is kept deliberately in its own file — separate from engine.go and the js/ runtime work — so the hit-test can evolve without colliding with the rendering pipeline.
Index ¶
- func EncodePNG(img image.Image) ([]byte, error)
- func LinkAt(links []Link, pt image.Point) (string, bool)
- func Screenshot(ctx context.Context, rawurl string, viewport image.Rectangle) ([]byte, error)
- type Document
- type Engine
- func (e *Engine) Fetch(ctx context.Context, rawurl string) (*Document, error)
- func (e *Engine) Render(ctx context.Context, rawurl string, viewport image.Rectangle) (*image.RGBA, *RenderInfo, error)
- func (e *Engine) RenderDocument(ctx context.Context, doc *Document, viewport image.Rectangle) (*image.RGBA, *RenderInfo, error)
- func (e *Engine) RenderDocumentWithLinks(ctx context.Context, doc *Document, viewport image.Rectangle) (*image.RGBA, *RenderInfo, []Link, error)
- func (e *Engine) RenderHTML(ctx context.Context, htmlSrc, baseURL string, viewport image.Rectangle) (*image.RGBA, *RenderInfo, error)
- func (e *Engine) RenderWithLinks(ctx context.Context, rawurl string, viewport image.Rectangle) (*image.RGBA, *RenderInfo, []Link, error)
- func (e *Engine) Screenshot(ctx context.Context, rawurl string, viewport image.Rectangle) ([]byte, error)
- type Link
- type RenderInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Document ¶
type Document struct {
URL string // final URL after redirects
Title string // <title> text
Root *dom.Node // owned DOM tree
HTML string // decoded (UTF-8) source
}
Document is a fetched and parsed page.
type Engine ¶
type Engine struct {
Client *http.Client
UserAgent string
MaxImages int
// DisableJS turns off the JavaScript pass. Offline fixture tests that must
// stay byte-deterministic set this; the default (false) runs page scripts.
DisableJS bool
// JSTimeout bounds the total script + timer budget per render. Zero selects
// js.DefaultTimeout.
JSTimeout time.Duration
// JSLog, if non-nil, receives console.* and diagnostic lines from the script
// pass (used for debugging; nil discards them).
JSLog func(string)
}
Engine holds the HTTP client and render configuration.
func New ¶
func New() *Engine
New returns an Engine with a browser-like HTTP client (Chrome TLS fingerprint, cookie jar, redirect following).
func (*Engine) Render ¶
func (e *Engine) Render(ctx context.Context, rawurl string, viewport image.Rectangle) (*image.RGBA, *RenderInfo, error)
Render fetches url and renders it into an image at the given viewport width (the height is grown to fit the full page, at least the viewport height).
func (*Engine) RenderDocument ¶
func (e *Engine) RenderDocument(ctx context.Context, doc *Document, viewport image.Rectangle) (*image.RGBA, *RenderInfo, error)
RenderDocument renders an already-fetched Document. It is the offline entry point used by fixtures and tests (image sub-resources are still fetched from the network if their src is absolute/resolvable).
func (*Engine) RenderDocumentWithLinks ¶
func (e *Engine) RenderDocumentWithLinks(ctx context.Context, doc *Document, viewport image.Rectangle) (*image.RGBA, *RenderInfo, []Link, error)
RenderDocumentWithLinks renders an already-fetched Document and returns its image, render info and anchor hit-map from a single layout pass. It runs the same cascade → layout → paint pipeline as RenderDocument.
func (*Engine) RenderHTML ¶
func (e *Engine) RenderHTML(ctx context.Context, htmlSrc, baseURL string, viewport image.Rectangle) (*image.RGBA, *RenderInfo, error)
RenderHTML renders an HTML string (with baseURL used to resolve relative image sources) into an image at the given viewport. It is the offline entry point for local fixtures and demos, running the same cascade/layout/paint pipeline as Render but without fetching the page itself.
func (*Engine) RenderWithLinks ¶
func (e *Engine) RenderWithLinks(ctx context.Context, rawurl string, viewport image.Rectangle) (*image.RGBA, *RenderInfo, []Link, error)
RenderWithLinks fetches url, renders it to a full-page image and, in the same layout pass, returns the anchor hit-map. It mirrors (*Engine).Render but also threads the laid-out box tree into LinksFromBox so the image and the link rectangles are guaranteed to describe the exact same layout. It is kept here (rather than folded into engine.go's RenderDocument) so the hit-test feature stays isolated from the core pipeline and the js/ runtime work.
type Link ¶
Link is one hyperlink's painted rectangle (in full-page image pixels, the same coordinate space as the image returned by Render) and its href resolved against the page URL to an absolute URL.
func LinksFromBox ¶
LinksFromBox walks a laid-out box tree and returns, in document order, one Link per <a href> anchor that painted at least one inline atom (a word or an image). Each anchor's Rect is the union of the rectangles of all inline atoms it produced, so a multi-line link yields a single bounding box covering every line. baseURL is the page's own URL: each anchor's raw href is resolved against it, and links that resolve to a non-navigable scheme (javascript:, mailto:, an empty or pure-fragment href) are dropped.
The rectangles are in the box tree's own coordinate space, which is the full-page image space (layout origin is 0,0 at the page top-left), so a caller can hit-test a full-page click directly against them.
type RenderInfo ¶
RenderInfo carries metadata about a completed render.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
render
command
Command render fetches a URL and writes a PNG screenshot of the statically rendered page.
|
Command render fetches a URL and writes a PNG screenshot of the statically rendered page. |
|
Package css implements a deliberately small but real subset of CSS: a value model, a tokenizer/parser for stylesheets and declaration blocks, tag/class/ id selectors with specificity, and a cascade with inheritance over a dom tree.
|
Package css implements a deliberately small but real subset of CSS: a value model, a tokenizer/parser for stylesheets and declaration blocks, tag/class/ id selectors with specificity, and a cascade with inheritance over a dom tree. |
|
Package dom is a small, owned DOM node tree built from golang.org/x/net/html.
|
Package dom is a small, owned DOM node tree built from golang.org/x/net/html. |
|
Package js runs a page's JavaScript against a minimal but real DOM binding on a pure-Go goja ECMAScript runtime.
|
Package js runs a page's JavaScript against a minimal but real DOM binding on a pure-Go goja ECMAScript runtime. |
|
Package layout is a block-and-inline flow engine.
|
Package layout is a block-and-inline flow engine. |
|
Package paint rasterises a laid-out box tree onto an *image.RGBA using go-opentype for anti-aliased text, go-widgets/painter for backgrounds and go-images-decoded bitmaps for <img>.
|
Package paint rasterises a laid-out box tree onto an *image.RGBA using go-opentype for anti-aliased text, go-widgets/painter for backgrounds and go-images-decoded bitmaps for <img>. |
