]> Cypherpunks repositories - gostls13.git/commitdiff
exp/template/html: fix bug, '<' normalization for text nodes that change context
authorMike Samuel <mikesamuel@gmail.com>
Wed, 21 Sep 2011 05:55:14 +0000 (22:55 -0700)
committerMike Samuel <mikesamuel@gmail.com>
Wed, 21 Sep 2011 05:55:14 +0000 (22:55 -0700)
R=nigeltao
CC=golang-dev
https://golang.org/cl/5080042

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

index 6490c5f9e0b073ce606e8c0cc5a2d01349cc8eb1..050746c1b2a46e5fce93f53e5e4c5b8058cbf93a 100644 (file)
@@ -551,8 +551,17 @@ func (e *escaper) escapeText(c context, n *parse.TextNode) context {
        for i != len(s) {
                c1, nread := contextAfterText(c, s[i:])
                i1 := i + nread
-               if c.state == c1.state && (c.state == stateText || c.state == stateRCDATA) {
-                       for j := i; j < i1; j++ {
+               if c.state == stateText || c.state == stateRCDATA {
+                       end := i1
+                       if c1.state != c.state {
+                               for j := end - 1; j >= i; j-- {
+                                       if s[j] == '<' {
+                                               end = j
+                                               break
+                                       }
+                               }
+                       }
+                       for j := i; j < end; j++ {
                                if s[j] == '<' {
                                        b.Write(s[written:j])
                                        b.WriteString("&lt;")
index 594a9606d7591a67e9180f984ee4188c5bf68bd9..cf1c8280028c9d5e2fc1969e9a2b6635e48202da 100644 (file)
@@ -366,6 +366,26 @@ func TestEscape(t *testing.T) {
                        // TODO: Elide comment.
                        "<b>Hello, <!-- name of world -->&lt;Cincinatti&gt;</b>",
                },
+               {
+                       "HTML comment not first < in text node.",
+                       "<<!-- -->!--",
+                       "&lt;<!-- -->!--",
+               },
+               {
+                       "HTML normalization 1",
+                       "a < b",
+                       "a &lt; b",
+               },
+               {
+                       "HTML normalization 2",
+                       "a << b",
+                       "a &lt;&lt; b",
+               },
+               {
+                       "HTML normalization 3",
+                       "a<<!-- --><!-- -->b",
+                       "a&lt;<!-- --><!-- -->b",
+               },
                {
                        "Split HTML comment",
                        "<b>Hello, <!-- name of {{if .T}}city -->{{.C}}{{else}}world -->{{.W}}{{end}}</b>",