document: add validation for table row/cell contents

OSX Word is very opinionated about the contents.
This commit is contained in:
Todd 2017-10-03 08:03:24 -05:00
parent b7c59b4c94
commit c887342ed1

View File

@ -394,12 +394,53 @@ func (d *Document) Validate() error {
return errors.New("document not initialized correctly, nil base")
}
for _, v := range []func() error{d.validateTableCells} {
if err := v(); err != nil {
return err
}
}
if err := d.x.Validate(); err != nil {
return err
}
return nil
}
func (d *Document) validateTableCells() error {
for _, elt := range d.x.Body.EG_BlockLevelElts {
for _, c := range elt.EG_ContentBlockContent {
for _, t := range c.Tbl {
for _, rc := range t.EG_ContentRowContent {
for _, row := range rc.Tr {
hasCell := false
for _, ecc := range row.EG_ContentCellContent {
cellHasPara := false
for _, cell := range ecc.Tc {
hasCell = true
for _, cellElt := range cell.EG_BlockLevelElts {
for _, cellCont := range cellElt.EG_ContentBlockContent {
if len(cellCont.P) > 0 {
cellHasPara = true
break
}
}
}
}
if !cellHasPara {
return errors.New("table cell must contain a paragraph")
}
}
// OSX Word requires this and won't open the file otherwise
if !hasCell {
return errors.New("table row must contain a cell")
}
}
}
}
}
}
return nil
}
// AddImage adds an image to the document package, returning a reference that
// can be used to add the image to a run and place it in the document contents.
func (d *Document) AddImage(i common.Image) (common.ImageRef, error) {