Skip to content

Commit 4dcc71e

Browse files
committed
Don't autolink if authority is only "end" characters (#15)
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.`
1 parent 724d6d0 commit 4dcc71e

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

src/main/java/org/nibor/autolink/internal/Scanners.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static int findUrlEnd(CharSequence input, int beginIndex) {
2424
int curly = 0;
2525
boolean doubleQuote = false;
2626
boolean singleQuote = false;
27-
int last = beginIndex;
27+
int last = -1;
2828
loop:
2929
for (int i = beginIndex; i < input.length(); i++) {
3030
char c = input.charAt(i);

src/main/java/org/nibor/autolink/internal/UrlScanner.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ public LinkSpan scan(CharSequence input, int triggerIndex, int rewindIndex) {
2424
}
2525

2626
int last = Scanners.findUrlEnd(input, afterSlashSlash);
27+
if (last == -1) {
28+
return null;
29+
}
2730

28-
return new LinkSpanImpl(LinkType.URL, first, last + 1 );
31+
return new LinkSpanImpl(LinkType.URL, first, last + 1);
2932
}
3033

3134
// See "scheme" in RFC 3986

src/main/java/org/nibor/autolink/internal/WwwScanner.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ private static int findFirst(final CharSequence input, final int beginIndex, fin
4646

4747
private static int findLast(final CharSequence input, final int beginIndex) {
4848
final int last = Scanners.findUrlEnd(input, beginIndex);
49+
if (last == -1) {
50+
return -1;
51+
}
4952

5053
// Make sure there is at least one dot after the first dot,
5154
// so www.something is not allowed, but www.something.co.uk is

src/test/java/org/nibor/autolink/AutolinkUrlTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ public void schemes() {
5656
}
5757

5858
@Test
59-
public void hostTooShort() {
59+
public void authority() {
6060
assertLinked("ab://", "ab://");
61+
assertLinked("http://", "http://");
62+
assertLinked("http:// ", "http:// ");
63+
assertLinked("\"http://\"", "\"http://\"");
64+
assertLinked("\"http://...\", ", "\"http://...\", ");
65+
66+
assertLinked("http://a.", "|http://a|.");
6167
}
6268

6369
@Test

0 commit comments

Comments
 (0)