@@ -38,25 +38,19 @@ type JsonStringWriter interface {
38
38
stringWriter
39
39
}
40
40
41
- func WriteJsonString (buf JsonStringWriter , s string ) {
42
- WriteJson (buf , []byte (s ))
41
+ func WriteJsonString (buf JsonStringWriter , s string , escapeHTML bool ) {
42
+ WriteJson (buf , []byte (s ), escapeHTML )
43
43
}
44
44
45
45
/**
46
46
* Function ported from encoding/json: func (e *encodeState) string(s string) (int, error)
47
47
*/
48
- func WriteJson (buf JsonStringWriter , s []byte ) {
48
+ func WriteJson (buf JsonStringWriter , s []byte , escapeHTML bool ) {
49
49
buf .WriteByte ('"' )
50
50
start := 0
51
51
for i := 0 ; i < len (s ); {
52
52
if b := s [i ]; b < utf8 .RuneSelf {
53
- /*
54
- if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
55
- i++
56
- continue
57
- }
58
- */
59
- if lt [b ] == true {
53
+ if htmlSafeSet [b ] || (! escapeHTML && safeSet [b ]) {
60
54
i ++
61
55
continue
62
56
}
@@ -74,11 +68,15 @@ func WriteJson(buf JsonStringWriter, s []byte) {
74
68
case '\r' :
75
69
buf .WriteByte ('\\' )
76
70
buf .WriteByte ('r' )
71
+ case '\t' :
72
+ buf .WriteByte ('\\' )
73
+ buf .WriteByte ('t' )
77
74
default :
78
- // This encodes bytes < 0x20 except for \n and \r,
79
- // as well as < and >. The latter are escaped because they
80
- // can lead to security holes when user-controlled strings
81
- // are rendered into JSON and served to some browsers.
75
+ // This encodes bytes < 0x20 except for \t, \n and \r.
76
+ // If escapeHTML is set, it also escapes <, >, and &
77
+ // because they can lead to security holes when
78
+ // user-controlled strings are rendered into JSON
79
+ // and served to some browsers.
82
80
buf .WriteString (`\u00` )
83
81
buf .WriteByte (hex [b >> 4 ])
84
82
buf .WriteByte (hex [b & 0xF ])
0 commit comments