Skip to content

Commit

Permalink
Included nested tables in document.Tables() output (unidoc#257)
Browse files Browse the repository at this point in the history
* included nested tables in document.Tables() output

* add test case for getting tables

remove comments

* cleanups
  • Loading branch information
freb authored and gunnsth committed Apr 3, 2019
1 parent 167fc12 commit 32d0f79
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
28 changes: 26 additions & 2 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,30 @@ func (d *Document) insertTable(relativeTo Paragraph, before bool) Table {
return d.AddTable()
}

func (d *Document) tables(bc *wml.EG_ContentBlockContent) []Table {
ret := []Table{}
for _, t := range bc.Tbl {
ret = append(ret, Table{d, t})
for _, crc := range t.EG_ContentRowContent {
for _, tr := range crc.Tr {
for _, ccc := range tr.EG_ContentCellContent {
for _, tc := range ccc.Tc {
for _, ble := range tc.EG_BlockLevelElts {
for _, cbc := range ble.EG_ContentBlockContent {
for _, tbl := range d.tables(cbc) {
ret = append(ret, tbl)
}
}
}
}
}
}
}
}

return ret
}

// Tables returns the tables defined in the document.
func (d *Document) Tables() []Table {
ret := []Table{}
Expand All @@ -343,8 +367,8 @@ func (d *Document) Tables() []Table {
}
for _, ble := range d.x.Body.EG_BlockLevelElts {
for _, c := range ble.EG_ContentBlockContent {
for _, t := range c.Tbl {
ret = append(ret, Table{d, t})
for _, t := range d.tables(c) {
ret = append(ret, t)
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"baliance.com/gooxml/common"
"baliance.com/gooxml/document"
"baliance.com/gooxml/schema/soo/wml"
"baliance.com/gooxml/testhelper"
)

Expand Down Expand Up @@ -323,3 +324,33 @@ func TestIssue198(t *testing.T) {
doc.Save(&got)
testhelper.CompareGoldenZip(t, fn+".golden", got.Bytes())
}

func TestGetTables(t *testing.T) {
doc := document.New()
table := doc.AddTable()
tables := doc.Tables()

if len(tables) != 1 {
t.Errorf("expected 1 table, got %d", len(tables))
return
}

if table != tables[0] {
t.Error("retrieved table != added table")
return
}

tbl := document.New().AddTable().X()

tc := table.AddRow().AddCell().X()
elts := wml.NewEG_BlockLevelElts()
tc.EG_BlockLevelElts = append(tc.EG_BlockLevelElts, elts)
c := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, c)
c.Tbl = append(c.Tbl, tbl)

tables = doc.Tables()
if len(tables) < 2 {
t.Errorf("nested table not enumerated. found %d, expected 2", len(tables))
}
}

0 comments on commit 32d0f79

Please sign in to comment.