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:
parent
9f06ccb48d
commit
ca6e1dbc22
@ -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="<unix>"></port>`},
|
||||
{Value: &Domain{Name: []byte("google.com&friends")}, ExpectXML: `<domain>google.com&friends</domain>`},
|
||||
{Value: &Book{Title: "Pride & Prejudice"}, ExpectXML: `<book>Pride & Prejudice</book>`},
|
||||
{Value: atomValue, ExpectXML: atomXml},
|
||||
{
|
||||
Value: &Ship{
|
||||
|
Loading…
Reference in New Issue
Block a user