Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
html/template: include itself while cloning
Browse files Browse the repository at this point in the history
template.Clone() initialized template set incorrectly:
it didn't include itself.

* include itself in template set while cloning
* add a test

Fixes golang#12996

Change-Id: I932530e4f7f1bbebf833e12b000a5ce052bc9223
Reviewed-on: https://go-review.googlesource.com/16104
Reviewed-by: Andrew Gerrand <[email protected]>
  • Loading branch information
nodirt authored and adg committed Oct 20, 2015
1 parent 09eb588 commit cf59c1f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/html/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func (t *Template) Clone() (*Template, error) {
set: make(map[string]*Template),
},
}
ret.set[ret.Name()] = ret
for _, x := range textClone.Templates() {
name := x.Name()
src := t.set[name]
Expand Down
29 changes: 29 additions & 0 deletions src/html/template/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package template

import (
"bytes"
"testing"
)

func TestTemplateClone(t *testing.T) {
// https://golang.org/issue/12996
orig := New("name")
clone, err := orig.Clone()
if err != nil {
t.Fatal(err)
}
if len(clone.Templates()) != len(orig.Templates()) {
t.Fatalf("Invalid lenth of t.Clone().Templates()")
}

const want = "stuff"
parsed := Must(clone.Parse(want))
var buf bytes.Buffer
err = parsed.Execute(&buf, nil)
if err != nil {
t.Fatal(err)
}
if got := buf.String(); got != want {
t.Fatalf("got %q; want %q", got, want)
}
}

0 comments on commit cf59c1f

Please sign in to comment.