Improved linkify

This commit is contained in:
M66B 2019-06-13 10:20:45 +02:00
parent 196d204c9b
commit 27cb003558
1 changed files with 7 additions and 6 deletions

View File

@ -213,23 +213,23 @@ public class HtmlHelper {
document.select("ol").tagName("div"); document.select("ol").tagName("div");
document.select("ul").tagName("div"); document.select("ul").tagName("div");
final Pattern pattern = Pattern.compile(
PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + "|" +
PatternsCompat.AUTOLINK_WEB_URL.pattern());
// Autolink // Autolink
NodeTraversor.traverse(new NodeVisitor() { NodeTraversor.traverse(new NodeVisitor() {
@Override @Override
public void head(Node node, int depth) { public void head(Node node, int depth) {
if (node instanceof TextNode) { if (node instanceof TextNode) {
TextNode tnode = (TextNode) node; TextNode tnode = (TextNode) node;
String text = tnode.text();
Pattern pattern = Pattern.compile( Matcher matcher = pattern.matcher(text);
PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + "|" +
PatternsCompat.AUTOLINK_WEB_URL.pattern());
Matcher matcher = pattern.matcher(tnode.text());
if (matcher.find()) { if (matcher.find()) {
Element span = document.createElement("span"); Element span = document.createElement("span");
int pos = 0; int pos = 0;
String text = tnode.text();
do { do {
boolean linked = false; boolean linked = false;
Node parent = tnode.parent(); Node parent = tnode.parent();
@ -260,6 +260,7 @@ public class HtmlHelper {
pos = matcher.end(); pos = matcher.end();
} while (matcher.find()); } while (matcher.find());
span.appendText(text.substring(pos)); span.appendText(text.substring(pos));
tnode.before(span); tnode.before(span);