From a005a8d1b4ab37c1f5fd200fc57d2a67ce87c8ac Mon Sep 17 00:00:00 2001 From: Samuel Tan Date: Wed, 5 Jul 2017 16:28:41 -0700 Subject: 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 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/html/template/template.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/html/template/template.go') 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 -- cgit v1.3-5-g9baa