Skip to content

Commit 8a0382c

Browse files
committed
Potentially fix leading whitespaces issues
1 parent d5f16c5 commit 8a0382c

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/html_parser.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,19 @@ class HtmlParser extends StatelessWidget {
520520
}
521521
parentAfterText = parentAfter?.text ?? " ";
522522
}
523+
if (tree.text?.contains("Linking") ?? false) {
524+
print(keepLeadingSpace.data);
525+
print(tree.text);
526+
print(BLOCK_ELEMENTS.contains(tree.element?.localName ?? ""));
527+
}
523528
/// If the text is the first element in the current tree node list, it
524-
/// starts with a whitespace, it isn't a line break, and we don't need to
525-
/// keep the whitespace, delete it.
529+
/// starts with a whitespace, it isn't a line break, and either the
530+
/// whitespace is unnecessary or it is a block element, delete it.
526531
if (textIndex < 1
527532
&& tree.text!.startsWith(' ')
528533
&& tree.element?.localName != "br"
529-
&& !keepLeadingSpace.data
534+
&& (!keepLeadingSpace.data
535+
|| BLOCK_ELEMENTS.contains(tree.element?.localName ?? ""))
530536
) {
531537
tree.text = tree.text!.replaceFirst(' ', '');
532538
}

lib/src/html_elements.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,40 @@ const STYLED_ELEMENTS = [
7070
"ul",
7171
];
7272

73+
const BLOCK_ELEMENTS = [
74+
"article",
75+
"aside",
76+
"blockquote",
77+
"body",
78+
"center",
79+
"dd",
80+
"div",
81+
"dl",
82+
"dt",
83+
"figcaption",
84+
"figure",
85+
"footer",
86+
"h1",
87+
"h2",
88+
"h3",
89+
"h4",
90+
"h5",
91+
"h6",
92+
"header",
93+
"hr",
94+
"html",
95+
"li",
96+
"main",
97+
"nav",
98+
"noscript",
99+
"ol",
100+
"p",
101+
"pre",
102+
"section",
103+
"summary",
104+
"ul",
105+
];
106+
73107
const INTERACTABLE_ELEMENTS = [
74108
"a",
75109
];

0 commit comments

Comments
 (0)