Properly autolink URLs with unescaped parentheses

pull/147/head
Roland Illig 7 years ago
parent 99cbff252b
commit 5986051d9d

@ -131,9 +131,15 @@ public class HtmlHelper {
int end = 0; int end = 0;
while (matcher.find()) { while (matcher.find()) {
sb.append(Html.escapeHtml(text.substring(end, matcher.start()))); sb.append(Html.escapeHtml(text.substring(end, matcher.start())));
String ref = Html.escapeHtml(matcher.group());
String url = matcher.group();
if (url.endsWith(")") && !url.contains("(")) {
url = url.substring(0, url.length() - 1);
}
String ref = Html.escapeHtml(url);
sb.append(String.format("<a href=\"%s\">%s</a>", ref, ref)); sb.append(String.format("<a href=\"%s\">%s</a>", ref, ref));
end = matcher.end(); end = matcher.start() + url.length();
} }
sb.append(text.substring(end)); sb.append(text.substring(end));
return sb.toString(); return sb.toString();

@ -120,10 +120,10 @@ public class HtmlHelperTest {
testAutolink( testAutolink(
"See my homepage (at https://example.org), with lots of examples.", "See my homepage (at https://example.org), with lots of examples.",
// FIXME: The URL doesn't contain an opening parenthesis, therefore // This URL doesn't contain an opening parenthesis, therefore
// the closing parenthesis probably also doesn't belong to it. // the closing parenthesis probably also doesn't belong to it.
"See my homepage (at <a href=\"https://example.org)\">" + "See my homepage (at <a href=\"https://example.org\">" +
"https://example.org)</a>, with lots of examples." "https://example.org</a>), with lots of examples."
); );
// The terminating sequence of a base64-encoded URL parameter is always // The terminating sequence of a base64-encoded URL parameter is always

Loading…
Cancel
Save