Properly handle <br> tag when converting HTML to plain text

This commit is contained in:
cketti 2020-04-24 00:30:35 +02:00
parent 2530dea98a
commit a3bf389065
2 changed files with 19 additions and 0 deletions

View file

@ -41,6 +41,7 @@ private class FormattingVisitor : NodeVisitor {
val name = node.nodeName()
when {
name == "li" -> append("\n")
name == "br" -> append("\n")
node is Element && node.isBlock -> {
if (node.hasText()) {
addEmptyLine()

View file

@ -249,4 +249,22 @@ public class HtmlConverterTest {
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">hello<hr>world<br></pre>", result);
}
@Test
public void htmlToText_withLineBreaks() {
String input = "One<br>Two<br><br>Three";
String result = HtmlConverter.htmlToText(input);
assertEquals("One\nTwo\n\nThree", result);
}
@Test
public void htmlToText_withBlockElements() {
String input = "<p>One</p><p>Two<br>Three</p><div>Four</div>";
String result = HtmlConverter.htmlToText(input);
assertEquals("One\n\nTwo\nThree\n\nFour", result);
}
}