1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-12 11:40:28 +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" "flag"
"fmt" "fmt"
"os" "os"
"strings"
"github.com/fumiama/docxlib" "github.com/fumiama/docxlib"
) )
@@ -150,7 +151,9 @@ func main() {
} }
if *unm { 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 { if err != nil {
panic(err) panic(err)
} }
@@ -174,7 +177,7 @@ func main() {
fmt.Printf("[%d] ", x) fmt.Printf("[%d] ", x)
for y, c := range r.TableCells { for y, c := range r.TableCells {
if len(c.Paragraphs) > 0 && len(c.Paragraphs[0].Children) > 0 { 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 { } else {
fmt.Print("\t") fmt.Print("\t")
} }

View File

@@ -64,7 +64,7 @@ func newEmptyA4File() *Docx {
"word/webSettings.xml", "word/webSettings.xml",
"[Content_Types].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 docx.Document.Body.file = docx
return 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 { switch tt.Name.Local {
case "extent": case "extent":
r.Extent = new(WPExtent) r.Extent = new(WPExtent)
r.Extent.CX, err = strconv.ParseInt(getAtt(tt.Attr, "cx"), 10, 64) for _, v := range tt.Attr {
if err != nil { switch v.Name.Local {
return err case "cx":
} r.Extent.CX, err = strconv.ParseInt(v.Value, 10, 64)
r.Extent.CY, err = strconv.ParseInt(getAtt(tt.Attr, "cy"), 10, 64) case "cy":
if err != nil { r.Extent.CY, err = strconv.ParseInt(v.Value, 10, 64)
return err }
if err != nil {
return err
}
} }
case "effectExtent": case "effectExtent":
r.EffectExtent = new(WPEffectExtent) r.EffectExtent = new(WPEffectExtent)
r.EffectExtent.L, err = strconv.ParseInt(getAtt(tt.Attr, "l"), 10, 64) for _, v := range tt.Attr {
if err != nil { switch v.Name.Local {
return err case "l":
} r.EffectExtent.L, err = strconv.ParseInt(v.Value, 10, 64)
r.EffectExtent.T, err = strconv.ParseInt(getAtt(tt.Attr, "t"), 10, 64) case "t":
if err != nil { r.EffectExtent.T, err = strconv.ParseInt(v.Value, 10, 64)
return err case "r":
} r.EffectExtent.R, err = strconv.ParseInt(v.Value, 10, 64)
r.EffectExtent.R, err = strconv.ParseInt(getAtt(tt.Attr, "r"), 10, 64) case "b":
if err != nil { r.EffectExtent.B, err = strconv.ParseInt(v.Value, 10, 64)
return err }
} if err != nil {
r.EffectExtent.B, err = strconv.ParseInt(getAtt(tt.Attr, "b"), 10, 64) return err
if err != nil { }
return err
} }
case "docPr": case "docPr":
r.DocPr = new(WPDocPr) 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") { if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err 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 { if err != nil {
return err return err
} }
@@ -550,7 +556,11 @@ func (p *PicCNvPicPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
switch tt.Name.Local { switch tt.Name.Local {
case "picLocks": case "picLocks":
var value APicLocks 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 { if err != nil {
return err return err
} }
@@ -655,7 +665,11 @@ func (a *ABlip) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
switch tt.Name.Local { switch tt.Name.Local {
case "alphaModFix": case "alphaModFix":
var value AAlphaModFix 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 { if err != nil {
return err 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 { if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local { switch tt.Name.Local {
case "off": case "off":
a.Off.X, err = strconv.ParseInt(getAtt(tt.Attr, "x"), 10, 64) for _, v := range tt.Attr {
if err != nil { switch v.Name.Local {
return err case "x":
} a.Off.X, err = strconv.ParseInt(v.Value, 10, 64)
a.Off.Y, err = strconv.ParseInt(getAtt(tt.Attr, "y"), 10, 64) case "y":
if err != nil { a.Off.Y, err = strconv.ParseInt(v.Value, 10, 64)
return err }
if err != nil {
return err
}
} }
case "ext": case "ext":
a.Ext.CX, err = strconv.ParseInt(getAtt(tt.Attr, "cx"), 10, 64) for _, v := range tt.Attr {
if err != nil { switch v.Name.Local {
return err case "cx":
} a.Ext.CX, err = strconv.ParseInt(v.Value, 10, 64)
a.Ext.CY, err = strconv.ParseInt(getAtt(tt.Attr, "cy"), 10, 64) case "cy":
if err != nil { a.Ext.CY, err = strconv.ParseInt(v.Value, 10, 64)
return err }
if err != nil {
return err
}
} }
default: default:
err = d.Skip() // skip unsupported tags 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 { switch tt.Name.Local {
case "simplePos": case "simplePos":
r.SimplePosXY = new(WPSimplePos) r.SimplePosXY = new(WPSimplePos)
r.SimplePosXY.X, err = strconv.ParseInt(getAtt(tt.Attr, "x"), 10, 64) for _, v := range tt.Attr {
if err != nil { switch v.Name.Local {
return err case "x":
} r.SimplePosXY.X, err = strconv.ParseInt(v.Value, 10, 64)
r.SimplePosXY.Y, err = strconv.ParseInt(getAtt(tt.Attr, "y"), 10, 64) case "y":
if err != nil { r.SimplePosXY.Y, err = strconv.ParseInt(v.Value, 10, 64)
return err }
if err != nil {
return err
}
} }
case "positionH": case "positionH":
r.PositionH = new(WPPositionH) r.PositionH = new(WPPositionH)

View File

@@ -484,14 +484,22 @@ func (r *WTableCellProperties) UnmarshalXML(d *xml.Decoder, start xml.StartEleme
switch tt.Name.Local { switch tt.Name.Local {
case "tcW": case "tcW":
r.TableCellWidth = new(WTableCellWidth) 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 { if err != nil {
return err return err
} }
r.TableCellWidth.Type = getAtt(tt.Attr, "type") r.TableCellWidth.Type = getAtt(tt.Attr, "type")
case "gridSpan": case "gridSpan":
r.GridSpan = new(WGridSpan) 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 { if err != nil {
return err return err
} }

View File

@@ -43,7 +43,7 @@ func unpack(zipReader *zip.Reader) (docx *Docx, err error) {
// fill remaining files into tmpfslst // fill remaining files into tmpfslst
docx.tmpfslst = append(docx.tmpfslst, f.Name) 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 return
} }