diff --git a/k9mail/src/main/java/com/fsck/k9/message/html/HtmlConverter.java b/k9mail/src/main/java/com/fsck/k9/message/html/HtmlConverter.java index 35d82ae53..e037c3f1d 100644 --- a/k9mail/src/main/java/com/fsck/k9/message/html/HtmlConverter.java +++ b/k9mail/src/main/java/com/fsck/k9/message/html/HtmlConverter.java @@ -237,6 +237,11 @@ public class HtmlConverter { "style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid $$COLOR$$; padding-left: 1ex;\">"; private static final String HTML_BLOCKQUOTE_END = ""; private static final String HTML_NEWLINE = "
"; + private static final Pattern ASCII_PATTERN_FOR_HR = Pattern.compile( + "(^|\\Q" + HTML_NEWLINE + "\\E)\\s*((\\Q" + HTML_NEWLINE + "\\E)*" + + "((((\\Q" + HTML_NEWLINE + "\\E){0,2}([-=_]{3,})(\\Q" + HTML_NEWLINE + + "\\E){0,2})|(([-=_]{2,} ?)(8<|8|%<|%)" + + "( ?[-=_]{2,})))+(\\Q" + HTML_NEWLINE + "\\E|$)))"); /** * Convert a text string into an HTML document. @@ -329,8 +334,7 @@ public class HtmlConverter { HTML_BLOCKQUOTE_END + "$1" ); - // Replace lines of -,= or _ with horizontal rules - text = text.replaceAll("\\s*([-=_]{30,}+)\\s*", "
"); + text = ASCII_PATTERN_FOR_HR.matcher(text).replaceAll("
"); StringBuffer sb = new StringBuffer(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH); diff --git a/k9mail/src/test/java/com/fsck/k9/message/html/HtmlConverterTest.java b/k9mail/src/test/java/com/fsck/k9/message/html/HtmlConverterTest.java index 57e6105e6..28ce3cf3f 100644 --- a/k9mail/src/test/java/com/fsck/k9/message/html/HtmlConverterTest.java +++ b/k9mail/src/test/java/com/fsck/k9/message/html/HtmlConverterTest.java @@ -207,4 +207,105 @@ public class HtmlConverterTest { "http://example.com/" + "", outputBuffer.toString()); } + + @Test + public void issue2259Spec() { + String text = "text\n" + + "---------------------------\n" + + "some other text\n" + + "===========================\n" + + "more text\n" + + "-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" + + "scissors below\n" + + "-- >8 --\n" + + "other direction\n" + + "-- 8< --\n" + + "end"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
text
" + + "some other text
" + + "more text
" + + "scissors below
" + + "other direction
" + + "end
", + result); + } + + @Test + public void dashesContainingSpacesIgnoredAsHR() { + String text = "hello\n--- --- --- --- ---\nfoo bar"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
hello
--- --- --- --- ---
foo bar
", + result); + } + + @Test + public void mergeConsecutiveBreaksIntoOne() { + String text = "hello\n------------\n---------------\nfoo bar"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
hello
foo bar
", result); + } + + @Test + public void dashedHorizontalRulePrefixedWithTextIgnoredAsHR() { + String text = "hello----\n\n"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
hello----

", result); + } + + @Test + public void doubleMinusIgnoredAsHR() { + String text = "--\n"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
--
", result); + } + + @Test + public void doubleEqualsIgnoredAsHR() { + String text = "==\n"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
==
", result); + } + + @Test + public void doubleUnderscoreIgnoredAsHR() { + String text = "__\n"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
__
", result); + } + + @Test + public void anyTripletIsHRuledOut() { + String text = "--=\n-=-\n===\n___\n\n"; + String result = HtmlConverter.textToHtml(text); + assertEquals("

", result); + } + + @Test + public void replaceSpaceSeparatedDashesWithHR() { + String text = "hello\n---------------------------\nfoo bar"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
hello
foo bar
", result); + } + + @Test + public void replacementWithHRAtBeginning() { + String text = "---------------------------\nfoo bar"; + String result = HtmlConverter.textToHtml(text); + assertEquals("

foo bar
", result); + } + + @Test + public void replacementWithHRAtEnd() { + String text = "hello\n__________________________________"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
hello
", result); + } + + @Test + public void replacementOfScissorsByHR() { + String text = "hello\n-- %< -------------- >8 --\nworld\n"; + String result = HtmlConverter.textToHtml(text); + assertEquals("
hello
world
", result); + } }