Commit ca6e1dbc authored by Kyle Lemons's avatar Kyle Lemons Committed by Russ Cox

xml: escape string chardata in xml.Marshal

Fixes #2150.

R=golang-dev, nigeltao, rsc
CC=golang-dev
https://golang.org/cl/4890042
parent 9f06ccb4
......@@ -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)
......
......@@ -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{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment