diff --git a/structcompatibility.go b/structcompatibility.go new file mode 100644 index 0000000..932fbfe --- /dev/null +++ b/structcompatibility.go @@ -0,0 +1,103 @@ +//go:build ignore + +package docx + +import ( + "encoding/xml" + "io" + "strings" +) + +// AlternateContent ... +type AlternateContent struct { + XMLName xml.Name `xml:"mc:AlternateContent,omitempty"` + + Choice *MCChoice + Fallback struct{} `xml:"mc:Fallback"` + + file *Docx +} + +// UnmarshalXML ... +func (a *AlternateContent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + for { + t, err := d.Token() + if err == io.EOF { + break + } + if err != nil { + return err + } + + if tt, ok := t.(xml.StartElement); ok { + switch tt.Name.Local { + case "Choice": + var value MCChoice + value.file = a.file + err = d.DecodeElement(&value, &tt) + if err != nil && !strings.HasPrefix(err.Error(), "expected") { + return err + } + a.Choice = &value + default: + err = d.Skip() // skip unsupported tags + if err != nil { + return err + } + continue + } + } + } + return nil +} + +// MCChoice ... +type MCChoice struct { + XMLName xml.Name `xml:"mc:Choice,omitempty"` + Requires string `xml:",attr,omitempty"` + + Elems []interface{} + + file *Docx +} + +// UnmarshalXML ... +func (c *MCChoice) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + for _, attr := range start.Attr { + switch attr.Name.Local { + case "Requires": + c.Requires = attr.Value + default: + // ignore other attributes + } + } + for { + t, err := d.Token() + if err == io.EOF { + break + } + if err != nil { + return err + } + + if tt, ok := t.(xml.StartElement); ok { + switch tt.Name.Local { + case "drawing": + var value Drawing + value.file = c.file + err = d.DecodeElement(&value, &tt) + if err != nil && !strings.HasPrefix(err.Error(), "expected") { + return err + } + c.Elems = append(c.Elems, &value) + default: + err = d.Skip() // skip unsupported tags + if err != nil { + return err + } + continue + } + } + } + return nil +} diff --git a/structtable.go b/structtable.go index e364686..3ae1b71 100644 --- a/structtable.go +++ b/structtable.go @@ -776,9 +776,9 @@ func (w *WTableBorders) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err // WTableBorder is a structure representing a single border of a Word table. type WTableBorder struct { Val string `xml:"w:val,attr"` - Size int `xml:"w:sz,attr"` - Space int `xml:"w:space,attr"` - Color string `xml:"w:color,attr"` + Size int `xml:"w:sz,attr,omitempty"` + Space int `xml:"w:space,attr,omitempty"` + Color string `xml:"w:color,attr,omitempty"` } // UnmarshalXML ... @@ -788,12 +788,18 @@ func (t *WTableBorder) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro case "val": t.Val = attr.Value case "sz": + if attr.Value == "" { + continue + } sz, err := strconv.Atoi(attr.Value) if err != nil { return err } t.Size = sz case "space": + if attr.Value == "" { + continue + } space, err := strconv.Atoi(attr.Value) if err != nil { return err