1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-05 07:40:24 +08:00

fix: recursive paragraph unmarshalling

This commit is contained in:
源文雨
2023-02-15 17:23:01 +08:00
parent 2771d05a51
commit 517198ded4
4 changed files with 85 additions and 8 deletions

View File

@@ -6,7 +6,7 @@ import (
)
func newEmptyFile() *Docx {
return &Docx{
docx := &Docx{
Document: Document{
XMLName: xml.Name{
Space: "w",
@@ -45,4 +45,6 @@ func newEmptyFile() *Docx {
rId: 3,
buf: bytes.NewBuffer(make([]byte, 0, 1024*1024*4)),
}
docx.Document.file = docx
return docx
}

View File

@@ -33,6 +33,8 @@ type Document struct {
XMLWP string `xml:"xmlns:wp,attr,omitempty"` // cannot be unmarshalled in
XMLWP14 string `xml:"xmlns:wp14,attr,omitempty"` // cannot be unmarshalled in
Body *Body
file *Docx
}
func (doc *Document) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
@@ -50,11 +52,16 @@ func (doc *Document) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
switch tt := t.(type) {
case xml.StartElement:
switch tt.Name.Local {
case "body":
case "p":
var value Paragraph
d.DecodeElement(&value, &start)
doc.Body.Paragraphs = append(doc.Body.Paragraphs, &value)
if len(value.Children) > 0 {
value.file = doc.file
doc.Body.Paragraphs = append(doc.Body.Paragraphs, &value)
}
default:
d.Skip()
continue
}
}

File diff suppressed because one or more lines are too long

View File

@@ -53,6 +53,7 @@ type WPInline struct {
Extent *WPExtent
EffectExtent *WPEffectExtent
DocPr *WPDocPr
Graphic *AGraphic
}
func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
@@ -66,18 +67,25 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
case xml.StartElement:
switch tt.Name.Local {
case "extent":
r.Extent = new(WPExtent)
r.Extent.CX = getAtt(tt.Attr, "cx")
r.Extent.CY = getAtt(tt.Attr, "cy")
case "effectExtent":
r.EffectExtent = new(WPEffectExtent)
r.EffectExtent.L = getAtt(tt.Attr, "l")
r.EffectExtent.T = getAtt(tt.Attr, "t")
r.EffectExtent.R = getAtt(tt.Attr, "r")
r.EffectExtent.B = getAtt(tt.Attr, "b")
case "docPr":
r.DocPr = new(WPDocPr)
r.DocPr.ID = getAtt(tt.Attr, "id")
r.DocPr.Name = getAtt(tt.Attr, "name")
r.DocPr.Macro = getAtt(tt.Attr, "macro")
r.DocPr.Hidden = getAtt(tt.Attr, "hidden")
case "graphic":
var value AGraphic
d.DecodeElement(&value, &start)
r.Graphic = &value
default:
continue
}
@@ -119,8 +127,64 @@ type AGraphic struct {
GraphicData *AGraphicData
}
func (a *AGraphic) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
switch tt := t.(type) {
case xml.StartElement:
switch tt.Name.Local {
case "graphicData":
var value AGraphicData
d.DecodeElement(&value, &start)
value.URI = getAtt(tt.Attr, "uri")
a.GraphicData = &value
default:
continue
}
}
}
return nil
}
// AGraphicData represents the data of a graphic in a Word document.
type AGraphicData struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/main graphicData,omitempty"`
URI string `xml:"uri,attr"`
Pic PICPic
}
// PICPic represents a picture in a Word document.
type PICPic struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/picture pic,omitempty"`
NonVisualPicProperties PICNonVisualPicProperties
BlipFill PICBlipFill
}
// PICNonVisualPicProperties represents the non-visual properties of a picture in a Word document.
type PICNonVisualPicProperties struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/picture nvPicPr,omitempty"`
NonVisualDrawingProperties PICNonVisualDrawingProperties
}
// PICNonVisualDrawingProperties represents the non-visual drawing properties of a picture in a Word document.
type PICNonVisualDrawingProperties struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/picture cNvPr,omitempty"`
ID string `xml:"id,attr"`
}
// PICBlipFill represents the blip fill of a picture in a Word document.
type PICBlipFill struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/picture blipFill,omitempty"`
Blip ABlip
}
// ABlip represents the blip of a picture in a Word document.
type ABlip struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/main blip,omitempty"`
Embed string `xml:"r:embed,attr"`
}