exp/template/html: do not escape the RHS of assignments

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
This commit is contained in:
Mike Samuel 2011-09-27 22:08:14 -07:00
parent 71557713b0
commit 260991ad5f
2 changed files with 15 additions and 1 deletions

View File

@ -153,6 +153,10 @@ func (e *escaper) escape(c context, n parse.Node) context {
// escapeAction escapes an action template node. // escapeAction escapes an action template node.
func (e *escaper) escapeAction(c context, n *parse.ActionNode) context { 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) c = nudge(c)
s := make([]string, 0, 3) s := make([]string, 0, 3)
switch c.state { switch c.state {

View File

@ -68,10 +68,20 @@ func TestEscape(t *testing.T) {
"<Goodbye>!", "<Goodbye>!",
}, },
{ {
"overescaping", "overescaping1",
"Hello, {{.C | html}}!", "Hello, {{.C | html}}!",
"Hello, <Cincinatti>!", "Hello, <Cincinatti>!",
}, },
{
"overescaping2",
"Hello, {{html .C}}!",
"Hello, <Cincinatti>!",
},
{
"overescaping3",
"{{with .C}}{{$msg := .}}Hello, {{$msg}}!{{end}}",
"Hello, <Cincinatti>!",
},
{ {
"assignment", "assignment",
"{{if $x := .H}}{{$x}}{{end}}", "{{if $x := .H}}{{$x}}{{end}}",