From c8ea03828b0645b1fd5725888e44873b75fcfbb6 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Wed, 22 Apr 2020 22:17:56 +0300 Subject: text/template: add CommentNode to template parse tree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #34652 Change-Id: Icf6e3eda593fed826736f34f95a9d66f5450cc98 Reviewed-on: https://go-review.googlesource.com/c/go/+/229398 Reviewed-by: Daniel Martí Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot --- src/html/template/escape.go | 2 ++ src/html/template/template_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) (limited to 'src/html') diff --git a/src/html/template/escape.go b/src/html/template/escape.go index f12dafa870..8739735cb7 100644 --- a/src/html/template/escape.go +++ b/src/html/template/escape.go @@ -124,6 +124,8 @@ func (e *escaper) escape(c context, n parse.Node) context { switch n := n.(type) { case *parse.ActionNode: return e.escapeAction(c, n) + case *parse.CommentNode: + return c case *parse.IfNode: return e.escapeBranch(c, &n.BranchNode, "if") case *parse.ListNode: diff --git a/src/html/template/template_test.go b/src/html/template/template_test.go index 86bd4db444..1f2c888bbe 100644 --- a/src/html/template/template_test.go +++ b/src/html/template/template_test.go @@ -10,6 +10,7 @@ import ( . "html/template" "strings" "testing" + "text/template/parse" ) func TestTemplateClone(t *testing.T) { @@ -160,6 +161,21 @@ func TestStringsInScriptsWithJsonContentTypeAreCorrectlyEscaped(t *testing.T) { } } +func TestSkipEscapeComments(t *testing.T) { + c := newTestCase(t) + tr := parse.New("root") + tr.Mode = parse.ParseComments + newT, err := tr.Parse("{{/* A comment */}}{{ 1 }}{{/* Another comment */}}", "", "", make(map[string]*parse.Tree)) + if err != nil { + t.Fatalf("Cannot parse template text: %v", err) + } + c.root, err = c.root.AddParseTree("root", newT) + if err != nil { + t.Fatalf("Cannot add parse tree to template: %v", err) + } + c.mustExecute(c.root, nil, "1") +} + type testCase struct { t *testing.T root *Template -- cgit v1.3-5-g9baa