]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: fix Parse when called twice with empty text
authorJess Frazelle <me@jessfraz.com>
Thu, 23 Jun 2016 04:57:52 +0000 (21:57 -0700)
committerAndrew Gerrand <adg@golang.org>
Tue, 16 Aug 2016 23:49:42 +0000 (23:49 +0000)
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>

src/text/template/multi_test.go
src/text/template/template.go

index c8723cb7a8b1529978273c0d744ae94b39d2a551..8142f008fdf4602162c80cc86d21b949ebbe6fab 100644 (file)
@@ -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())
+               }
+       }
+}
index 7a7f42a715371e8533787685e0a52e98735be10d..a8ad6279e30a5af8e0b6ac3e49afdb0c3ba56ebc 100644 (file)
@@ -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