Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 3921f579 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #5005 from k9mail/fix_urls_in_drafts

Fix URLs in drafts
parents 1835b502 2a674089
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -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()
        }
    }
@@ -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")) {
+27 −0
Original line number Diff line number Diff line
@@ -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);
    }
}