-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmltable.go
More file actions
358 lines (330 loc) · 9.49 KB
/
htmltable.go
File metadata and controls
358 lines (330 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package htmlbag
import (
"strconv"
"strings"
"github.com/boxesandglue/boxesandglue/backend/bag"
"github.com/boxesandglue/boxesandglue/backend/color"
"github.com/boxesandglue/boxesandglue/backend/document"
"github.com/boxesandglue/boxesandglue/backend/node"
"github.com/boxesandglue/boxesandglue/frontend"
)
// parseColumnWidth parses a column width specification and returns a Glue node.
// Supports:
// - fixed widths: "3cm", "50mm", "2in", "100pt"
// - flexible widths: "*" (1 share), "2*" (2 shares), "3*" (3 shares)
func parseColumnWidth(width string) *node.Glue {
g := node.NewGlue()
width = strings.TrimSpace(width)
if width == "" {
// No width specified - auto
g.Stretch = bag.Factor
g.StretchOrder = 1
return g
}
if strings.HasSuffix(width, "*") {
// Flexible width: "*", "2*", "3*", etc.
multiplier := 1.0
prefix := strings.TrimSuffix(width, "*")
if prefix != "" {
if m, err := strconv.ParseFloat(prefix, 64); err == nil {
multiplier = m
}
}
g.Stretch = bag.ScaledPoint(multiplier * float64(bag.Factor))
g.StretchOrder = 1
return g
}
// Fixed width
if sp, err := bag.SP(width); err == nil {
g.Width = sp
}
return g
}
func (cb *CSSBuilder) buildTable(te *frontend.Text, wd bag.ScaledPoint) (*node.VList, error) {
tbl := &frontend.Table{}
tbl.MaxWidth = wd
if sWd, ok := te.Settings[frontend.SettingWidth]; ok {
if wdStr, ok := sWd.(string); ok && strings.HasSuffix(wdStr, "%") {
tbl.MaxWidth = ParseRelativeSize(wdStr, wd, wd)
tbl.Stretch = true
}
}
// Process colgroup for column specifications
for _, itm := range te.Items {
switch t := itm.(type) {
case *frontend.Text:
elt, ok := t.Settings[frontend.SettingDebug].(string)
if !ok {
continue
}
if elt == "colgroup" {
cb.buildColgroup(t, tbl)
}
}
}
// First pass: process thead (header rows come first)
for _, itm := range te.Items {
switch t := itm.(type) {
case *frontend.Text:
elt, ok := t.Settings[frontend.SettingDebug].(string)
if !ok {
continue
}
if elt == "thead" {
rowsBefore := len(tbl.Rows)
cb.buildTBody(t, tbl)
tbl.HeaderRows = len(tbl.Rows) - rowsBefore
}
}
}
// Second pass: process tbody (body rows come after header)
for _, itm := range te.Items {
switch t := itm.(type) {
case *frontend.Text:
elt, ok := t.Settings[frontend.SettingDebug].(string)
if !ok {
continue
}
if elt == "tbody" {
cb.buildTBody(t, tbl)
}
}
}
vls, err := cb.frontend.BuildTable(tbl)
if err != nil {
return nil, err
}
vl := vls[0]
// PDF/UA: tag the table structure.
// Repeated headers on continuation pages are left untagged
// (the backend will wrap them as artifacts in PDF/UA mode).
if cb.enableTagging {
cb.tagTable(vl, tbl)
}
return vl, nil
}
func (cb *CSSBuilder) buildColgroup(te *frontend.Text, tbl *frontend.Table) {
for _, itm := range te.Items {
switch t := itm.(type) {
case *frontend.Text:
elt, ok := t.Settings[frontend.SettingDebug].(string)
if !ok {
continue
}
if elt == "col" {
width := ""
if w, ok := t.Settings[frontend.SettingColumnWidth].(string); ok {
width = w
}
colSpec := frontend.ColSpec{
ColumnWidth: parseColumnWidth(width),
}
tbl.ColSpec = append(tbl.ColSpec, colSpec)
}
}
}
}
func (cb *CSSBuilder) buildTBody(te *frontend.Text, tbl *frontend.Table) {
for _, itm := range te.Items {
switch t := itm.(type) {
case *frontend.Text:
elt, ok := t.Settings[frontend.SettingDebug].(string)
if !ok {
continue
}
if elt == "tr" {
cb.buildTR(t, tbl)
}
}
}
}
func (cb *CSSBuilder) buildTR(te *frontend.Text, tbl *frontend.Table) {
tr := &frontend.TableRow{}
for _, itm := range te.Items {
switch t := itm.(type) {
case *frontend.Text:
elt, ok := t.Settings[frontend.SettingDebug].(string)
if !ok {
continue
}
if elt == "td" || elt == "th" {
cb.buildTD(t, tr, elt == "th")
}
}
}
tbl.Rows = append(tbl.Rows, tr)
}
func (cb *CSSBuilder) buildTD(te *frontend.Text, row *frontend.TableRow, isHeader bool) {
td := &frontend.TableCell{}
td.IsHeader = isHeader
// Extract colspan and rowspan
settings := te.Settings
if v, ok := settings[frontend.SettingColspan]; ok && v != nil {
if colspan, ok := v.(int); ok && colspan > 1 {
td.ExtraColspan = colspan - 1
}
}
if v, ok := settings[frontend.SettingRowspan]; ok && v != nil {
if rowspan, ok := v.(int); ok && rowspan > 1 {
td.ExtraRowspan = rowspan - 1
}
}
// Extract border settings from CSS
if v, ok := settings[frontend.SettingBorderTopWidth]; ok && v != nil {
td.BorderTopWidth = v.(bag.ScaledPoint)
}
if v, ok := settings[frontend.SettingBorderBottomWidth]; ok && v != nil {
td.BorderBottomWidth = v.(bag.ScaledPoint)
}
if v, ok := settings[frontend.SettingBorderLeftWidth]; ok && v != nil {
td.BorderLeftWidth = v.(bag.ScaledPoint)
}
if v, ok := settings[frontend.SettingBorderRightWidth]; ok && v != nil {
td.BorderRightWidth = v.(bag.ScaledPoint)
}
if v, ok := settings[frontend.SettingBorderTopColor]; ok && v != nil {
td.BorderTopColor = v.(*color.Color)
}
if v, ok := settings[frontend.SettingBorderBottomColor]; ok && v != nil {
td.BorderBottomColor = v.(*color.Color)
}
if v, ok := settings[frontend.SettingBorderLeftColor]; ok && v != nil {
td.BorderLeftColor = v.(*color.Color)
}
if v, ok := settings[frontend.SettingBorderRightColor]; ok && v != nil {
td.BorderRightColor = v.(*color.Color)
}
// Extract padding settings
if v, ok := settings[frontend.SettingPaddingTop]; ok && v != nil {
td.PaddingTop = v.(bag.ScaledPoint)
}
if v, ok := settings[frontend.SettingPaddingBottom]; ok && v != nil {
td.PaddingBottom = v.(bag.ScaledPoint)
}
if v, ok := settings[frontend.SettingPaddingLeft]; ok && v != nil {
td.PaddingLeft = v.(bag.ScaledPoint)
}
if v, ok := settings[frontend.SettingPaddingRight]; ok && v != nil {
td.PaddingRight = v.(bag.ScaledPoint)
}
// Extract background color
if v, ok := settings[frontend.SettingBackgroundColor]; ok && v != nil {
td.BackgroundColor = v.(*color.Color)
}
// If this cell references a pre-rendered VList, use it directly as content.
if vlid, ok := settings[frontend.SettingPrerenderedVListID].(string); ok {
if vl, vlOK := cb.PendingVLists[vlid]; vlOK {
td.Contents = append(td.Contents, frontend.FormatToVList(func(wd bag.ScaledPoint) (*node.VList, error) {
return vl, nil
}))
}
}
for _, itm := range te.Items {
switch t := itm.(type) {
case *frontend.Text:
// For box elements (ul, ol, div, etc.), create a FormatToVList function
// that uses CreateVlist - this ensures the same code path as outside tables
if isBox, ok := t.Settings[frontend.SettingBox]; ok && isBox.(bool) {
textCopy := t
ftv := func(wd bag.ScaledPoint) (*node.VList, error) {
vl, err := cb.CreateVlist(textCopy, wd)
if err != nil {
return nil, err
}
// Margin-bottom may have been propagated from a child
// through a borderless parent (CSS margin collapsing).
// In a table cell, materialize it as a kern.
if mb, ok := textCopy.Settings[frontend.SettingMarginBottom]; ok {
if mbSP, ok := mb.(bag.ScaledPoint); ok && mbSP > 0 {
k := node.NewKern()
k.Kern = mbSP
k.Attributes = node.H{"origin": "margin-bottom"}
vl.List = node.InsertAfter(vl.List, node.Tail(vl.List), k)
vl.Height += mbSP
}
}
return vl, nil
}
td.Contents = append(td.Contents, frontend.FormatToVList(ftv))
} else {
td.Contents = append(td.Contents, itm)
}
default:
td.Contents = append(td.Contents, itm)
}
}
row.Cells = append(row.Cells, td)
}
// tagTable walks the table VList and creates Table/TR/TH/TD structure elements.
func (cb *CSSBuilder) tagTable(tableVL *node.VList, tbl *frontend.Table) {
tableSE := &document.StructureElement{Role: "Table"}
cb.structureCurrent.AddChild(tableSE)
// Create THead/TBody grouping SEs
var theadSE, tbodySE *document.StructureElement
if tbl.HeaderRows > 0 {
theadSE = &document.StructureElement{Role: "THead"}
tableSE.AddChild(theadSE)
}
tbodySE = &document.StructureElement{Role: "TBody"}
tableSE.AddChild(tbodySE)
// Walk rows: each child of the table VList is an HList (row)
rowIdx := 0
for cur := tableVL.List; cur != nil; cur = cur.Next() {
rowHL, ok := cur.(*node.HList)
if !ok {
continue
}
if rowIdx >= len(tbl.Rows) {
break
}
// Determine parent: THead for header rows, TBody otherwise
rowParent := tbodySE
if theadSE != nil && rowIdx < tbl.HeaderRows {
rowParent = theadSE
}
trSE := &document.StructureElement{Role: "TR"}
rowParent.AddChild(trSE)
// Walk cells in this row
row := tbl.Rows[rowIdx]
cellIdx := 0
for cellCur := rowHL.List; cellCur != nil; cellCur = cellCur.Next() {
cellVL, ok := cellCur.(*node.VList)
if !ok {
continue
}
if cellIdx >= len(row.Cells) {
break
}
cell := row.Cells[cellIdx]
role := "TD"
if cell.IsHeader {
role = "TH"
}
cellSE := &document.StructureElement{Role: role}
// Set Scope for TH cells
if cell.IsHeader {
if rowIdx < tbl.HeaderRows {
cellSE.Scope = "Column"
} else {
cellSE.Scope = "Row"
}
}
cellSE.ActualText = extractCellText(cell)
trSE.AddChild(cellSE)
tagVList(cellVL, cellSE)
cellIdx++
}
rowIdx++
}
}
// extractCellText extracts text content from a table cell's contents.
func extractCellText(cell *frontend.TableCell) string {
var b strings.Builder
for _, cc := range cell.Contents {
switch t := cc.(type) {
case *frontend.Text:
b.WriteString(extractTextContent(t))
}
}
return b.String()
}