Commit b2b3187f authored by Nigel Tao's avatar Nigel Tao

exp/template/html: fix JS regexp escape of an empty string.

R=dsymonds
CC=golang-dev, mikesamuel
https://golang.org/cl/4972063
parent a5d0b7ee
...@@ -194,8 +194,13 @@ func TestEscape(t *testing.T) { ...@@ -194,8 +194,13 @@ func TestEscape(t *testing.T) {
}, },
{ {
"jsRe", "jsRe",
"<button onclick='alert(&quot;{{.H}}&quot;)'>", `<button onclick='alert(/{{"foo+bar"}}/.test(""))'>`,
`<button onclick='alert(&quot;\x3cHello\x3e&quot;)'>`, `<button onclick='alert(/foo\x2bbar/.test(""))'>`,
},
{
"jsReBlank",
`<script>alert(/{{""}}/.test(""));</script>`,
`<script>alert(/(?:)/.test(""));</script>`,
}, },
{ {
"styleBidiKeywordPassed", "styleBidiKeywordPassed",
......
...@@ -174,7 +174,12 @@ func jsStrEscaper(args ...interface{}) string { ...@@ -174,7 +174,12 @@ func jsStrEscaper(args ...interface{}) string {
// expression literal. /foo{{.X}}bar/ matches the string "foo" followed by // expression literal. /foo{{.X}}bar/ matches the string "foo" followed by
// the literal text of {{.X}} followed by the string "bar". // the literal text of {{.X}} followed by the string "bar".
func jsRegexpEscaper(args ...interface{}) string { 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. // stringify is an optimized form of fmt.Sprint.
......
...@@ -224,7 +224,7 @@ func TestJSRegexpEscaper(t *testing.T) { ...@@ -224,7 +224,7 @@ func TestJSRegexpEscaper(t *testing.T) {
x interface{} x interface{}
esc string esc string
}{ }{
{"", ``}, {"", `(?:)`},
{"foo", `foo`}, {"foo", `foo`},
{"\u0000", `\0`}, {"\u0000", `\0`},
{"\t", `\t`}, {"\t", `\t`},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment