return string(s), contentTypeSrcset
}
}
- for i, arg := range args {
+ i := 0
+ for _, arg := range args {
+ // We skip untyped nil arguments for backward compatibility.
+ // Without this they would be output as <nil>, escaped.
+ // See issue 25875.
+ if arg == nil {
+ continue
+ }
+
args[i] = indirectToStringerOrError(arg)
+ i++
}
- return fmt.Sprint(args...), contentTypePlain
+ return fmt.Sprint(args[:i]...), contentTypePlain
}
testData := struct{ E error }{} // any non-empty interface here will do; error is just ready at hand
tmpl.Execute(got, testData)
- // Use this data instead of just hard-coding "<nil>" to avoid
- // dependencies on the html escaper and the behavior of fmt w.r.t. nil.
+ // A non-empty interface should print like an empty interface.
want := new(bytes.Buffer)
- data := struct{ E string }{E: fmt.Sprint(nil)}
+ data := struct{ E interface{} }{}
tmpl.Execute(want, data)
if !bytes.Equal(want.Bytes(), got.Bytes()) {
where urlescaper, attrescaper, and htmlescaper are aliases for internal escaping
functions.
+For these internal escaping functions, if an action pipeline evaluates to
+a nil interface value, it is treated as though it were an empty string.
+
Errors
See the documentation of ErrorCode for details.
A, E []string
B, M json.Marshaler
N int
- Z *int
+ U interface{} // untyped nil
+ Z *int // typed nil
W HTML
}{
F: false,
N: 42,
B: &badMarshaler{},
M: &goodMarshaler{},
+ U: nil,
Z: nil,
W: HTML(`¡<b class="foo">Hello</b>, <textarea>O'World</textarea>!`),
}
"{{.T}}",
"true",
},
+ {
+ "untypedNilValue",
+ "{{.U}}",
+ "",
+ },
+ {
+ "typedNilValue",
+ "{{.Z}}",
+ "<nil>",
+ },
{
"constant",
`<a href="/search?q={{"'a<b'"}}">`,
`<button onclick='alert( true )'>`,
},
{
- "jsNilValue",
+ "jsNilValueTyped",
"<button onclick='alert(typeof{{.Z}})'>",
`<button onclick='alert(typeof null )'>`,
},
+ {
+ "jsNilValueUntyped",
+ "<button onclick='alert(typeof{{.U}})'>",
+ `<button onclick='alert(typeof null )'>`,
+ },
{
"jsObjValue",
"<button onclick='alert({{.A}})'>",
{"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
"<script>alert("XSS");</script>", nil, true},
{"html", `{{html .PS}}`, "a string", tVal, true},
+ {"html typed nil", `{{html .NIL}}`, "<nil>", tVal, true},
+ {"html untyped nil", `{{html .Empty0}}`, "<no value>", tVal, true},
// JavaScript.
{"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true},