From: Samuel Tan Date: Thu, 14 Dec 2017 06:38:00 +0000 (-0800) Subject: Revert "html/template: prevent aliasing of parse Trees via AddParseTree" X-Git-Tag: go1.10beta2~32 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bf897845363eee8e39d3d4c02d3aadc864b5faf0;p=gostls13.git Revert "html/template: prevent aliasing of parse Trees via AddParseTree" This reverts commit cd0a5f08293e1bf1fac41ae6438d495318cd52fb, which unnecessarily restricts the use of AddParseTree. Change-Id: I1155214a20ba08981d604404e79fff54874fd8e4 Reviewed-on: https://go-review.googlesource.com/83919 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Russ Cox --- diff --git a/src/html/template/escape_test.go b/src/html/template/escape_test.go index 949985fe4a..96684793bd 100644 --- a/src/html/template/escape_test.go +++ b/src/html/template/escape_test.go @@ -1846,7 +1846,7 @@ func TestErrorOnUndefined(t *testing.T) { err := tmpl.Execute(nil, nil) if err == nil { - t.Fatal("expected error") + t.Error("expected error") } if !strings.Contains(err.Error(), "incomplete") { t.Errorf("expected error about incomplete template; got %s", err) @@ -1866,10 +1866,10 @@ func TestIdempotentExecute(t *testing.T) { for i := 0; i < 2; i++ { err = tmpl.ExecuteTemplate(got, "hello", nil) if err != nil { - t.Fatalf("unexpected error: %s", err) + t.Errorf("unexpected error: %s", err) } if got.String() != want { - t.Fatalf("after executing template \"hello\", got:\n\t%q\nwant:\n\t%q\n", got.String(), want) + t.Errorf("after executing template \"hello\", got:\n\t%q\nwant:\n\t%q\n", got.String(), want) } got.Reset() } @@ -1877,7 +1877,7 @@ func TestIdempotentExecute(t *testing.T) { // "main" does not cause the output of "hello" to change. err = tmpl.ExecuteTemplate(got, "main", nil) if err != nil { - t.Fatalf("unexpected error: %s", err) + t.Errorf("unexpected error: %s", err) } // If the HTML escaper is added again to the action {{"Ladies & Gentlemen!"}}, // we would expected to see the ampersand overescaped to "&amp;". @@ -1887,19 +1887,6 @@ func TestIdempotentExecute(t *testing.T) { } } -// This covers issue #21844. -func TestAddExistingTreeError(t *testing.T) { - tmpl := Must(New("foo").Parse(`

{{.}}

`)) - tmpl, err := tmpl.AddParseTree("bar", tmpl.Tree) - if err == nil { - t.Fatalf("expected error after AddParseTree") - } - const want = `html/template: cannot add parse tree that template "foo" already references` - if got := err.Error(); got != want { - t.Errorf("got error:\n\t%q\nwant:\n\t%q\n", got, want) - } -} - func BenchmarkEscapedExecute(b *testing.B) { tmpl := Must(New("t").Parse(`{{.}}`)) var buf bytes.Buffer diff --git a/src/html/template/template.go b/src/html/template/template.go index 9dc066855f..4641a37da3 100644 --- a/src/html/template/template.go +++ b/src/html/template/template.go @@ -219,11 +219,6 @@ func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error t.nameSpace.mu.Lock() defer t.nameSpace.mu.Unlock() - for _, tmpl := range t.set { - if tmpl.Tree == tree { - return nil, fmt.Errorf("html/template: cannot add parse tree that template %q already references", tmpl.Name()) - } - } text, err := t.text.AddParseTree(name, tree) if err != nil { return nil, err