Skip to content

Commit b0abb3c

Browse files
committed
Fix issue with whitespace getting deleted inside <body> when surrounded by two inline elements
1 parent 6e2b3aa commit b0abb3c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/html_parser.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:collection';
22
import 'dart:math';
33

4+
import 'package:collection/collection.dart';
45
import 'package:csslib/parser.dart' as cssparser;
56
import 'package:csslib/visitor.dart' as css;
67
import 'package:flutter/material.dart';
@@ -438,7 +439,7 @@ class HtmlParser extends StatelessWidget {
438439
&& tree.text!.startsWith(' ')
439440
&& tree.element?.localName != "br"
440441
&& (!keepLeadingSpace.data
441-
|| BLOCK_ELEMENTS.contains(tree.element?.localName ?? ""))
442+
|| tree.style.display == Display.BLOCK)
442443
&& (elementIndex < 1
443444
|| (elementIndex >= 1
444445
&& parentNodes?[elementIndex - 1] is dom.Text
@@ -747,11 +748,16 @@ class HtmlParser extends StatelessWidget {
747748
static StyledElement _removeEmptyElements(StyledElement tree) {
748749
List<StyledElement> toRemove = <StyledElement>[];
749750
bool lastChildBlock = true;
750-
tree.children.forEach((child) {
751+
tree.children.forEachIndexed((index, child) {
751752
if (child is EmptyContentElement || child is EmptyLayoutElement) {
752753
toRemove.add(child);
753754
} else if (child is TextContentElement
754-
&& (tree.name == "body" || tree.name == "ul")
755+
&& ((tree.name == "body"
756+
&& (index == 0
757+
|| index + 1 == tree.children.length
758+
|| tree.children[index - 1].style.display == Display.BLOCK
759+
|| tree.children[index + 1].style.display == Display.BLOCK))
760+
|| tree.name == "ul")
755761
&& child.text!.replaceAll(' ', '').isEmpty) {
756762
toRemove.add(child);
757763
} else if (child is TextContentElement

0 commit comments

Comments
 (0)