@@ -626,8 +626,10 @@ class HtmlParser extends StatelessWidget {
626626 parentAfterText = parentAfter? .text ?? " " ;
627627 }
628628 /// If the text is the first element in the current tree node list, it
629- /// starts with a whitespace, it isn't a line break, and either the
630- /// whitespace is unnecessary or it is a block element, delete it.
629+ /// starts with a whitespace, it isn't a line break, either the
630+ /// whitespace is unnecessary or it is a block element, and either it is
631+ /// first element in the parent node list or the previous element
632+ /// in the parent node list ends with a whitespace, delete it.
631633 ///
632634 /// We should also delete the whitespace at any point in the node list
633635 /// if the previous element is a <br> because that tag makes the element
@@ -637,6 +639,10 @@ class HtmlParser extends StatelessWidget {
637639 && tree.element? .localName != "br"
638640 && (! keepLeadingSpace.data
639641 || BLOCK_ELEMENTS .contains (tree.element? .localName ?? "" ))
642+ && (elementIndex < 1
643+ || (elementIndex >= 1
644+ && parentNodes? [elementIndex - 1 ] is dom.Text
645+ && parentNodes! [elementIndex - 1 ].text! .endsWith (" " )))
640646 ) {
641647 tree.text = tree.text! .replaceFirst (' ' , '' );
642648 } else if (textIndex >= 1
0 commit comments