From 260991ad5f1cd73ff9f993bb389ac6d7e56fb509 Mon Sep 17 00:00:00 2001 From: Mike Samuel Date: Tue, 27 Sep 2011 22:08:14 -0700 Subject: [PATCH] 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 --- src/pkg/exp/template/html/escape.go | 4 ++++ src/pkg/exp/template/html/escape_test.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/pkg/exp/template/html/escape.go b/src/pkg/exp/template/html/escape.go index 5ea819fc50..bb286c8844 100644 --- a/src/pkg/exp/template/html/escape.go +++ b/src/pkg/exp/template/html/escape.go @@ -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 { diff --git a/src/pkg/exp/template/html/escape_test.go b/src/pkg/exp/template/html/escape_test.go index d251cdb9a3..c464459165 100644 --- a/src/pkg/exp/template/html/escape_test.go +++ b/src/pkg/exp/template/html/escape_test.go @@ -68,10 +68,20 @@ func TestEscape(t *testing.T) { "<Goodbye>!", }, { - "overescaping", + "overescaping1", "Hello, {{.C | html}}!", "Hello, <Cincinatti>!", }, + { + "overescaping2", + "Hello, {{html .C}}!", + "Hello, <Cincinatti>!", + }, + { + "overescaping3", + "{{with .C}}{{$msg := .}}Hello, {{$msg}}!{{end}}", + "Hello, <Cincinatti>!", + }, { "assignment", "{{if $x := .H}}{{$x}}{{end}}",