Skip to content

Commit

Permalink
Merge pull request #4 from robinst/issue-3-angle-brackets-and-slash
Browse files Browse the repository at this point in the history
Treat more special characters as trailing delimiters (#3)
  • Loading branch information
robinst committed Feb 9, 2016
2 parents 5160add + 249b044 commit 9a6c3f9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
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

0 comments on commit 9a6c3f9

Please sign in to comment.