Added fail-save

pull/213/head
M66B 2 years ago
parent ae09857097
commit f17b715257

@ -1530,82 +1530,91 @@ public class HtmlHelper {
} }
Matcher matcher = pattern.matcher(text); Matcher matcher = pattern.matcher(text);
if (matcher.find()) { if (matcher.find())
Element span = document.createElement("span"); try {
Element span = document.createElement("span");
int pos = 0; int pos = 0;
do { do {
boolean linked = false; boolean linked = false;
Node parent = tnode.parent(); Node parent = tnode.parent();
while (parent != null) { while (parent != null) {
if ("a".equals(parent.nodeName())) { if ("a".equals(parent.nodeName())) {
linked = true; linked = true;
break; break;
}
parent = parent.parent();
} }
parent = parent.parent();
}
String group = matcher.group();
int start = matcher.start();
int end = matcher.end();
// Workarounds String group = matcher.group();
if (group.endsWith(".")) { int start = matcher.start();
end--; int end = matcher.end();
group = group.substring(0, group.length() - 1); if (start < pos || start > end) {
} Log.e("Autolink pos=" + pos +
if (group.startsWith("(")) { " start=" + start + " end=" + end +
start++; " len=" + group.length() + "/" + text.length());
group = group.substring(1); return;
} }
if (group.endsWith(")")) {
end--;
group = group.substring(0, group.length() - 1);
}
if (end < text.length() && text.charAt(end) == '$') {
end++;
group += '$';
}
boolean email = group.contains("@") && !group.contains(":"); // Workarounds
Log.i("Web url=" + group + " " + start + "..." + end + "/" + text.length() + if (group.endsWith(".")) {
" linked=" + linked + " email=" + email + " count=" + links); end--;
group = group.substring(0, group.length() - 1);
}
if (group.startsWith("(")) {
start++;
group = group.substring(1);
}
if (group.endsWith(")")) {
end--;
group = group.substring(0, group.length() - 1);
}
if (end < text.length() && text.charAt(end) == '$') {
end++;
group += '$';
}
if (linked) boolean email = group.contains("@") && !group.contains(":");
span.appendText(text.substring(pos, end)); Log.i("Web url=" + group + " " + start + "..." + end + "/" + text.length() +
else { " linked=" + linked + " email=" + email + " count=" + links);
span.appendText(text.substring(pos, start));
Element a = document.createElement("a"); if (linked)
if (BuildConfig.DEBUG && GPA_PATTERN.matcher(group).matches()) span.appendText(text.substring(pos, end));
a.attr("href", BuildConfig.GPA_URI + group);
else { else {
String url = (email ? "mailto:" : "") + group; span.appendText(text.substring(pos, start));
if (outbound)
try { Element a = document.createElement("a");
Uri uri = UriHelper.guessScheme(Uri.parse(url)); if (BuildConfig.DEBUG && GPA_PATTERN.matcher(group).matches())
a.attr("href", uri.toString()); a.attr("href", BuildConfig.GPA_URI + group);
} catch (Throwable ex) { else {
Log.e(ex); String url = (email ? "mailto:" : "") + group;
if (outbound)
try {
Uri uri = UriHelper.guessScheme(Uri.parse(url));
a.attr("href", uri.toString());
} catch (Throwable ex) {
Log.e(ex);
a.attr("href", url);
}
else
a.attr("href", url); a.attr("href", url);
} }
else a.text(group);
a.attr("href", url); span.appendChild(a);
}
a.text(group);
span.appendChild(a);
links++; links++;
} }
pos = end; pos = end;
} while (links < MAX_AUTO_LINK && matcher.find()); } while (links < MAX_AUTO_LINK && matcher.find());
span.appendText(text.substring(pos)); span.appendText(text.substring(pos));
tnode.before(span); tnode.before(span);
tnode.text(""); tnode.text("");
} } catch (Throwable ex) {
Log.e(ex);
}
} }
} }

Loading…
Cancel
Save