Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/main/java/org/nibor/autolink/internal/UrlScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private int findLast(CharSequence input, int beginIndex) {
int round = 0;
int square = 0;
int curly = 0;
int angle = 0;
boolean doubleQuote = false;
boolean singleQuote = false;
int last = beginIndex;
Expand All @@ -67,13 +68,22 @@ private int findLast(CharSequence input, int beginIndex) {
case '\u000B':
case '\f':
case '\r':
// These can never be part of an URL, so stop now
break loop;
case '?':
case '!':
case '.':
case ',':
case ':':
case ';':
// These may be part of an URL but not at the end
continue loop;
case '/':
// This may be part of an URL and at the end, but not if the previous character can't be the end of an URL
if (last != i - 1) {
continue loop;
}
break;
case '(':
round++;
break;
Expand All @@ -92,6 +102,12 @@ private int findLast(CharSequence input, int beginIndex) {
case '}':
curly--;
break;
case '<':
angle++;
break;
case '>':
angle--;
break;
case '"':
doubleQuote = !doubleQuote;
break;
Expand All @@ -102,7 +118,7 @@ private int findLast(CharSequence input, int beginIndex) {
last = i;
continue loop;
}
if (round >= 0 && square >= 0 && curly >= 0 && !doubleQuote && !singleQuote) {
if (round >= 0 && square >= 0 && curly >= 0 && angle >= 0 && !doubleQuote && !singleQuote) {
last = i;
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/nibor/autolink/AutolinkUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ public void delimiterSeparation() {
assertLinked("http://example.org/:", "|http://example.org/|:");
assertLinked("http://example.org/?", "|http://example.org/|?");
assertLinked("http://example.org/!", "|http://example.org/|!");
assertLinked("http://example.org/;", "|http://example.org/|;");
}

@Test
public void matchingPunctuation() {
assertLinked("http://example.org/a(b)", "|http://example.org/a(b)|");
assertLinked("http://example.org/a[b]", "|http://example.org/a[b]|");
assertLinked("http://example.org/a{b}", "|http://example.org/a{b}|");
assertLinked("http://example.org/a<b>", "|http://example.org/a<b>|");
assertLinked("http://example.org/a\"b\"", "|http://example.org/a\"b\"|");
assertLinked("http://example.org/a'b'", "|http://example.org/a'b'|");
assertLinked("(http://example.org/)", "(|http://example.org/|)");
Expand All @@ -110,11 +112,33 @@ public void matchingPunctuationTricky() {
assertLinked("[(http://example.org/)]", "[(|http://example.org/|)]");
assertLinked("(http://example.org/).", "(|http://example.org/|).");
assertLinked("(http://example.org/.)", "(|http://example.org/|.)");
assertLinked("http://example.org/>", "|http://example.org/|>");
// not sure about these:
assertLinked("http://example.org/(", "|http://example.org/(|");
assertLinked("http://example.org/]()", "|http://example.org/|]()");
}

@Test
public void html() {
assertLinked("http://example.org\">", "|http://example.org|\">");
assertLinked("http://example.org'>", "|http://example.org|'>");
assertLinked("http://example.org\"/>", "|http://example.org|\"/>");
assertLinked("http://example.org'/>", "|http://example.org|'/>");
}

@Test
public void css() {
assertLinked("http://example.org\");", "|http://example.org|\");");
assertLinked("http://example.org');", "|http://example.org|');");
}

@Test
public void slash() {
assertLinked("http://example.org/", "|http://example.org/|");
assertLinked("http://example.org/a/", "|http://example.org/a/|");
assertLinked("http://example.org//", "|http://example.org//|");
}

@Test
public void multiple() {
assertLinked("http://one.org/ http://two.org/", "|http://one.org/| |http://two.org/|");
Expand Down