Skip to content

Commit

Permalink
Don't autolink if authority is only "end" characters (#15)
Browse files Browse the repository at this point in the history
This change stops these and other examples from being linked:

    http://.
    http://"
    http://<space>

Note that `http://` and `http://.` are valid URLs according to RFC 3986,
because `authority` can be zero or more `unreserved` characters. But we
don't autolink `http://` on its own or the trailing `.` of
`http://example.org.`
  • Loading branch information
robinst committed Aug 31, 2017
1 parent 724d6d0 commit 4dcc71e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/nibor/autolink/internal/Scanners.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static int findUrlEnd(CharSequence input, int beginIndex) {
int curly = 0;
boolean doubleQuote = false;
boolean singleQuote = false;
int last = beginIndex;
int last = -1;
loop:
for (int i = beginIndex; i < input.length(); i++) {
char c = input.charAt(i);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/nibor/autolink/internal/UrlScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public LinkSpan scan(CharSequence input, int triggerIndex, int rewindIndex) {
}

int last = Scanners.findUrlEnd(input, afterSlashSlash);
if (last == -1) {
return null;
}

return new LinkSpanImpl(LinkType.URL, first, last + 1 );
return new LinkSpanImpl(LinkType.URL, first, last + 1);
}

// See "scheme" in RFC 3986
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/nibor/autolink/internal/WwwScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ private static int findFirst(final CharSequence input, final int beginIndex, fin

private static int findLast(final CharSequence input, final int beginIndex) {
final int last = Scanners.findUrlEnd(input, beginIndex);
if (last == -1) {
return -1;
}

// Make sure there is at least one dot after the first dot,
// so www.something is not allowed, but www.something.co.uk is
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/org/nibor/autolink/AutolinkUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ public void schemes() {
}

@Test
public void hostTooShort() {
public void authority() {
assertLinked("ab://", "ab://");
assertLinked("http://", "http://");
assertLinked("http:// ", "http:// ");
assertLinked("\"http://\"", "\"http://\"");
assertLinked("\"http://...\", ", "\"http://...\", ");

assertLinked("http://a.", "|http://a|.");
}

@Test
Expand Down

0 comments on commit 4dcc71e

Please sign in to comment.