From 37a6f1bda045a40f22ccfd3cc1c72741875b7889 Mon Sep 17 00:00:00 2001 From: tanay Date: Tue, 15 Jun 2021 17:53:45 -0400 Subject: [PATCH] Fix edge case with leading whitespaces --- lib/html_parser.dart | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/html_parser.dart b/lib/html_parser.dart index 761e5fd2cc..c4e70ed785 100644 --- a/lib/html_parser.dart +++ b/lib/html_parser.dart @@ -625,8 +625,10 @@ class HtmlParser extends StatelessWidget { parentAfterText = parentAfter?.text ?? " "; } /// If the text is the first element in the current tree node list, it - /// starts with a whitespace, it isn't a line break, and either the - /// whitespace is unnecessary or it is a block element, delete it. + /// starts with a whitespace, it isn't a line break, either the + /// whitespace is unnecessary or it is a block element, and either it is + /// first element in the parent node list or the previous element + /// in the parent node list ends with a whitespace, delete it. /// /// We should also delete the whitespace at any point in the node list /// if the previous element is a
because that tag makes the element @@ -636,6 +638,10 @@ class HtmlParser extends StatelessWidget { && tree.element?.localName != "br" && (!keepLeadingSpace.data || BLOCK_ELEMENTS.contains(tree.element?.localName ?? "")) + && (elementIndex < 1 + || (elementIndex >= 1 + && parentNodes?[elementIndex - 1] is dom.Text + && parentNodes![elementIndex - 1].text!.endsWith(" "))) ) { tree.text = tree.text!.replaceFirst(' ', ''); } else if (textIndex >= 1