Commit 80f39f7b authored by Rob Pike's avatar Rob Pike

html/template: export the parse.Tree for the escaped template

The underlying parse tree is visible in text/template, so it should be visible here.
Done by copying the underlying *parse.Tree up to the top level of the struct, and then making sure it's kept up to date.
Fixes #6318.

R=mikesamuel
CC=golang-dev
https://golang.org/cl/13479044
parent 2b44b364
...@@ -35,11 +35,13 @@ func escapeTemplates(tmpl *Template, names ...string) error { ...@@ -35,11 +35,13 @@ func escapeTemplates(tmpl *Template, names ...string) error {
for _, name := range names { for _, name := range names {
if t := tmpl.set[name]; t != nil { if t := tmpl.set[name]; t != nil {
t.text.Tree = nil t.text.Tree = nil
t.Tree = nil
} }
} }
return err return err
} }
tmpl.escaped = true tmpl.escaped = true
tmpl.Tree = tmpl.text.Tree
} }
e.commit() e.commit()
return nil return nil
......
...@@ -673,6 +673,10 @@ func TestEscape(t *testing.T) { ...@@ -673,6 +673,10 @@ func TestEscape(t *testing.T) {
t.Errorf("%s: escaped output for pointer: want\n\t%q\ngot\n\t%q", test.name, w, g) t.Errorf("%s: escaped output for pointer: want\n\t%q\ngot\n\t%q", test.name, w, g)
continue continue
} }
if tmpl.Tree != tmpl.text.Tree {
t.Errorf("%s: tree mismatch", test.name)
continue
}
} }
} }
......
...@@ -22,6 +22,8 @@ type Template struct { ...@@ -22,6 +22,8 @@ type Template struct {
// we need to keep our version of the name space and the underlying // we need to keep our version of the name space and the underlying
// template's in sync. // template's in sync.
text *template.Template text *template.Template
// The underlying template's parse tree, updated to be HTML-safe.
Tree *parse.Tree
*nameSpace // common to all associated templates *nameSpace // common to all associated templates
} }
...@@ -149,6 +151,7 @@ func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error ...@@ -149,6 +151,7 @@ func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error
ret := &Template{ ret := &Template{
false, false,
text, text,
text.Tree,
t.nameSpace, t.nameSpace,
} }
t.set[name] = ret t.set[name] = ret
...@@ -176,6 +179,7 @@ func (t *Template) Clone() (*Template, error) { ...@@ -176,6 +179,7 @@ func (t *Template) Clone() (*Template, error) {
ret := &Template{ ret := &Template{
false, false,
textClone, textClone,
textClone.Tree,
&nameSpace{ &nameSpace{
set: make(map[string]*Template), set: make(map[string]*Template),
}, },
...@@ -195,6 +199,7 @@ func (t *Template) Clone() (*Template, error) { ...@@ -195,6 +199,7 @@ func (t *Template) Clone() (*Template, error) {
ret.set[name] = &Template{ ret.set[name] = &Template{
false, false,
x, x,
x.Tree,
ret.nameSpace, ret.nameSpace,
} }
} }
...@@ -206,6 +211,7 @@ func New(name string) *Template { ...@@ -206,6 +211,7 @@ func New(name string) *Template {
tmpl := &Template{ tmpl := &Template{
false, false,
template.New(name), template.New(name),
nil,
&nameSpace{ &nameSpace{
set: make(map[string]*Template), set: make(map[string]*Template),
}, },
...@@ -228,6 +234,7 @@ func (t *Template) new(name string) *Template { ...@@ -228,6 +234,7 @@ func (t *Template) new(name string) *Template {
tmpl := &Template{ tmpl := &Template{
false, false,
t.text.New(name), t.text.New(name),
nil,
t.nameSpace, t.nameSpace,
} }
tmpl.set[name] = tmpl tmpl.set[name] = tmpl
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment