mirror of
https://github.com/golang/go
synced 2024-11-23 18:30:06 -07:00
text/template: fix Parse when called twice with empty text
Fixes #16156 Change-Id: I6989db4fd392583a2d490339cefc525b07c11b90 Reviewed-on: https://go-review.googlesource.com/24380 Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Andrew Gerrand <adg@golang.org>
This commit is contained in:
parent
9b88fac00c
commit
2b583a190e
@ -349,3 +349,39 @@ func TestParse(t *testing.T) {
|
||||
t.Fatalf("parsing test: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyTemplate(t *testing.T) {
|
||||
cases := []struct {
|
||||
defn []string
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{[]string{""}, "once", ""},
|
||||
{[]string{"", ""}, "twice", ""},
|
||||
{[]string{"{{.}}", "{{.}}"}, "twice", "twice"},
|
||||
{[]string{"{{/* a comment */}}", "{{/* a comment */}}"}, "comment", ""},
|
||||
{[]string{"{{.}}", ""}, "twice", ""},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
root := New("root")
|
||||
|
||||
var (
|
||||
m *Template
|
||||
err error
|
||||
)
|
||||
for _, d := range c.defn {
|
||||
m, err = root.New(c.in).Parse(d)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
buf := &bytes.Buffer{}
|
||||
if err := m.Execute(buf, c.in); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if buf.String() != c.want {
|
||||
t.Errorf("expected string %q: got %q", c.want, buf.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -208,7 +208,7 @@ func (t *Template) associate(new *Template, tree *parse.Tree) (bool, error) {
|
||||
if new.common != t.common {
|
||||
panic("internal error: associate not common")
|
||||
}
|
||||
if t.tmpl[new.name] != nil && parse.IsEmptyTree(tree.Root) {
|
||||
if t.tmpl[new.name] != nil && parse.IsEmptyTree(tree.Root) && t.Tree != nil {
|
||||
// If a template by that name exists,
|
||||
// don't replace it with an empty template.
|
||||
return false, nil
|
||||
|
Loading…
Reference in New Issue
Block a user