Skip to content

Commit 12b48b1

Browse files
committed
Delete whitespaces when a <br> directly precedes the text node
1 parent e701273 commit 12b48b1

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lib/html_parser.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,23 @@ class HtmlParser extends StatelessWidget {
523523
/// If the text is the first element in the current tree node list, it
524524
/// starts with a whitespace, it isn't a line break, and either the
525525
/// whitespace is unnecessary or it is a block element, delete it.
526+
///
527+
/// We should also delete the whitespace at any point in the node list
528+
/// if the previous element is a <br> because that tag makes the element
529+
/// act like a block element.
526530
if (textIndex < 1
527531
&& tree.text!.startsWith(' ')
528532
&& tree.element?.localName != "br"
529533
&& (!keepLeadingSpace.data
530534
|| BLOCK_ELEMENTS.contains(tree.element?.localName ?? ""))
531535
) {
532536
tree.text = tree.text!.replaceFirst(' ', '');
537+
} else if (textIndex >= 1
538+
&& tree.text!.startsWith(' ')
539+
&& tree.element?.nodes[textIndex - 1] is dom.Element
540+
&& (tree.element?.nodes[textIndex - 1] as dom.Element).localName == "br"
541+
) {
542+
tree.text = tree.text!.replaceFirst(' ', '');
533543
}
534544
/// If the text is the last element in the current tree node list, it isn't
535545
/// a line break, and the next text node starts with a whitespace,

0 commit comments

Comments
 (0)