]> Cypherpunks repositories - gostls13.git/commitdiff
xml: escape string chardata in xml.Marshal
authorKyle Lemons <kyle@kylelemons.net>
Wed, 17 Aug 2011 16:12:08 +0000 (12:12 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 17 Aug 2011 16:12:08 +0000 (12:12 -0400)
Fixes #2150.

R=golang-dev, nigeltao, rsc
CC=golang-dev
https://golang.org/cl/4890042

src/pkg/xml/marshal.go
src/pkg/xml/marshal_test.go

index f6e5bf5cdba5d3f6a184ea3a661f7c512533dc16..ea421c1b17dfe81439ecb906bd99cc7f66a540ee 100644 (file)
@@ -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)
index 77b2e726d5ad6ad54d00e55e0bc128985c919b56..5b972fafe675d54ec491b15a3f494cb588006dc5 100644 (file)
@@ -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{