From: Kyle Lemons Date: Wed, 17 Aug 2011 16:12:08 +0000 (-0400) Subject: xml: escape string chardata in xml.Marshal X-Git-Tag: weekly.2011-08-17~15 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ca6e1dbc2258e95e9ac1a1269768e544a259964a;p=gostls13.git xml: escape string chardata in xml.Marshal Fixes #2150. R=golang-dev, nigeltao, rsc CC=golang-dev https://golang.org/cl/4890042 --- diff --git a/src/pkg/xml/marshal.go b/src/pkg/xml/marshal.go index f6e5bf5cdb..ea421c1b17 100644 --- a/src/pkg/xml/marshal.go +++ b/src/pkg/xml/marshal.go @@ -177,7 +177,7 @@ func (p *printer) marshalValue(val reflect.Value, name string) os.Error { case "": case "chardata": if tk := f.Type.Kind(); tk == reflect.String { - p.Write([]byte(val.Field(i).String())) + Escape(p, []byte(val.Field(i).String())) } else if tk == reflect.Slice { if elem, ok := val.Field(i).Interface().([]byte); ok { Escape(p, elem) diff --git a/src/pkg/xml/marshal_test.go b/src/pkg/xml/marshal_test.go index 77b2e726d5..5b972fafe6 100644 --- a/src/pkg/xml/marshal_test.go +++ b/src/pkg/xml/marshal_test.go @@ -57,6 +57,11 @@ type Domain struct { Name []byte `xml:"chardata"` } +type Book struct { + XMLName Name `xml:"book"` + Title string `xml:"chardata"` +} + type SecretAgent struct { XMLName Name `xml:"agent"` Handle string `xml:"attr"` @@ -113,6 +118,7 @@ var marshalTests = []struct { {Value: &Port{Number: "443"}, ExpectXML: `443`}, {Value: &Port{Type: ""}, ExpectXML: ``}, {Value: &Domain{Name: []byte("google.com&friends")}, ExpectXML: `google.com&friends`}, + {Value: &Book{Title: "Pride & Prejudice"}, ExpectXML: `Pride & Prejudice`}, {Value: atomValue, ExpectXML: atomXml}, { Value: &Ship{