Commit 467c726e authored by Robert Griesemer's avatar Robert Griesemer

add " and ' to list of html-escaped chars

R=rsc
http://go/go-review/1017025
parent 796e29eb
...@@ -21,29 +21,38 @@ func StringFormatter(w io.Writer, value interface{}, format string) { ...@@ -21,29 +21,38 @@ func StringFormatter(w io.Writer, value interface{}, format string) {
fmt.Fprint(w, value); fmt.Fprint(w, value);
} }
var (
var esc_amp = strings.Bytes("&") esc_quot = strings.Bytes("""); // shorter than """
var esc_lt = strings.Bytes("<") esc_apos = strings.Bytes("'"); // shorter than "'"
var esc_gt = strings.Bytes(">") esc_amp = strings.Bytes("&");
esc_lt = strings.Bytes("<");
esc_gt = strings.Bytes(">");
)
// HtmlEscape writes to w the properly escaped HTML equivalent // HtmlEscape writes to w the properly escaped HTML equivalent
// of the plain text data s. // of the plain text data s.
func HtmlEscape(w io.Writer, s []byte) { func HtmlEscape(w io.Writer, s []byte) {
var esc []byte;
last := 0; last := 0;
for i, c := range s { for i, c := range s {
if c == '&' || c == '<' || c == '>' {
w.Write(s[last:i]);
switch c { switch c {
case '"':
esc = esc_quot;
case '\'':
esc = esc_apos;
case '&': case '&':
w.Write(esc_amp); esc = esc_amp;
case '<': case '<':
w.Write(esc_lt); esc = esc_lt;
case '>': case '>':
w.Write(esc_gt); esc = esc_gt;
default:
continue;
} }
w.Write(s[last:i]);
w.Write(esc);
last = i+1; last = i+1;
} }
}
w.Write(s[last:len(s)]); w.Write(s[last:len(s)]);
} }
......
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