diff options
| author | Samuel Tan <samueltan@google.com> | 2017-07-05 16:28:41 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2017-07-14 18:19:04 +0000 |
| commit | a005a8d1b4ab37c1f5fd200fc57d2a67ce87c8ac (patch) | |
| tree | ad9ea47f38a359bcbf35831cdb8d5ac8e0232950 /src/html/template/template.go | |
| parent | 9664bc1d1ff8ee2ddcea37d335ca4510a57a1e0c (diff) | |
| download | go-a005a8d1b4ab37c1f5fd200fc57d2a67ce87c8ac.tar.xz | |
html/template: use the same escaper across multiple template executions
The escaper contains information about which templates have already been
visited and escaped. This information is necessary to prevent templates
that have already been escaped from being over-escaped. However, since we
currently create a new escaper each time we execute a template, this
information does not persist across multiple template executions.
Fix this by saving an escaper in each template name space which is shared by
all templates in that name space.
While there, fix error message formatting for an escaping unit test.
Fixes #20842
Change-Id: Ie392c3e7ce0e0a9947bdf56c99e926e7c7db76e4
Reviewed-on: https://go-review.googlesource.com/47256
Reviewed-by: Mike Samuel <mikesamuel@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/html/template/template.go')
| -rw-r--r-- | src/html/template/template.go | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/html/template/template.go b/src/html/template/template.go index 246ef04dbe..6a661bf6e5 100644 --- a/src/html/template/template.go +++ b/src/html/template/template.go @@ -36,6 +36,7 @@ type nameSpace struct { mu sync.Mutex set map[string]*Template escaped bool + esc escaper } // Templates returns a slice of the templates associated with t, including t @@ -250,13 +251,13 @@ func (t *Template) Clone() (*Template, error) { if err != nil { return nil, err } + ns := &nameSpace{set: make(map[string]*Template)} + ns.esc = makeEscaper(ns) ret := &Template{ nil, textClone, textClone.Tree, - &nameSpace{ - set: make(map[string]*Template), - }, + ns, } ret.set[ret.Name()] = ret for _, x := range textClone.Templates() { @@ -279,13 +280,13 @@ func (t *Template) Clone() (*Template, error) { // New allocates a new HTML template with the given name. func New(name string) *Template { + ns := &nameSpace{set: make(map[string]*Template)} + ns.esc = makeEscaper(ns) tmpl := &Template{ nil, template.New(name), nil, - &nameSpace{ - set: make(map[string]*Template), - }, + ns, } tmpl.set[name] = tmpl return tmpl |
