]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: Reset the parent stack before printing a chardata or comment field...
authorHajime Hoshi <hajimehoshi@gmail.com>
Sat, 9 May 2015 19:22:11 +0000 (04:22 +0900)
committerroger peppe <rogpeppe@gmail.com>
Thu, 4 Jun 2015 07:16:25 +0000 (07:16 +0000)
This CL resets the parent stack when printing a character or comment field struct.
In the case of XML elements, the previous parents stack must be considered. However,
charadata or comment fields can't be printed in other fields so it seems required to reset
the parent stack each time a chardata or comment field is printed.

Fixes #5072

Change-Id: I84f61c9bfce94133cd0c076c11211b9be5b4b1ac
Reviewed-on: https://go-review.googlesource.com/9910
Reviewed-by: Nigel Tao <nigeltao@golang.org>
Reviewed-by: roger peppe <rogpeppe@gmail.com>
src/encoding/xml/marshal.go
src/encoding/xml/marshal_test.go

index d0899c0fa6bd1a0b8e07e7d33600d0ea7b737fa4..63f8e2aa8705c9ec8f4f7b589bbb7cda24540991 100644 (file)
@@ -924,6 +924,9 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
 
                switch finfo.flags & fMode {
                case fCharData:
+                       if err := s.setParents(&noField, reflect.Value{}); err != nil {
+                               return err
+                       }
                        if vf.CanInterface() && vf.Type().Implements(textMarshalerType) {
                                data, err := vf.Interface().(encoding.TextMarshaler).MarshalText()
                                if err != nil {
@@ -967,6 +970,9 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
                        continue
 
                case fComment:
+                       if err := s.setParents(&noField, reflect.Value{}); err != nil {
+                               return err
+                       }
                        k := vf.Kind()
                        if !(k == reflect.String || k == reflect.Slice && vf.Type().Elem().Kind() == reflect.Uint8) {
                                return fmt.Errorf("xml: bad type for comment field of %s", val.Type())
index 5e9718c20c790dd3b2e099a0d0b51cfb7e98d33c..394855782ec13b069006b2e4801c074b2e5a2109 100644 (file)
@@ -340,6 +340,16 @@ type OuterOuterStruct struct {
        OuterStruct
 }
 
+type NestedAndChardata struct {
+       AB       []string `xml:"A>B"`
+       Chardata string   `xml:",chardata"`
+}
+
+type NestedAndComment struct {
+       AB      []string `xml:"A>B"`
+       Comment string   `xml:",comment"`
+}
+
 func ifaceptr(x interface{}) interface{} {
        return &x
 }
@@ -995,6 +1005,14 @@ var marshalTests = []struct {
                ExpectXML: `<outer xmlns="testns" int="10"></outer>`,
                Value:     &OuterOuterStruct{OuterStruct{IntAttr: 10}},
        },
+       {
+               ExpectXML: `<NestedAndChardata><A><B></B><B></B></A>test</NestedAndChardata>`,
+               Value:     &NestedAndChardata{AB: make([]string, 2), Chardata: "test"},
+       },
+       {
+               ExpectXML: `<NestedAndComment><A><B></B><B></B></A><!--test--></NestedAndComment>`,
+               Value:     &NestedAndComment{AB: make([]string, 2), Comment: "test"},
+       },
 }
 
 func TestMarshal(t *testing.T) {