Skip to content

Commit

Permalink
Fix unexpected link end with unfinished delimiter pairs (#5)
Browse files Browse the repository at this point in the history
This: http://example.org/"_(foo)
Was extracted like this: http://example.org/"_(foo

The problem was that all delimiter pairs were checked in one go, instead
of just when the delimiter actually occurred.

This also changes behavior for unbalanced brackets: A lone closing
bracket immediately terminates a link now.
  • Loading branch information
robinst committed Feb 9, 2016
1 parent 9a6c3f9 commit 9c30a1e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
40 changes: 33 additions & 7 deletions src/main/java/org/nibor/autolink/internal/UrlScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,49 +77,75 @@ private int findLast(CharSequence input, int beginIndex) {
case ':':
case ';':
// These may be part of an URL but not at the end
continue loop;
break;
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;
if (last == i - 1) {
last = i;
}
break;
case '(':
round++;
break;
case ')':
round--;
if (round >= 0) {
last = i;
} else {
// More closing than opening brackets, stop now
break loop;
}
break;
case '[':
square++;
break;
case ']':
square--;
if (square >= 0) {
last = i;
} else {
// More closing than opening brackets, stop now
break loop;
}
break;
case '{':
curly++;
break;
case '}':
curly--;
if (curly >= 0) {
last = i;
} else {
// More closing than opening brackets, stop now
break loop;
}
break;
case '<':
angle++;
break;
case '>':
angle--;
if (angle >= 0) {
last = i;
} else {
// More closing than opening brackets, stop now
break loop;
}
break;
case '"':
doubleQuote = !doubleQuote;
if (!doubleQuote) {
last = i;
}
break;
case '\'':
singleQuote = !singleQuote;
if (!singleQuote) {
last = i;
}
break;
default:
last = i;
continue loop;
}
if (round >= 0 && square >= 0 && curly >= 0 && angle >= 0 && !doubleQuote && !singleQuote) {
last = i;
}
}
return last;
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/org/nibor/autolink/AutolinkUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,23 @@ public void matchingPunctuationTricky() {
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/(|");
// not sure about these
assertLinked("http://example.org/(", "|http://example.org/|(");
assertLinked("http://example.org/(.", "|http://example.org/|(.");
assertLinked("http://example.org/]()", "|http://example.org/|]()");
}

@Test
public void quotes() {
assertLinked("http://example.org/\"_(foo)", "|http://example.org/\"_(foo)|");
assertLinked("http://example.org/\"_(foo)\"", "|http://example.org/\"_(foo)\"|");
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/\"a", "|http://example.org/\"a|");
assertLinked("http://example.org/it's", "|http://example.org/it's|");
}

@Test
public void html() {
assertLinked("http://example.org\">", "|http://example.org|\">");
Expand All @@ -143,6 +155,7 @@ public void slash() {
public void multiple() {
assertLinked("http://one.org/ http://two.org/", "|http://one.org/| |http://two.org/|");
assertLinked("http://one.org/ : http://two.org/", "|http://one.org/| : |http://two.org/|");
assertLinked("(http://one.org/)(http://two.org/)", "(|http://one.org/|)(|http://two.org/|)");
}

@Test
Expand Down

0 comments on commit 9c30a1e

Please sign in to comment.