aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Lemons <kyle@kylelemons.net>2011-08-17 12:12:08 -0400
committerRuss Cox <rsc@golang.org>2011-08-17 12:12:08 -0400
commitca6e1dbc2258e95e9ac1a1269768e544a259964a (patch)
tree51761d3e76b0d08d841082871cefd1d3731074fd
parent9f06ccb48d9bb17deeb7515c521993879141334d (diff)
downloadgo-ca6e1dbc2258e95e9ac1a1269768e544a259964a.tar.gz
go-ca6e1dbc2258e95e9ac1a1269768e544a259964a.zip
xml: escape string chardata in xml.Marshal
Fixes #2150. R=golang-dev, nigeltao, rsc CC=golang-dev https://golang.org/cl/4890042
-rw-r--r--src/pkg/xml/marshal.go2
-rw-r--r--src/pkg/xml/marshal_test.go6
2 files changed, 7 insertions, 1 deletions
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: `<port>443</port>`},
{Value: &Port{Type: "<unix>"}, ExpectXML: `<port type="&lt;unix&gt;"></port>`},
{Value: &Domain{Name: []byte("google.com&friends")}, ExpectXML: `<domain>google.com&amp;friends</domain>`},
+ {Value: &Book{Title: "Pride & Prejudice"}, ExpectXML: `<book>Pride &amp; Prejudice</book>`},
{Value: atomValue, ExpectXML: atomXml},
{
Value: &Ship{