]> Cypherpunks repositories - gostls13.git/commitdiff
exp/template/html: fix JS regexp escape of an empty string.
authorNigel Tao <nigeltao@golang.org>
Mon, 12 Sep 2011 01:57:34 +0000 (11:57 +1000)
committerNigel Tao <nigeltao@golang.org>
Mon, 12 Sep 2011 01:57:34 +0000 (11:57 +1000)
R=dsymonds
CC=golang-dev, mikesamuel
https://golang.org/cl/4972063

src/pkg/exp/template/html/escape_test.go
src/pkg/exp/template/html/js.go
src/pkg/exp/template/html/js_test.go

index efd97fced5b9deb3690631a129e0740221e02234..23f573498776b1ead84a98030d619c9893a372c9 100644 (file)
@@ -194,8 +194,13 @@ func TestEscape(t *testing.T) {
                },
                {
                        "jsRe",
-                       "<button onclick='alert(&quot;{{.H}}&quot;)'>",
-                       `<button onclick='alert(&quot;\x3cHello\x3e&quot;)'>`,
+                       `<button onclick='alert(/{{"foo+bar"}}/.test(""))'>`,
+                       `<button onclick='alert(/foo\x2bbar/.test(""))'>`,
+               },
+               {
+                       "jsReBlank",
+                       `<script>alert(/{{""}}/.test(""));</script>`,
+                       `<script>alert(/(?:)/.test(""));</script>`,
                },
                {
                        "styleBidiKeywordPassed",
index 41476519036bd95340427c2238fe0b29740280a4..f9251a053baab44d79168c24c08afbe0f056b8b1 100644 (file)
@@ -174,7 +174,12 @@ func jsStrEscaper(args ...interface{}) string {
 // expression literal. /foo{{.X}}bar/ matches the string "foo" followed by
 // the literal text of {{.X}} followed by the string "bar".
 func jsRegexpEscaper(args ...interface{}) string {
-       return replace(stringify(args...), jsRegexpReplacementTable)
+       s := replace(stringify(args...), jsRegexpReplacementTable)
+       if s == "" {
+               // /{{.X}}/ should not produce a line comment when .X == "".
+               return "(?:)"
+       }
+       return s
 }
 
 // stringify is an optimized form of fmt.Sprint.
index 0ae70d1e2b618b427ca6a9d3e1186e7e0f287541..76fc23845d278fa92b4df03ce0d76ca5674498e5 100644 (file)
@@ -224,7 +224,7 @@ func TestJSRegexpEscaper(t *testing.T) {
                x   interface{}
                esc string
        }{
-               {"", ``},
+               {"", `(?:)`},
                {"foo", `foo`},
                {"\u0000", `\0`},
                {"\t", `\t`},