Skip to content

Commit 5fc8ca0

Browse files
authored
Merge pull request Sub6Resources#739 from tneotia/bugfix/leading-whitespaces-2
Fix leading whitespaces edge case
2 parents e7e69a2 + a15230d commit 5fc8ca0

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/html_parser.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,10 @@ class HtmlParser extends StatelessWidget {
625625
parentAfterText = parentAfter?.text ?? " ";
626626
}
627627
/// If the text is the first element in the current tree node list, it
628-
/// starts with a whitespace, it isn't a line break, and either the
629-
/// whitespace is unnecessary or it is a block element, delete it.
628+
/// starts with a whitespace, it isn't a line break, either the
629+
/// whitespace is unnecessary or it is a block element, and either it is
630+
/// first element in the parent node list or the previous element
631+
/// in the parent node list ends with a whitespace, delete it.
630632
///
631633
/// We should also delete the whitespace at any point in the node list
632634
/// if the previous element is a <br> because that tag makes the element
@@ -636,6 +638,10 @@ class HtmlParser extends StatelessWidget {
636638
&& tree.element?.localName != "br"
637639
&& (!keepLeadingSpace.data
638640
|| BLOCK_ELEMENTS.contains(tree.element?.localName ?? ""))
641+
&& (elementIndex < 1
642+
|| (elementIndex >= 1
643+
&& parentNodes?[elementIndex - 1] is dom.Text
644+
&& parentNodes![elementIndex - 1].text!.endsWith(" ")))
639645
) {
640646
tree.text = tree.text!.replaceFirst(' ', '');
641647
} else if (textIndex >= 1

0 commit comments

Comments
 (0)