Skip to content

Commit 319b0df

Browse files
authored
Merge pull request Sub6Resources#484 from vrtdev/bugfix/increase-table-col-calc
Support multiple table colgroups and their span attribute
2 parents 0226739 + f3aaac3 commit 319b0df

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

example/lib/main.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ const htmlData = """
6363
<table>
6464
<colgroup>
6565
<col width="50%" />
66-
<col width="25%" />
67-
<col width="25%" />
66+
<col span="2" width="25%" />
6867
</colgroup>
6968
<thead>
7069
<tr><th>One</th><th>Two</th><th>Three</th></tr>

lib/src/layout_element.dart

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,35 @@ class TableLayoutElement extends LayoutElement {
3232
@override
3333
Widget toWidget(RenderContext context) {
3434
final rows = <TableRowLayoutElement>[];
35-
List<TrackSize> columnSizes;
35+
List<TrackSize> columnSizes = <TrackSize>[];
3636
for (var child in children) {
3737
if (child is TableStyleElement) {
3838
// Map <col> tags to predetermined column track sizes
39-
columnSizes = child.children.where((c) => c.name == "col").map((c) {
40-
final colWidth = c.attributes["width"];
41-
if (colWidth != null && colWidth.endsWith("%")) {
42-
final percentageSize =
43-
double.tryParse(colWidth.substring(0, colWidth.length - 1));
44-
return percentageSize != null
45-
? FlexibleTrackSize(percentageSize * 0.01)
46-
: FlexibleTrackSize(1);
47-
} else if (colWidth != null) {
48-
final fixedPxSize = double.tryParse(colWidth);
49-
return fixedPxSize != null
50-
? FixedTrackSize(fixedPxSize)
51-
: FlexibleTrackSize(1);
52-
} else {
53-
return FlexibleTrackSize(1);
54-
}
55-
}).toList(growable: false);
39+
columnSizes = child.children
40+
.where((c) => c.name == "col")
41+
.map((c) {
42+
final span =
43+
int.parse(c.attributes["span"] ?? "1", onError: (_) => 1);
44+
final colWidth = c.attributes["width"];
45+
return List.generate(span, (index) {
46+
if (colWidth != null && colWidth.endsWith("%")) {
47+
final percentageSize = double.tryParse(
48+
colWidth.substring(0, colWidth.length - 1));
49+
return percentageSize != null
50+
? FlexibleTrackSize(percentageSize * 0.01)
51+
: FlexibleTrackSize(1);
52+
} else if (colWidth != null) {
53+
final fixedPxSize = double.tryParse(colWidth);
54+
return fixedPxSize != null
55+
? FixedTrackSize(fixedPxSize)
56+
: FlexibleTrackSize(1);
57+
} else {
58+
return FlexibleTrackSize(1);
59+
}
60+
});
61+
})
62+
.expand((element) => element)
63+
.toList(growable: false);
5664
} else if (child is TableSectionLayoutElement) {
5765
rows.addAll(child.children.whereType());
5866
} else if (child is TableRowLayoutElement) {
@@ -71,6 +79,7 @@ class TableLayoutElement extends LayoutElement {
7179
.fold(0, (int value, child) => value + child.colspan))
7280
.fold(0, max);
7381

82+
// Place the cells in the rows/columns
7483
final cells = <GridPlacement>[];
7584
final columnRowOffset = List.generate(columnMax + 1, (_) => 0);
7685
int rowi = 0;
@@ -113,8 +122,12 @@ class TableLayoutElement extends LayoutElement {
113122
rowi++;
114123
}
115124

116-
final finalColumnSizes =
117-
columnSizes ?? List.generate(columnMax, (_) => FlexibleTrackSize(1));
125+
// Create column tracks (insofar there were no colgroups that already defined them)
126+
List<TrackSize> finalColumnSizes = (columnSizes ?? <TrackSize>[]).take(
127+
columnMax).toList();
128+
finalColumnSizes += List.generate(
129+
max(0, columnMax - finalColumnSizes.length),
130+
(_) => FlexibleTrackSize(1));
118131
return Container(
119132
decoration: BoxDecoration(
120133
color: style.backgroundColor,

0 commit comments

Comments
 (0)