Skip to content

Commit 1ead9ca

Browse files
committed
fixed table rendering
1 parent dc92077 commit 1ead9ca

File tree

2 files changed

+49
-233
lines changed

2 files changed

+49
-233
lines changed

lib/html_parser.dart

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -296,30 +296,33 @@ class HtmlRichTextParser extends StatelessWidget {
296296
// a text only node is a child of a tag with no inner html
297297
if (node is dom.Text) {
298298
// WHITESPACE CONSIDERATIONS ---
299-
// truly empty nodes, should just be ignored
299+
// truly empty nodes should just be ignored
300300
if (node.text.trim() == "" && node.text.indexOf(" ") == -1) {
301301
return;
302302
}
303303

304-
// if (node.text.trim() == "" &&
305-
// node.text.indexOf(" ") != -1 &&
306-
// parseContext.condenseWhitespace) {
307-
// node.text = " ";
308-
// }
309-
310304
// we might want to preserve internal whitespace
311305
// empty strings of whitespace might be significant or not, condense it by default
312306
String finalText = parseContext.condenseWhitespace
313307
? condenseHtmlWhitespace(node.text)
314308
: node.text;
315309

316-
// if this is part of a string of spans, we will preserve leading and trailing whitespace
317-
if (!(parseContext.parentElement is TextSpan ||
318-
parseContext.parentElement is LinkTextSpan))
319-
finalText = finalText.trim();
310+
// if this is part of a string of spans, we will preserve leading
311+
// and trailing whitespace unless the previous character is whitespace
312+
if (parseContext.parentElement == null)
313+
finalText = finalText.trimLeft();
314+
else if (parseContext.parentElement is TextSpan ||
315+
parseContext.parentElement is LinkTextSpan) {
316+
String lastString = parseContext.parentElement.text ?? '';
317+
if (!parseContext.parentElement.children.isEmpty) {
318+
lastString = parseContext.parentElement.children.last.text;
319+
}
320+
if (lastString.endsWith(' ') || lastString.endsWith('\n'))
321+
finalText = finalText.trimLeft();
322+
}
320323

321324
// if the finalText is actually empty, just return
322-
if (finalText.isEmpty) return;
325+
if (finalText.trim().isEmpty) return;
323326

324327
// NOW WE HAVE OUR TRULY FINAL TEXT
325328
// debugPrint("Plain Text Node: '$finalText'");
@@ -372,7 +375,7 @@ class HtmlRichTextParser extends StatelessWidget {
372375
.add(BlockText(child: RichText(text: span)));
373376
}
374377

375-
// this allows future items to be added as children
378+
// this allows future items to be added as children of this item
376379
parseContext.parentElement = span;
377380

378381
// if the parent is a LinkTextSpan, keep the main attributes of that span going.
@@ -387,8 +390,10 @@ class HtmlRichTextParser extends StatelessWidget {
387390
));
388391

389392
// if the parent is a normal span, just add this to that list
390-
} else {
393+
} else if (!(parseContext.parentElement.children is List<Widget>)) {
391394
parseContext.parentElement.children.add(span);
395+
} else {
396+
print('doing nothing');
392397
}
393398
return;
394399
}
@@ -521,33 +526,54 @@ class HtmlRichTextParser extends StatelessWidget {
521526
break;
522527

523528
case "table":
524-
case "tbody":
525-
case "thead":
526529
// new block, so clear out the parent element
527530
parseContext.parentElement = null;
528531
nextContext.parentElement = Column(
529532
crossAxisAlignment: CrossAxisAlignment.start,
533+
children: <Widget>[],
530534
);
531535
nextContext.rootWidgetList.add(nextContext.parentElement);
532536
break;
533537

538+
// we don't handle tbody or thead elements separately for now
539+
case "tbody":
540+
case "thead":
541+
break;
542+
543+
// caption elements throw us off
544+
case "caption":
545+
RichText text =
546+
RichText(text: TextSpan(text: '', children: <TextSpan>[]));
547+
Row row = Row(
548+
crossAxisAlignment: CrossAxisAlignment.center,
549+
children: <Widget>[
550+
text,
551+
],
552+
);
553+
nextContext.parentElement.children.add(row);
554+
nextContext.parentElement = text.text;
555+
break;
556+
534557
case "td":
535558
case "th":
536559
int colspan = 1;
537560
if (node.attributes['colspan'] != null) {
538561
colspan = int.tryParse(node.attributes['colspan']);
539562
}
563+
RichText text =
564+
RichText(text: TextSpan(text: '', children: <TextSpan>[]));
540565
Expanded cell = Expanded(
541566
flex: colspan,
542-
child: Wrap(),
567+
child: text,
543568
);
544569
nextContext.parentElement.children.add(cell);
545-
nextContext.parentElement = cell.child;
570+
nextContext.parentElement = text.text;
546571
break;
547572

548573
case "tr":
549574
Row row = Row(
550575
crossAxisAlignment: CrossAxisAlignment.center,
576+
children: <Widget>[],
551577
);
552578
nextContext.parentElement.children.add(row);
553579
nextContext.parentElement = row;

0 commit comments

Comments
 (0)