1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-04 23:30:25 +08:00

fix: Paragraph.MarshalXML

This commit is contained in:
源文雨
2023-02-09 14:50:59 +08:00
parent c4142b1c82
commit f995e72acd
3 changed files with 23 additions and 3 deletions

View File

@@ -59,6 +59,7 @@ func main() {
panic(err)
}
for _, para := range doc.Document.Body.Paragraphs {
fmt.Println("New paragraph")
for _, child := range para.Children {
if child.Run != nil {
if child.Run.Text != nil {
@@ -80,6 +81,7 @@ func main() {
}
}
fmt.Print("End of paragraph\n\n")
}
fmt.Println("End of main")
}

View File

@@ -571,6 +571,8 @@ func TestMarshalDrawingStructure(t *testing.T) {
para2 := w.AddParagraph()
para2.AddText("test font size and color").Size(22).Color("ff0000")
para2.AddText("test font size and color").Size(22).Color("ff0000")
para2.AddText("test font size and color").Size(22).Color("ff0000")
nextPara := w.AddParagraph()
nextPara.AddLink("google", `http://google.com`)

View File

@@ -19,10 +19,26 @@ type Paragraph struct {
}
func (p *Paragraph) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
for _, c := range p.Children {
e.EncodeElement(c, start)
err := e.EncodeToken(start)
if err != nil {
return err
}
return nil
for _, c := range p.Children {
switch {
case c.Link != nil:
err = e.Encode(c.Link)
case c.Run != nil:
err = e.Encode(c.Run)
case c.Properties != nil:
err = e.Encode(c.Properties)
default:
continue
}
if err != nil {
return err
}
}
return e.EncodeToken(start.End())
}
func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {