From f3aaac30574bf36b5b39cdb3529e1f1bb159187a Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Thu, 7 Jan 2021 16:26:19 +0100 Subject: [PATCH] Support multiple table colgroups and their span attribute This also fixes a crash you could get when the specs didn't match the data (cf #456) --- example/lib/main.dart | 3 +-- lib/src/layout_element.dart | 53 +++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index fd4c99c3e6..d3467c0cf4 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -63,8 +63,7 @@ const htmlData = """ - - + diff --git a/lib/src/layout_element.dart b/lib/src/layout_element.dart index 5ec89df2df..95287b8e8c 100644 --- a/lib/src/layout_element.dart +++ b/lib/src/layout_element.dart @@ -32,27 +32,35 @@ class TableLayoutElement extends LayoutElement { @override Widget toWidget(RenderContext context) { final rows = []; - List columnSizes; + List columnSizes = []; for (var child in children) { if (child is TableStyleElement) { // Map tags to predetermined column track sizes - columnSizes = child.children.where((c) => c.name == "col").map((c) { - final colWidth = c.attributes["width"]; - if (colWidth != null && colWidth.endsWith("%")) { - final percentageSize = - double.tryParse(colWidth.substring(0, colWidth.length - 1)); - return percentageSize != null - ? FlexibleTrackSize(percentageSize * 0.01) - : FlexibleTrackSize(1); - } else if (colWidth != null) { - final fixedPxSize = double.tryParse(colWidth); - return fixedPxSize != null - ? FixedTrackSize(fixedPxSize) - : FlexibleTrackSize(1); - } else { - return FlexibleTrackSize(1); - } - }).toList(growable: false); + columnSizes = child.children + .where((c) => c.name == "col") + .map((c) { + final span = + int.parse(c.attributes["span"] ?? "1", onError: (_) => 1); + final colWidth = c.attributes["width"]; + return List.generate(span, (index) { + if (colWidth != null && colWidth.endsWith("%")) { + final percentageSize = double.tryParse( + colWidth.substring(0, colWidth.length - 1)); + return percentageSize != null + ? FlexibleTrackSize(percentageSize * 0.01) + : FlexibleTrackSize(1); + } else if (colWidth != null) { + final fixedPxSize = double.tryParse(colWidth); + return fixedPxSize != null + ? FixedTrackSize(fixedPxSize) + : FlexibleTrackSize(1); + } else { + return FlexibleTrackSize(1); + } + }); + }) + .expand((element) => element) + .toList(growable: false); } else if (child is TableSectionLayoutElement) { rows.addAll(child.children.whereType()); } else if (child is TableRowLayoutElement) { @@ -71,6 +79,7 @@ class TableLayoutElement extends LayoutElement { .fold(0, (int value, child) => value + child.colspan)) .fold(0, max); + // Place the cells in the rows/columns final cells = []; final columnRowOffset = List.generate(columnMax + 1, (_) => 0); int rowi = 0; @@ -113,8 +122,12 @@ class TableLayoutElement extends LayoutElement { rowi++; } - final finalColumnSizes = - columnSizes ?? List.generate(columnMax, (_) => FlexibleTrackSize(1)); + // Create column tracks (insofar there were no colgroups that already defined them) + List finalColumnSizes = (columnSizes ?? []).take( + columnMax).toList(); + finalColumnSizes += List.generate( + max(0, columnMax - finalColumnSizes.length), + (_) => FlexibleTrackSize(1)); return Container( decoration: BoxDecoration( color: style.backgroundColor,
OneTwoThree