Merge pull request #2387 from ployt0/HtmlConverterTestForHR

Make <hr> tags from their plain text equivalents

Fixes #2259
This commit is contained in:
cketti 2017-03-15 05:16:24 +01:00 committed by GitHub
commit 9b49f08b60
2 changed files with 107 additions and 2 deletions

View file

@ -237,6 +237,11 @@ public class HtmlConverter {
"style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid $$COLOR$$; padding-left: 1ex;\">"; "style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid $$COLOR$$; padding-left: 1ex;\">";
private static final String HTML_BLOCKQUOTE_END = "</blockquote>"; private static final String HTML_BLOCKQUOTE_END = "</blockquote>";
private static final String HTML_NEWLINE = "<br />"; private static final String HTML_NEWLINE = "<br />";
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&lt;|<gt>8|%&lt;|<gt>%)" +
"( ?[-=_]{2,})))+(\\Q" + HTML_NEWLINE + "\\E|$)))");
/** /**
* Convert a text string into an HTML document. * Convert a text string into an HTML document.
@ -329,8 +334,7 @@ public class HtmlConverter {
HTML_BLOCKQUOTE_END + "$1" HTML_BLOCKQUOTE_END + "$1"
); );
// Replace lines of -,= or _ with horizontal rules text = ASCII_PATTERN_FOR_HR.matcher(text).replaceAll("<hr>");
text = text.replaceAll("\\s*([-=_]{30,}+)\\s*", "<hr />");
StringBuffer sb = new StringBuffer(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH); StringBuffer sb = new StringBuffer(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH);

View file

@ -207,4 +207,105 @@ public class HtmlConverterTest {
"http://example.com/" + "http://example.com/" +
"</a>", outputBuffer.toString()); "</a>", 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("<pre class=\"k9mail\">text<hr>" +
"some other text<hr>" +
"more text<hr>" +
"scissors below<hr>" +
"other direction<hr>" +
"end</pre>",
result);
}
@Test
public void dashesContainingSpacesIgnoredAsHR() {
String text = "hello\n--- --- --- --- ---\nfoo bar";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">hello<br />--- --- --- --- ---<br />foo bar</pre>",
result);
}
@Test
public void mergeConsecutiveBreaksIntoOne() {
String text = "hello\n------------\n---------------\nfoo bar";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">hello<hr>foo bar</pre>", result);
}
@Test
public void dashedHorizontalRulePrefixedWithTextIgnoredAsHR() {
String text = "hello----\n\n";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">hello----<br /><br /></pre>", result);
}
@Test
public void doubleMinusIgnoredAsHR() {
String text = "--\n";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">--<br /></pre>", result);
}
@Test
public void doubleEqualsIgnoredAsHR() {
String text = "==\n";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">==<br /></pre>", result);
}
@Test
public void doubleUnderscoreIgnoredAsHR() {
String text = "__\n";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">__<br /></pre>", result);
}
@Test
public void anyTripletIsHRuledOut() {
String text = "--=\n-=-\n===\n___\n\n";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\"><hr></pre>", result);
}
@Test
public void replaceSpaceSeparatedDashesWithHR() {
String text = "hello\n---------------------------\nfoo bar";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">hello<hr>foo bar</pre>", result);
}
@Test
public void replacementWithHRAtBeginning() {
String text = "---------------------------\nfoo bar";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\"><hr>foo bar</pre>", result);
}
@Test
public void replacementWithHRAtEnd() {
String text = "hello\n__________________________________";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">hello<hr></pre>", result);
}
@Test
public void replacementOfScissorsByHR() {
String text = "hello\n-- %< -------------- >8 --\nworld\n";
String result = HtmlConverter.textToHtml(text);
assertEquals("<pre class=\"k9mail\">hello<hr>world<br /></pre>", result);
}
} }