Skip to content

proposal: html/template: export filters and escapers #70375

@apleshkov

Description

@apleshkov

Proposal Details

I try to create a tool to translate text/template and html/template to go source code.

For example:

<div>{{.}}</div>

translates to smth like this:

func Render(w io.Writer, data string) {
  io.WriteString(w, HTMLEscape(data))
}

Go has great escaping mechanism for html, but it's non-published API unfortunately.

From go v1.23 the //go:linkname directive is restricted, so it has made some things impossible. Re-implementing the whole escaping mechanism from scratch feels like very bad option.

It's possible to enrich the template tree by calling the html.template's Execute method, and it doesn't matter if it fails or not, cause the current implementation adds additional escapers before the actual execution. This trick works now, but could be broken at any moment, so it'd be great to publish the escape method https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/html/template/template.go;l=96

The other huge problem is the unpublished filters and escapers themselves: https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/html/template/escape.go;l=64

I couldn't find a way to call them w/o //go:linkname. It's possible to create a special template like {{_html_template_attrescaper .}} and execute it. It does work, but it's very slow. So it would be awesome to expose all the escapers from the funcMap variable.

These updates shouldn't break the API, but instead other tools can reuse it. It could be similar translators, or even other html template engines, cause escaping is the core part anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions