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

fix: empty getAtt

This commit is contained in:
源文雨
2023-02-24 12:19:01 +08:00
parent be9f9e9672
commit 1e5dbea998
5 changed files with 86 additions and 52 deletions

View File

@@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"strings"
"github.com/fumiama/docxlib"
)
@@ -150,7 +151,9 @@ func main() {
}
if *unm {
f, err := os.Create("unmarshal_" + *fileLocation)
i := strings.LastIndex(*fileLocation, "/")
name := (*fileLocation)[:i+1] + "unmarshal_" + (*fileLocation)[i+1:]
f, err := os.Create(name)
if err != nil {
panic(err)
}
@@ -174,7 +177,7 @@ func main() {
fmt.Printf("[%d] ", x)
for y, c := range r.TableCells {
if len(c.Paragraphs) > 0 && len(c.Paragraphs[0].Children) > 0 {
fmt.Printf("<%d> %s\t", y, c.Paragraphs[0].Children[0].(*docxlib.Run).Text.Text)
fmt.Printf("<%d> %v\t", y, &c.Paragraphs[0])
} else {
fmt.Print("\t")
}

View File

@@ -64,7 +64,7 @@ func newEmptyA4File() *Docx {
"word/webSettings.xml",
"[Content_Types].xml",
},
buf: bytes.NewBuffer(make([]byte, 0, 1024*1024*4)),
buf: bytes.NewBuffer(make([]byte, 0, 1024*1024)),
}
docx.Document.Body.file = docx
return docx

View File

@@ -128,31 +128,33 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
switch tt.Name.Local {
case "extent":
r.Extent = new(WPExtent)
r.Extent.CX, err = strconv.ParseInt(getAtt(tt.Attr, "cx"), 10, 64)
if err != nil {
return err
}
r.Extent.CY, err = strconv.ParseInt(getAtt(tt.Attr, "cy"), 10, 64)
if err != nil {
return err
for _, v := range tt.Attr {
switch v.Name.Local {
case "cx":
r.Extent.CX, err = strconv.ParseInt(v.Value, 10, 64)
case "cy":
r.Extent.CY, err = strconv.ParseInt(v.Value, 10, 64)
}
if err != nil {
return err
}
}
case "effectExtent":
r.EffectExtent = new(WPEffectExtent)
r.EffectExtent.L, err = strconv.ParseInt(getAtt(tt.Attr, "l"), 10, 64)
if err != nil {
return err
}
r.EffectExtent.T, err = strconv.ParseInt(getAtt(tt.Attr, "t"), 10, 64)
if err != nil {
return err
}
r.EffectExtent.R, err = strconv.ParseInt(getAtt(tt.Attr, "r"), 10, 64)
if err != nil {
return err
}
r.EffectExtent.B, err = strconv.ParseInt(getAtt(tt.Attr, "b"), 10, 64)
if err != nil {
return err
for _, v := range tt.Attr {
switch v.Name.Local {
case "l":
r.EffectExtent.L, err = strconv.ParseInt(v.Value, 10, 64)
case "t":
r.EffectExtent.T, err = strconv.ParseInt(v.Value, 10, 64)
case "r":
r.EffectExtent.R, err = strconv.ParseInt(v.Value, 10, 64)
case "b":
r.EffectExtent.B, err = strconv.ParseInt(v.Value, 10, 64)
}
if err != nil {
return err
}
}
case "docPr":
r.DocPr = new(WPDocPr)
@@ -321,7 +323,11 @@ func (w *WPCNvGraphicFramePr) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
value.NoChangeAspect, err = strconv.Atoi(getAtt(tt.Attr, "noChangeAspect"))
v := getAtt(tt.Attr, "noChangeAspect")
if v == "" {
continue
}
value.NoChangeAspect, err = strconv.Atoi(v)
if err != nil {
return err
}
@@ -550,7 +556,11 @@ func (p *PicCNvPicPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
switch tt.Name.Local {
case "picLocks":
var value APicLocks
value.NoChangeAspect, err = strconv.Atoi(getAtt(tt.Attr, "noChangeAspect"))
v := getAtt(tt.Attr, "noChangeAspect")
if v == "" {
continue
}
value.NoChangeAspect, err = strconv.Atoi(v)
if err != nil {
return err
}
@@ -655,7 +665,11 @@ func (a *ABlip) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
switch tt.Name.Local {
case "alphaModFix":
var value AAlphaModFix
value.Amount, err = strconv.Atoi(getAtt(tt.Attr, "amt"))
v := getAtt(tt.Attr, "amt")
if v == "" {
continue
}
value.Amount, err = strconv.Atoi(v)
if err != nil {
return err
}
@@ -779,22 +793,28 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "off":
a.Off.X, err = strconv.ParseInt(getAtt(tt.Attr, "x"), 10, 64)
if err != nil {
return err
}
a.Off.Y, err = strconv.ParseInt(getAtt(tt.Attr, "y"), 10, 64)
if err != nil {
return err
for _, v := range tt.Attr {
switch v.Name.Local {
case "x":
a.Off.X, err = strconv.ParseInt(v.Value, 10, 64)
case "y":
a.Off.Y, err = strconv.ParseInt(v.Value, 10, 64)
}
if err != nil {
return err
}
}
case "ext":
a.Ext.CX, err = strconv.ParseInt(getAtt(tt.Attr, "cx"), 10, 64)
if err != nil {
return err
}
a.Ext.CY, err = strconv.ParseInt(getAtt(tt.Attr, "cy"), 10, 64)
if err != nil {
return err
for _, v := range tt.Attr {
switch v.Name.Local {
case "cx":
a.Ext.CX, err = strconv.ParseInt(v.Value, 10, 64)
case "cy":
a.Ext.CY, err = strconv.ParseInt(v.Value, 10, 64)
}
if err != nil {
return err
}
}
default:
err = d.Skip() // skip unsupported tags
@@ -976,13 +996,16 @@ func (r *WPAnchor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
switch tt.Name.Local {
case "simplePos":
r.SimplePosXY = new(WPSimplePos)
r.SimplePosXY.X, err = strconv.ParseInt(getAtt(tt.Attr, "x"), 10, 64)
if err != nil {
return err
}
r.SimplePosXY.Y, err = strconv.ParseInt(getAtt(tt.Attr, "y"), 10, 64)
if err != nil {
return err
for _, v := range tt.Attr {
switch v.Name.Local {
case "x":
r.SimplePosXY.X, err = strconv.ParseInt(v.Value, 10, 64)
case "y":
r.SimplePosXY.Y, err = strconv.ParseInt(v.Value, 10, 64)
}
if err != nil {
return err
}
}
case "positionH":
r.PositionH = new(WPPositionH)

View File

@@ -484,14 +484,22 @@ func (r *WTableCellProperties) UnmarshalXML(d *xml.Decoder, start xml.StartEleme
switch tt.Name.Local {
case "tcW":
r.TableCellWidth = new(WTableCellWidth)
r.TableCellWidth.W, err = strconv.ParseInt(getAtt(tt.Attr, "w"), 10, 64)
v := getAtt(tt.Attr, "w")
if v == "" {
continue
}
r.TableCellWidth.W, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return err
}
r.TableCellWidth.Type = getAtt(tt.Attr, "type")
case "gridSpan":
r.GridSpan = new(WGridSpan)
r.GridSpan.Val, err = strconv.Atoi(getAtt(tt.Attr, "val"))
v := getAtt(tt.Attr, "val")
if v == "" {
continue
}
r.GridSpan.Val, err = strconv.Atoi(v)
if err != nil {
return err
}

View File

@@ -43,7 +43,7 @@ func unpack(zipReader *zip.Reader) (docx *Docx, err error) {
// fill remaining files into tmpfslst
docx.tmpfslst = append(docx.tmpfslst, f.Name)
}
docx.buf = bytes.NewBuffer(make([]byte, 0, 1024*1024*4))
docx.buf = bytes.NewBuffer(make([]byte, 0, 1024*1024))
return
}