Commit d92bc7a5 authored by Russ Cox's avatar Russ Cox

encoding/json: document HTML escaping in Compact

Make explicit that Compact does HTML escaping.

Fixes #30357.

Change-Id: I4648f8f3e907d659db977d07253f716df6e07d7b
Reviewed-on: https://go-review.googlesource.com/c/go/+/173417
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent b51c1570
...@@ -45,11 +45,12 @@ import ( ...@@ -45,11 +45,12 @@ import (
// //
// String values encode as JSON strings coerced to valid UTF-8, // String values encode as JSON strings coerced to valid UTF-8,
// replacing invalid bytes with the Unicode replacement rune. // replacing invalid bytes with the Unicode replacement rune.
// The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e" // So that the JSON will be safe to embed inside HTML <script> tags,
// to keep some browsers from misinterpreting JSON output as HTML. // the string is encoded using HTMLEscape,
// Ampersand "&" is also escaped to "\u0026" for the same reason. // which replaces "<", ">", "&", U+2028, and U+2029 are escaped
// This escaping can be disabled using an Encoder that had SetEscapeHTML(false) // to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029".
// called on it. // This replacement can be disabled when using an Encoder,
// by calling SetEscapeHTML(false).
// //
// Array and slice values encode as JSON arrays, except that // Array and slice values encode as JSON arrays, except that
// []byte encodes as a base64-encoded string, and a nil slice // []byte encodes as a base64-encoded string, and a nil slice
......
...@@ -8,6 +8,9 @@ import "bytes" ...@@ -8,6 +8,9 @@ import "bytes"
// Compact appends to dst the JSON-encoded src with // Compact appends to dst the JSON-encoded src with
// insignificant space characters elided. // insignificant space characters elided.
// Like Marshal, Compact applies HTMLEscape to any
// string literals so that the JSON will be safe to embed
// inside HTML <script> tags.
func Compact(dst *bytes.Buffer, src []byte) error { func Compact(dst *bytes.Buffer, src []byte) error {
return compact(dst, src, false) return compact(dst, src, false)
} }
......
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