From 8f334dd0c318518dbcff3d9f4466727a73a5e4f8 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 6 Feb 2018 19:14:56 +0100 Subject: [PATCH] Fix bug in EmailSectionExtractor when text ends with an empty line --- .../k9/message/html/EmailSectionExtractor.kt | 10 +++--- .../message/html/EmailSectionExtractorTest.kt | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/message/html/EmailSectionExtractor.kt b/k9mail/src/main/java/com/fsck/k9/message/html/EmailSectionExtractor.kt index eed0aea74..e995d8363 100644 --- a/k9mail/src/main/java/com/fsck/k9/message/html/EmailSectionExtractor.kt +++ b/k9mail/src/main/java/com/fsck/k9/message/html/EmailSectionExtractor.kt @@ -96,12 +96,10 @@ class EmailSectionExtractor private constructor(val text: String) { } private fun completeLastSection() { - if (!isStartOfLine) { - if (quoteDepth == 0) { - sectionBuilder.addSegment(0, sectionStartIndex, text.length) - } else { - sectionBuilder.addSegment(spaces, startOfContentIndex, text.length) - } + if (quoteDepth == 0) { + sectionBuilder.addSegment(0, sectionStartIndex, text.length) + } else if (!isStartOfLine) { + sectionBuilder.addSegment(spaces, startOfContentIndex, text.length) } appendSection() diff --git a/k9mail/src/test/java/com/fsck/k9/message/html/EmailSectionExtractorTest.kt b/k9mail/src/test/java/com/fsck/k9/message/html/EmailSectionExtractorTest.kt index 1b00df103..bce97ac1f 100644 --- a/k9mail/src/test/java/com/fsck/k9/message/html/EmailSectionExtractorTest.kt +++ b/k9mail/src/test/java/com/fsck/k9/message/html/EmailSectionExtractorTest.kt @@ -26,6 +26,19 @@ class EmailSectionExtractorTest { } } + @Test + fun simpleMessageEndingWithTwoNewlines() { + val message = "Hello\n\n" + + val sections = EmailSectionExtractor.extract(message) + + assertThat(sections.size).isEqualTo(1) + with(sections[0]) { + assertThat(quoteDepth).isEqualTo(0) + assertThat(toString()).isEqualTo(message) + } + } + @Test fun quoteFollowedByReply() { val message = """ @@ -81,6 +94,24 @@ class EmailSectionExtractorTest { } } + @Test + fun quoteEndingWithEmptyLineButNoNewline() { + val message = """ + > Quoted text + > """.trimIndent() + + val sections = EmailSectionExtractor.extract(message) + + assertThat(sections.size).isEqualTo(1) + with(sections[0]) { + assertThat(quoteDepth).isEqualTo(1) + // Note: "Quoted text\n\n" would be a better representation of the quoted text. The goal of this test is + // not to preserve the current behavior of only ending in one newline, but to make sure we don't add the + // last line twice. + assertThat(toString()).isEqualTo("Quoted text\n") + } + } + @Test fun chaosQuoting() { val message = """