@@ -304,28 +304,7 @@ class HtmlParser extends StatelessWidget {
304
304
tree: tree,
305
305
style: context.style.copyOnlyInherited (tree.style),
306
306
);
307
- List <int > lineEndingIndices = [];
308
- tree.children.forEachIndexed ((index, element) {
309
- //we want the element to be a block element, but we don't want to add
310
- //new-lines before/after the html and body
311
- if (element.style.display == Display .BLOCK
312
- && element.element? .localName != "html"
313
- && element.element? .localName != "body"
314
- ) {
315
- //if the parent element is body and the element is first, we don't want
316
- //to add a new-line before
317
- if (index == 0 && element.element? .parent? .localName == "body" ) {
318
- lineEndingIndices.add (index + 1 );
319
- } else {
320
- lineEndingIndices.addAll ([index, index + 1 ]);
321
- }
322
- }
323
- });
324
- //we don't need a new-line at the end
325
- if (lineEndingIndices.isNotEmpty && lineEndingIndices.last == tree.children.length) {
326
- lineEndingIndices.removeLast ();
327
- }
328
- lineEndingIndices = lineEndingIndices.toSet ().toList ();
307
+
329
308
if (customRender.containsKey (tree.name)) {
330
309
final render = customRender[tree.name]! .call (
331
310
newContext,
@@ -354,17 +333,26 @@ class HtmlParser extends StatelessWidget {
354
333
}
355
334
356
335
//Return the correct InlineSpan based on the element type.
357
- if (tree.style.display == Display .BLOCK ) {
336
+ if (tree.style.display == Display .BLOCK && tree.children.isNotEmpty ) {
358
337
if (newContext.parser.selectable) {
359
- final children = tree.children.map ((tree) => parseTree (newContext, tree)).toList ();
360
- //use provided indices to insert new-lines at those locations
361
- //makes sure to account for list size changes with "+ i"
362
- lineEndingIndices.forEachIndexed ((i, element) {
363
- children.insert (element + i, TextSpan (text: "\n " ));
364
- });
365
338
return TextSpan (
366
339
style: newContext.style.generateTextStyle (),
367
- children: children,
340
+ children: tree.children
341
+ .expandIndexed ((i, childTree) => [
342
+ if (shrinkWrap &&
343
+ childTree.style.display == Display .BLOCK &&
344
+ i > 0 &&
345
+ tree.children[i - 1 ] is ReplacedElement )
346
+ TextSpan (text: "\n " ),
347
+ parseTree (newContext, childTree),
348
+ if (shrinkWrap &&
349
+ i != tree.children.length - 1 &&
350
+ childTree.style.display == Display .BLOCK &&
351
+ childTree.element? .localName != "html" &&
352
+ childTree.element? .localName != "body" )
353
+ TextSpan (text: "\n " ),
354
+ ])
355
+ .toList (),
368
356
);
369
357
}
370
358
return WidgetSpan (
@@ -373,7 +361,22 @@ class HtmlParser extends StatelessWidget {
373
361
newContext: newContext,
374
362
style: tree.style,
375
363
shrinkWrap: context.parser.shrinkWrap,
376
- children: tree.children.map ((tree) => parseTree (newContext, tree)).toList (),
364
+ children: tree.children
365
+ .expandIndexed ((i, childTree) => [
366
+ if (shrinkWrap &&
367
+ childTree.style.display == Display .BLOCK &&
368
+ i > 0 &&
369
+ tree.children[i - 1 ] is ReplacedElement )
370
+ TextSpan (text: "\n " ),
371
+ parseTree (newContext, childTree),
372
+ if (shrinkWrap &&
373
+ i != tree.children.length - 1 &&
374
+ childTree.style.display == Display .BLOCK &&
375
+ childTree.element? .localName != "html" &&
376
+ childTree.element? .localName != "body" )
377
+ TextSpan (text: "\n " ),
378
+ ])
379
+ .toList (),
377
380
),
378
381
);
379
382
} else if (tree.style.display == Display .LIST_ITEM ) {
@@ -458,22 +461,17 @@ class HtmlParser extends StatelessWidget {
458
461
);
459
462
} else {
460
463
return WidgetSpan (
461
- child: RawGestureDetector (
462
- key: AnchorKey .of (key, tree),
463
- gestures: {
464
- MultipleTapGestureRecognizer :
465
- GestureRecognizerFactoryWithHandlers <
466
- MultipleTapGestureRecognizer >(
467
- () => MultipleTapGestureRecognizer (),
468
- (instance) {
469
- instance
470
- ..onTap = _onAnchorTap != null
471
- ? () => _onAnchorTap !(tree.href, context, tree.attributes, tree.element)
472
- : null ;
473
- },
474
- ),
475
- },
476
- child: (childSpan as WidgetSpan ).child,
464
+ child: MultipleTapGestureDetector (
465
+ onTap: _onAnchorTap != null
466
+ ? () => _onAnchorTap !(tree.href, context, tree.attributes, tree.element)
467
+ : null ,
468
+ child: GestureDetector (
469
+ key: AnchorKey .of (key, tree),
470
+ onTap: _onAnchorTap != null
471
+ ? () => _onAnchorTap !(tree.href, context, tree.attributes, tree.element)
472
+ : null ,
473
+ child: (childSpan as WidgetSpan ).child,
474
+ ),
477
475
),
478
476
);
479
477
}
@@ -512,21 +510,26 @@ class HtmlParser extends StatelessWidget {
512
510
child: StyledText (
513
511
textSpan: TextSpan (
514
512
style: newContext.style.generateTextStyle (),
515
- children: tree.children
516
- .map ((tree) => parseTree (newContext, tree))
517
- .toList (),
513
+ children: tree.children.map ((tree) => parseTree (newContext, tree)).toList (),
518
514
),
519
515
style: newContext.style,
520
- renderContext: context ,
516
+ renderContext: newContext ,
521
517
),
522
518
),
523
519
);
524
520
} else {
525
521
///[tree] is an inline element.
526
522
return TextSpan (
527
523
style: newContext.style.generateTextStyle (),
528
- children:
529
- tree.children.map ((tree) => parseTree (newContext, tree)).toList (),
524
+ children: tree.children
525
+ .expand ((tree) => [
526
+ parseTree (newContext, tree),
527
+ if (tree.style.display == Display .BLOCK &&
528
+ tree.element? .localName != "html" &&
529
+ tree.element? .localName != "body" )
530
+ TextSpan (text: "\n " ),
531
+ ])
532
+ .toList (),
530
533
);
531
534
}
532
535
}
@@ -937,7 +940,7 @@ class StyledText extends StatelessWidget {
937
940
);
938
941
}
939
942
return SizedBox (
940
- width: calculateWidth (style.display, renderContext),
943
+ width: consumeExpandedBlock (style.display, renderContext),
941
944
child: Text .rich (
942
945
textSpan,
943
946
style: style.generateTextStyle (),
@@ -950,13 +953,10 @@ class StyledText extends StatelessWidget {
950
953
);
951
954
}
952
955
953
- double ? calculateWidth (Display ? display, RenderContext context) {
956
+ double ? consumeExpandedBlock (Display ? display, RenderContext context) {
954
957
if ((display == Display .BLOCK || display == Display .LIST_ITEM ) && ! renderContext.parser.shrinkWrap) {
955
958
return double .infinity;
956
959
}
957
- if (renderContext.parser.shrinkWrap) {
958
- return MediaQuery .of (context.buildContext).size.width;
959
- }
960
960
return null ;
961
961
}
962
962
}
0 commit comments