Loading app/core/src/main/java/com/fsck/k9/message/html/HtmlToPlainText.kt +20 −3 Original line number Diff line number Diff line Loading @@ -24,15 +24,28 @@ object HtmlToPlainText { private class FormattingVisitor : NodeVisitor { private var width = 0 private val output = StringBuilder() private var collectLinkText = false private var linkText = StringBuilder() override fun head(node: Node, depth: Int) { val name = node.nodeName() when { node is TextNode -> append(node.text()) node is TextNode -> { val text = node.text() append(text) if (collectLinkText) { linkText.append(text) } } name == "li" -> { startNewLine() append("* ") } name == "a" && node.hasAttr("href") -> { collectLinkText = true linkText.clear() } node is Element && node.isBlock -> startNewLine() } } Loading @@ -47,13 +60,17 @@ private class FormattingVisitor : NodeVisitor { addEmptyLine() } } name == "a" -> { name == "a" && node.hasAttr("href") -> { collectLinkText = false if (node.absUrl("href").isNotEmpty()) { if (linkText.toString() != node.attr("href")) { append(" <${node.attr("href")}>") } } } } } private fun append(text: String) { if (text.startsWith("\n")) { Loading app/core/src/test/java/com/fsck/k9/message/html/HtmlConverterTest.java +27 −0 Original line number Diff line number Diff line Loading @@ -279,4 +279,31 @@ public class HtmlConverterTest { assertEquals("One\n\nTwo\nThree\n\nFour", result); } @Test public void htmlToText_withLink() { String input = "<a href='https://domain.example/'>Link text</a>"; String result = HtmlConverter.htmlToText(input); assertEquals("Link text <https://domain.example/>", result); } @Test public void htmlToText_withLinkifiedUrl() { String input = "Text <a href='https://domain.example/path/'>https://domain.example/path/</a> more text"; String result = HtmlConverter.htmlToText(input); assertEquals("Text https://domain.example/path/ more text", result); } @Test public void htmlToText_withLinkifiedUrlContainingFormatting() { String input = "<a href='https://domain.example/path/'>https://<b>domain.example</b>/path/</a>"; String result = HtmlConverter.htmlToText(input); assertEquals("https://domain.example/path/", result); } } Loading
app/core/src/main/java/com/fsck/k9/message/html/HtmlToPlainText.kt +20 −3 Original line number Diff line number Diff line Loading @@ -24,15 +24,28 @@ object HtmlToPlainText { private class FormattingVisitor : NodeVisitor { private var width = 0 private val output = StringBuilder() private var collectLinkText = false private var linkText = StringBuilder() override fun head(node: Node, depth: Int) { val name = node.nodeName() when { node is TextNode -> append(node.text()) node is TextNode -> { val text = node.text() append(text) if (collectLinkText) { linkText.append(text) } } name == "li" -> { startNewLine() append("* ") } name == "a" && node.hasAttr("href") -> { collectLinkText = true linkText.clear() } node is Element && node.isBlock -> startNewLine() } } Loading @@ -47,13 +60,17 @@ private class FormattingVisitor : NodeVisitor { addEmptyLine() } } name == "a" -> { name == "a" && node.hasAttr("href") -> { collectLinkText = false if (node.absUrl("href").isNotEmpty()) { if (linkText.toString() != node.attr("href")) { append(" <${node.attr("href")}>") } } } } } private fun append(text: String) { if (text.startsWith("\n")) { Loading
app/core/src/test/java/com/fsck/k9/message/html/HtmlConverterTest.java +27 −0 Original line number Diff line number Diff line Loading @@ -279,4 +279,31 @@ public class HtmlConverterTest { assertEquals("One\n\nTwo\nThree\n\nFour", result); } @Test public void htmlToText_withLink() { String input = "<a href='https://domain.example/'>Link text</a>"; String result = HtmlConverter.htmlToText(input); assertEquals("Link text <https://domain.example/>", result); } @Test public void htmlToText_withLinkifiedUrl() { String input = "Text <a href='https://domain.example/path/'>https://domain.example/path/</a> more text"; String result = HtmlConverter.htmlToText(input); assertEquals("Text https://domain.example/path/ more text", result); } @Test public void htmlToText_withLinkifiedUrlContainingFormatting() { String input = "<a href='https://domain.example/path/'>https://<b>domain.example</b>/path/</a>"; String result = HtmlConverter.htmlToText(input); assertEquals("https://domain.example/path/", result); } }