Skip to content

Commit

Permalink
Fix / improve HTML table appearance
Browse files Browse the repository at this point in the history
Working around issues in flutter_html-3.0.0-beta2... But trying to
revert back to 2.2.1 was also not better, there other features aren't
working (e.g. vertical-align:top).
Now at least all tables are displayed correctly
  • Loading branch information
holybiber committed Oct 27, 2023
1 parent e26a9df commit 9a45efb
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions lib/routes/view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ class MainHtmlView extends StatelessWidget {
style: {
"table": Style(
// set table width, otherwise they're broken
width: Width(MediaQuery.of(context).size.width - 50))
width: Width(MediaQuery.of(context).size.width - 50)),
"td": Style(
padding: const EdgeInsets.fromLTRB(5, 3, 5, 3)
.htmlPadding),
"th": Style(
textAlign: TextAlign.center,
verticalAlign: VerticalAlign.top),
// TODO: reduce left padding/margin of <li> items
// But this doesn't seem to work in flutter_html-3.0.0-beta2
/* "li": Style(
padding: HtmlPaddings.zero, margin: Margins.zero) */
},
onAnchorTap: (url, _, __) {
debugPrint("Link tapped: $url");
Expand All @@ -78,26 +88,49 @@ class MainHtmlView extends StatelessWidget {
}
}

/// FIXME: Bad workaround for bug in flutter_html 3.0.0-beta2:
/// FIXME: workarounds for bugs in flutter_html 3.0.0-beta2:
/// https://github.com/Sub6Resources/flutter_html/issues/1188
/// Remove all <ul> and <p> in table cells.
/// That means some content isn't displayed! But that's still better than
/// a completely unusable app (turns completely white) when clicking
/// on some worksheets like "Training Meeting Outline" and "Hearing from God"
/// Get rid of <ul> and <p> in table cells to avoid completely unusable app
/// and fix some other issues
///
/// Hopefully flutter_html 3.0.0 is soon released and fixes the issue
/// Hopefully flutter_html 3.0.0 is soon released and fixes the issues
sanitize(String html) {
var dom = parse(html);
// debugPrint("Number of p in td: ${dom.querySelectorAll('td p').length}");

// Change <td><p>Content</p></td> to <td>Content</td>
// FIXME: That could actually be fixed in the HTML generated by pywikitools
for (var element in dom.querySelectorAll('td p')) {
debugPrint('Warning: Removing <p> element in <td> as workaround for a bug');
debugPrint('Warning: Found <p> element in <td>, removing...');
element.parent!.innerHtml = element.innerHtml;
element.remove();
}
// Change <th><p>Content</p></th> to <th>Content</th>
// FIXME: That could actually be fixed in the HTML generated by pywikitools
for (var element in dom.querySelectorAll('th p')) {
debugPrint('Warning: Found <p> element in <th>, removing...');
element.parent!.innerHtml = element.innerHtml;
element.remove();
}
// e.g. Hearing_from_God:
// Change <td><ul><li>item1</li><li>item2</li></ul></td> to
// <td>• item1<br/>• item2<br/></td>
// FIXME: Remove once the issue 1188 (see above) is solved
for (var element in dom.querySelectorAll('td ul')) {
debugPrint(
'Warning: Removing <ul> element in <td> as workaround for a bug');
debugPrint('Warning: Found <ul> element in <td>: Working around the bug');
String newHtml = '';
for (var li in element.children) {
assert(li.localName == 'li');
newHtml += '• ${li.innerHtml}<br/>';
}
element.parent!.innerHtml = newHtml;
element.remove();
}
// e.g. "Three different voices" in Hearing_from_God or Time_with_God
// has <th style="width:x%"> which confuses flutter_html, so remove
// the style attribute
for (var element in dom.querySelectorAll('th')) {
element.attributes.remove('style');
}

return dom;
}

0 comments on commit 9a45efb

Please sign in to comment.