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

Parse runs

This commit is contained in:
Gonzalo Fernandez-Victorio
2021-05-11 15:25:46 +01:00
parent bb0851ceff
commit af18d96b6b
2 changed files with 40 additions and 1 deletions

View File

@@ -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")
}
}
}
}

View File

@@ -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
}