summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Lemons <kyle@kylelemons.net>2011-08-17 12:12:08 -0400
committerKyle Lemons <kyle@kylelemons.net>2011-08-17 12:12:08 -0400
commit82cfe3292650ae099bb7e48d9a9da29b0106d9dd (patch)
treed24cb873ad916c93b621766b5c10a5da76f94110
parent5db9e76e4079757d96342f13d49cd3d4f077cca8 (diff)
downloadgo-82cfe3292650ae099bb7e48d9a9da29b0106d9dd.tar.gz
xml: escape string chardata in xml.Marshal
Fixes issue 2150. R=golang-dev, nigeltao, rsc CC=golang-dev http://codereview.appspot.com/4890042 Committer: Russ Cox <rsc@golang.org>
-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 f6e5bf5cd..ea421c1b1 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 77b2e726d..5b972fafe 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{