@@ -304,28 +304,7 @@ class HtmlParser extends StatelessWidget {
304304 tree: tree,
305305 style: context.style.copyOnlyInherited (tree.style),
306306 );
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+
329308 if (customRender.containsKey (tree.name)) {
330309 final render = customRender[tree.name]! .call (
331310 newContext,
@@ -354,17 +333,26 @@ class HtmlParser extends StatelessWidget {
354333 }
355334
356335 //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 ) {
358337 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- });
365338 return TextSpan (
366339 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 (),
368356 );
369357 }
370358 return WidgetSpan (
@@ -373,7 +361,22 @@ class HtmlParser extends StatelessWidget {
373361 newContext: newContext,
374362 style: tree.style,
375363 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 (),
377380 ),
378381 );
379382 } else if (tree.style.display == Display .LIST_ITEM ) {
@@ -458,22 +461,17 @@ class HtmlParser extends StatelessWidget {
458461 );
459462 } else {
460463 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+ ),
477475 ),
478476 );
479477 }
@@ -512,21 +510,26 @@ class HtmlParser extends StatelessWidget {
512510 child: StyledText (
513511 textSpan: TextSpan (
514512 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 (),
518514 ),
519515 style: newContext.style,
520- renderContext: context ,
516+ renderContext: newContext ,
521517 ),
522518 ),
523519 );
524520 } else {
525521 ///[tree] is an inline element.
526522 return TextSpan (
527523 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 (),
530533 );
531534 }
532535 }
@@ -937,7 +940,7 @@ class StyledText extends StatelessWidget {
937940 );
938941 }
939942 return SizedBox (
940- width: calculateWidth (style.display, renderContext),
943+ width: consumeExpandedBlock (style.display, renderContext),
941944 child: Text .rich (
942945 textSpan,
943946 style: style.generateTextStyle (),
@@ -950,13 +953,10 @@ class StyledText extends StatelessWidget {
950953 );
951954 }
952955
953- double ? calculateWidth (Display ? display, RenderContext context) {
956+ double ? consumeExpandedBlock (Display ? display, RenderContext context) {
954957 if ((display == Display .BLOCK || display == Display .LIST_ITEM ) && ! renderContext.parser.shrinkWrap) {
955958 return double .infinity;
956959 }
957- if (renderContext.parser.shrinkWrap) {
958- return MediaQuery .of (context.buildContext).size.width;
959- }
960960 return null ;
961961 }
962962}
0 commit comments