@@ -296,30 +296,33 @@ class HtmlRichTextParser extends StatelessWidget {
296
296
// a text only node is a child of a tag with no inner html
297
297
if (node is dom.Text ) {
298
298
// WHITESPACE CONSIDERATIONS ---
299
- // truly empty nodes, should just be ignored
299
+ // truly empty nodes should just be ignored
300
300
if (node.text.trim () == "" && node.text.indexOf (" " ) == - 1 ) {
301
301
return ;
302
302
}
303
303
304
- // if (node.text.trim() == "" &&
305
- // node.text.indexOf(" ") != -1 &&
306
- // parseContext.condenseWhitespace) {
307
- // node.text = " ";
308
- // }
309
-
310
304
// we might want to preserve internal whitespace
311
305
// empty strings of whitespace might be significant or not, condense it by default
312
306
String finalText = parseContext.condenseWhitespace
313
307
? condenseHtmlWhitespace (node.text)
314
308
: node.text;
315
309
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
+ }
320
323
321
324
// if the finalText is actually empty, just return
322
- if (finalText.isEmpty) return ;
325
+ if (finalText.trim (). isEmpty) return ;
323
326
324
327
// NOW WE HAVE OUR TRULY FINAL TEXT
325
328
// debugPrint("Plain Text Node: '$finalText'");
@@ -372,7 +375,7 @@ class HtmlRichTextParser extends StatelessWidget {
372
375
.add (BlockText (child: RichText (text: span)));
373
376
}
374
377
375
- // this allows future items to be added as children
378
+ // this allows future items to be added as children of this item
376
379
parseContext.parentElement = span;
377
380
378
381
// if the parent is a LinkTextSpan, keep the main attributes of that span going.
@@ -387,8 +390,10 @@ class HtmlRichTextParser extends StatelessWidget {
387
390
));
388
391
389
392
// if the parent is a normal span, just add this to that list
390
- } else {
393
+ } else if ( ! (parseContext.parentElement.children is List < Widget >)) {
391
394
parseContext.parentElement.children.add (span);
395
+ } else {
396
+ print ('doing nothing' );
392
397
}
393
398
return ;
394
399
}
@@ -521,33 +526,54 @@ class HtmlRichTextParser extends StatelessWidget {
521
526
break ;
522
527
523
528
case "table" :
524
- case "tbody" :
525
- case "thead" :
526
529
// new block, so clear out the parent element
527
530
parseContext.parentElement = null ;
528
531
nextContext.parentElement = Column (
529
532
crossAxisAlignment: CrossAxisAlignment .start,
533
+ children: < Widget > [],
530
534
);
531
535
nextContext.rootWidgetList.add (nextContext.parentElement);
532
536
break ;
533
537
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
+
534
557
case "td" :
535
558
case "th" :
536
559
int colspan = 1 ;
537
560
if (node.attributes['colspan' ] != null ) {
538
561
colspan = int .tryParse (node.attributes['colspan' ]);
539
562
}
563
+ RichText text =
564
+ RichText (text: TextSpan (text: '' , children: < TextSpan > []));
540
565
Expanded cell = Expanded (
541
566
flex: colspan,
542
- child: Wrap () ,
567
+ child: text ,
543
568
);
544
569
nextContext.parentElement.children.add (cell);
545
- nextContext.parentElement = cell.child ;
570
+ nextContext.parentElement = text.text ;
546
571
break ;
547
572
548
573
case "tr" :
549
574
Row row = Row (
550
575
crossAxisAlignment: CrossAxisAlignment .center,
576
+ children: < Widget > [],
551
577
);
552
578
nextContext.parentElement.children.add (row);
553
579
nextContext.parentElement = row;
0 commit comments