]> Cypherpunks repositories - gostls13.git/commitdiff
exp/template/html: do not escape the RHS of assignments
authorMike Samuel <mikesamuel@gmail.com>
Wed, 28 Sep 2011 05:08:14 +0000 (22:08 -0700)
committerMike Samuel <mikesamuel@gmail.com>
Wed, 28 Sep 2011 05:08:14 +0000 (22:08 -0700)
In

  {{$x := . | foo}}
  {{$x}}

the first action is a variable assignment that contributes
nothing to the output while the first is a use that needs
to be escaped.

This CL fixes escapeAction to distinguish assignments from
interpolations and to only modify interpolations.

R=nigeltao, r
CC=golang-dev
https://golang.org/cl/5143048

src/pkg/exp/template/html/escape.go
src/pkg/exp/template/html/escape_test.go

index 5ea819fc50a752b895009a10cb2e884aaadc2970..bb286c88445d8eef9815920f79f3fec865fd42f3 100644 (file)
@@ -153,6 +153,10 @@ func (e *escaper) escape(c context, n parse.Node) context {
 
 // escapeAction escapes an action template node.
 func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
+       if len(n.Pipe.Decl) != 0 {
+               // A local variable assignment, not an interpolation.
+               return c
+       }
        c = nudge(c)
        s := make([]string, 0, 3)
        switch c.state {
index d251cdb9a31b2b4e5529f773110640d3fd8e5322..c46445916571d4765139ebd648b41456a65d45b1 100644 (file)
@@ -68,10 +68,20 @@ func TestEscape(t *testing.T) {
                        "&lt;Goodbye&gt;!",
                },
                {
-                       "overescaping",
+                       "overescaping1",
                        "Hello, {{.C | html}}!",
                        "Hello, &lt;Cincinatti&gt;!",
                },
+               {
+                       "overescaping2",
+                       "Hello, {{html .C}}!",
+                       "Hello, &lt;Cincinatti&gt;!",
+               },
+               {
+                       "overescaping3",
+                       "{{with .C}}{{$msg := .}}Hello, {{$msg}}!{{end}}",
+                       "Hello, &lt;Cincinatti&gt;!",
+               },
                {
                        "assignment",
                        "{{if $x := .H}}{{$x}}{{end}}",