Skip to content

Commit c3cc248

Browse files
committed
...but preserve whitespace inside 'pre' tag
1 parent 52376e5 commit c3cc248

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

lib/html_parser.dart

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,23 +304,24 @@ class HtmlRichTextParser extends StatelessWidget {
304304

305305
// we might want to preserve internal whitespace
306306
// empty strings of whitespace might be significant or not, condense it by default
307-
String finalText = parseContext.condenseWhitespace
308-
? condenseHtmlWhitespace(node.text)
309-
: node.text;
310-
311-
// if this is part of a string of spans, we will preserve leading
312-
// and trailing whitespace unless the previous character is whitespace
313-
if (parseContext.parentElement == null)
314-
finalText = finalText.trimLeft();
315-
else if (parseContext.parentElement is TextSpan ||
316-
parseContext.parentElement is LinkTextSpan) {
317-
String lastString = parseContext.parentElement.text ?? '';
318-
if (!parseContext.parentElement.children.isEmpty) {
319-
lastString = parseContext.parentElement.children.last.text;
307+
String finalText = node.text;
308+
if (parseContext.condenseWhitespace) {
309+
finalText = condenseHtmlWhitespace(node.text);
310+
311+
// if this is part of a string of spans, we will preserve leading
312+
// and trailing whitespace unless the previous character is whitespace
313+
if (parseContext.parentElement == null)
314+
finalText = finalText.trimLeft();
315+
else if (parseContext.parentElement is TextSpan ||
316+
parseContext.parentElement is LinkTextSpan) {
317+
String lastString = parseContext.parentElement.text ?? '';
318+
if (!parseContext.parentElement.children.isEmpty) {
319+
lastString = parseContext.parentElement.children.last.text;
320+
}
321+
if (lastString == '' ||
322+
lastString.endsWith(' ') ||
323+
lastString.endsWith('\n')) finalText = finalText.trimLeft();
320324
}
321-
if (lastString == '' ||
322-
lastString.endsWith(' ') ||
323-
lastString.endsWith('\n')) finalText = finalText.trimLeft();
324325
}
325326

326327
// if the finalText is actually empty, just return
@@ -403,7 +404,7 @@ class HtmlRichTextParser extends StatelessWidget {
403404
// OTHER ELEMENT NODES
404405
else if (node is dom.Element) {
405406
assert(() {
406-
debugPrint("Found ${node.localName}");
407+
// debugPrint("Found ${node.localName}");
407408
// debugPrint(node.outerHtml);
408409
return true;
409410
}());

0 commit comments

Comments
 (0)