Added tests for autolink feature

pull/147/head
Roland Illig 7 years ago
parent f05bd3c9d5
commit de5e571e71

@ -111,11 +111,7 @@ public class HtmlHelper {
public void head(Node node, int depth) { public void head(Node node, int depth) {
if (node instanceof TextNode) { if (node instanceof TextNode) {
String text = Html.escapeHtml(((TextNode) node).text()); String text = Html.escapeHtml(((TextNode) node).text());
Matcher matcher = pattern.matcher(text); text = autolink(text);
while (matcher.find()) {
String ref = matcher.group();
text = text.replace(ref, String.format("<a href=\"%s\">%s</a>", ref, ref));
}
node.before(text); node.before(text);
((TextNode) node).text(""); ((TextNode) node).text("");
} }
@ -129,6 +125,15 @@ public class HtmlHelper {
return document.body().html(); return document.body().html();
} }
static String autolink(String text) {
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String ref = matcher.group();
text = text.replace(ref, String.format("<a href=\"%s\">%s</a>", ref, ref));
}
return text;
}
static Drawable decodeImage(String source, Context context, long id, boolean show) { static Drawable decodeImage(String source, Context context, long id, boolean show) {
int px = Helper.dp2pixels(context, 48); int px = Helper.dp2pixels(context, 48);

@ -0,0 +1,56 @@
package eu.faircode.email;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class HtmlHelperTest {
@Test
void autolink() {
testAutolink(
"To visit http://www.example.org, go to http://www.example.org.",
"" +
// FIXME: The trailing comma must not be part of the URL.
"To visit <a href=\"http://www.example.org,\">http://www.example.org,</a> " +
// FIXME: The trailing dot must not be part of the URL.
"go to <a href=\"http://www.example.org.\">http://www.example.org.</a>");
testAutolink(
"one hhhhh|spt://example.org three",
// FIXME: "hhhhh|spt" is not a proper URL scheme.
"one <a href=\"hhhhh|spt://example.org\">hhhhh|spt://example.org</a> three"
);
testAutolink(
"https://example.org/search?q=%C3%A4&hl=nl",
// TODO: Strictly speaking, the & should be encoded as &amp;.
// Most browsers can deal with this situation though.
"<a href=\"https://example.org/search?q=%C3%A4&hl=nl\">" +
"https://example.org/search?q=%C3%A4&hl=nl</a>"
);
testAutolink(
"Go to \"http://example.org/\".",
// FIXME: The quote must not end up as part of the URL.
"Go to \"<a href=\"http://example.org/\".\">http://example.org/\".</a>"
);
testAutolink(
"Go to <http://example.org/>.",
// FIXME: The < must be encoded as &lt;.
// FIXME: THe > must not end up as part of the URL.
"Go to <<a href=\"http://example.org/>.\">http://example.org/>.</a>"
);
}
private void testAutolink(String input, String expectedOutput) {
assertThat(HtmlHelper.autolink(input)).isEqualTo(expectedOutput);
}
}
Loading…
Cancel
Save