diff --git a/structdoc_test.go b/structdoc_test.go index 58c4e0a..519f8ea 100644 --- a/structdoc_test.go +++ b/structdoc_test.go @@ -31,6 +31,9 @@ func TestStructure(t *testing.T) { if child.Link == nil && child.Properties == nil && child.Run == nil { t.Errorf("There are children with all fields nil") } + if child.Run != nil && child.Run.Text == nil { + t.Errorf("We have a run with no text") + } } } } diff --git a/structrun.go b/structrun.go index e2c1bae..e692934 100644 --- a/structrun.go +++ b/structrun.go @@ -1,6 +1,9 @@ package docxlib -import "encoding/xml" +import ( + "encoding/xml" + "io" +) const ( HYPERLINK_STYLE = "a1" @@ -62,3 +65,36 @@ type Size struct { XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main sz"` Val int `xml:"w:val,attr"` } + +func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + var elem Run + for { + t, err := d.Token() + if err == io.EOF { + break + } + + switch tt := t.(type) { + case xml.CharData: + var value string + d.DecodeElement(&value, &start) + elem.Text = &Text{Text: value} + case xml.StartElement: + if tt.Name.Local == "rPr" { + var value RunProperties + d.DecodeElement(&value, &start) + elem.RunProperties = &value + } else if tt.Name.Local == "instrText" { + var value string + d.DecodeElement(&value, &start) + elem.InstrText = value + } else { + continue + } + } + + } + *r = elem + return nil + +}