From d754647963d41bcd96ea4d12d824f01e8c50f076 Mon Sep 17 00:00:00 2001 From: David Symonds Date: Fri, 12 Jul 2013 14:35:55 +1000 Subject: [PATCH] encoding/json: escape U+2028 and U+2029. Fixes #5836. R=golang-dev, bradfitz, r, rsc CC=golang-dev https://golang.org/cl/10883045 --- src/pkg/encoding/json/decode_test.go | 6 ++--- src/pkg/encoding/json/encode.go | 32 ++++++++++++++++++++++++--- src/pkg/encoding/json/indent.go | 9 ++++++++ src/pkg/encoding/json/scanner_test.go | 19 ++++++++++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/pkg/encoding/json/decode_test.go b/src/pkg/encoding/json/decode_test.go index 97cbb4f09b..1191d6cee5 100644 --- a/src/pkg/encoding/json/decode_test.go +++ b/src/pkg/encoding/json/decode_test.go @@ -568,14 +568,14 @@ func TestUnmarshalPtrPtr(t *testing.T) { } func TestEscape(t *testing.T) { - const input = `"foobar"` - const expected = `"\"foobar\"\u003chtml\u003e"` + const input = `"foobar"` + " [\u2028 \u2029]" + const expected = `"\"foobar\"\u003chtml\u003e [\u2028 \u2029]"` b, err := Marshal(input) if err != nil { t.Fatalf("Marshal error: %v", err) } if s := string(b); s != expected { - t.Errorf("Encoding of [%s] was [%s], want [%s]", input, s, expected) + t.Errorf("Encoding of [%s]:\n got [%s]\nwant [%s]", input, s, expected) } } diff --git a/src/pkg/encoding/json/encode.go b/src/pkg/encoding/json/encode.go index e25a9b8805..55df9b5768 100644 --- a/src/pkg/encoding/json/encode.go +++ b/src/pkg/encoding/json/encode.go @@ -149,14 +149,14 @@ func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { return buf.Bytes(), nil } -// HTMLEscape appends to dst the JSON-encoded src with <, >, and & -// characters inside string literals changed to \u003c, \u003e, \u0026 +// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 +// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 // so that the JSON will be safe to embed inside HTML