1
0
mirror of https://github.com/golang/go synced 2024-11-24 21:10:04 -07:00

xml: escape string chardata in xml.Marshal

Fixes #2150.

R=golang-dev, nigeltao, rsc
CC=golang-dev
https://golang.org/cl/4890042
This commit is contained in:
Kyle Lemons 2011-08-17 12:12:08 -04:00 committed by Russ Cox
parent 9f06ccb48d
commit ca6e1dbc22
2 changed files with 7 additions and 1 deletions

View 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)

View 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{