]> Cypherpunks repositories - gostls13.git/commitdiff
template: fixed html formatter bug where it would turn a []byte
authorAndrew Gerrand <adg@golang.org>
Thu, 18 Mar 2010 22:46:39 +0000 (09:46 +1100)
committerAndrew Gerrand <adg@golang.org>
Thu, 18 Mar 2010 22:46:39 +0000 (09:46 +1100)
into a string of decimal numbers.

R=r, rsc
CC=golang-dev
https://golang.org/cl/624041

src/pkg/template/format.go
src/pkg/template/template_test.go

index 717dcbdbb66e4092956b5b75a29ce3214f6a5c6f..8a31de970a3af28bf12249e1e100ea01f081ac8e 100644 (file)
@@ -61,7 +61,11 @@ func HTMLEscape(w io.Writer, s []byte) {
 
 // HTMLFormatter formats arbitrary values for HTML
 func HTMLFormatter(w io.Writer, value interface{}, format string) {
-       var b bytes.Buffer
-       fmt.Fprint(&b, value)
-       HTMLEscape(w, b.Bytes())
+       b, ok := value.([]byte)
+       if !ok {
+               var buf bytes.Buffer
+               fmt.Fprint(&buf, value)
+               b = buf.Bytes()
+       }
+       HTMLEscape(w, b)
 }
index aaf7f2ec329bb787b4731debafd1b9bfccececfe..2dd6468077b7b2b8b5274ac45d0c70d130f4a030 100644 (file)
@@ -565,3 +565,14 @@ func TestVarIndirection(t *testing.T) {
                t.Errorf("for %q: expected %q got %q", input, expect, buf.String())
        }
 }
+
+func TestHTMLFormatterWithByte(t *testing.T) {
+       s := "Test string."
+       b := []byte(s)
+       var buf bytes.Buffer
+       HTMLFormatter(&buf, b, "")
+       bs := buf.String()
+       if bs != s {
+               t.Errorf("munged []byte, expected: %s got: %s", s, bs)
+       }
+}