From e60cffa4ca9ae726d96b53817d82d98402017772 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 21 Dec 2020 11:21:59 -0800 Subject: html/template: attach functions to namespace The text/template functions are stored in a data structure shared by all related templates, so do the same with the original, unwrapped, functions on the html/template side. For #39807 Fixes #43295 Change-Id: I9f64a0a601f1151c863a2833b5be2baf649b6cef Reviewed-on: https://go-review.googlesource.com/c/go/+/279492 Trust: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Emmanuel Odeke --- src/html/template/exec_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/html/template/exec_test.go') diff --git a/src/html/template/exec_test.go b/src/html/template/exec_test.go index eb00824260..cd6b78a1a9 100644 --- a/src/html/template/exec_test.go +++ b/src/html/template/exec_test.go @@ -1776,3 +1776,23 @@ func TestRecursiveExecute(t *testing.T) { t.Fatal(err) } } + +// Issue 43295. +func TestTemplateFuncsAfterClone(t *testing.T) { + s := `{{ f . }}` + want := "test" + orig := New("orig").Funcs(map[string]interface{}{ + "f": func(in string) string { + return in + }, + }).New("child") + + overviewTmpl := Must(Must(orig.Clone()).Parse(s)) + var out strings.Builder + if err := overviewTmpl.Execute(&out, want); err != nil { + t.Fatal(err) + } + if got := out.String(); got != want { + t.Fatalf("got %q; want %q", got, want) + } +} -- cgit v1.3-5-g9baa