Skip to content

Commit b21ddee

Browse files
committed
looking for uppercase W as well and not accepting alphanumeric before www.
1 parent 3563c68 commit b21ddee

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

src/main/java/org/nibor/autolink/LinkExtractor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ private Scanner trigger(char c) {
5050
case '@':
5151
return emailScanner;
5252
case 'w':
53+
case 'W':
5354
return wwwScanner;
5455
}
5556
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static boolean isNonAscii(char c) {
1818
return c >= 0x80;
1919
}
2020

21-
public static int findUrlEnd(CharSequence input, int beginIndex) {
21+
public static final int findUrlEnd(CharSequence input, int beginIndex) {
2222
int round = 0;
2323
int square = 0;
2424
int curly = 0;

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ public class WwwUrlScanner implements Scanner {
1212

1313
@Override
1414
public LinkSpan scan(final CharSequence input, int triggerIndex, int rewindIndex) {
15-
int afterDot = triggerIndex + 4;
16-
if (afterDot >= input.length() || input.charAt(triggerIndex + 1) != 'w' || input.charAt(triggerIndex + 2) != 'w' || input.charAt(triggerIndex + 3) != '.') {
15+
final int afterDot = triggerIndex + 4;
16+
if (afterDot >= input.length() || !isWWW(input, triggerIndex)) {
1717
return null;
1818
}
1919

20-
int first = triggerIndex;
20+
final int first = findFirst(input, triggerIndex, rewindIndex);
21+
if (first == -1) {
22+
return null;
23+
}
24+
2125
int last = findLast(input, afterDot);
2226
if (last == -1) {
2327
return null;
@@ -26,8 +30,21 @@ public LinkSpan scan(final CharSequence input, int triggerIndex, int rewindIndex
2630
return new LinkSpanImpl(LinkType.WWW, first, last + 1);
2731
}
2832

29-
private int findLast(CharSequence input, int beginIndex) {
30-
int last = Scanners.findUrlEnd(input, beginIndex);
33+
private static final int findFirst(final CharSequence input, final int beginIndex, final int rewindIndex) {
34+
if (beginIndex == rewindIndex) {
35+
return beginIndex;
36+
}
37+
38+
// Is the character before www. allowed?
39+
if (isAllowed(input.charAt(beginIndex - 1))) {
40+
return beginIndex;
41+
}
42+
43+
return -1;
44+
}
45+
46+
private static final int findLast(final CharSequence input, final int beginIndex) {
47+
final int last = Scanners.findUrlEnd(input, beginIndex);
3148

3249
// Make sure there is at least one dot after the first dot,
3350
// so www.something is not allowed, but www.something.co.uk is
@@ -38,4 +55,15 @@ private int findLast(CharSequence input, int beginIndex) {
3855

3956
return -1;
4057
}
58+
59+
private static final boolean isAllowed(char c) {
60+
return c != '.' && !Scanners.isAlnum(c);
61+
}
62+
63+
private static final boolean isWWW(final CharSequence input, final int triggerIndex) {
64+
return
65+
(input.charAt(triggerIndex + 1) == 'w' || input.charAt(triggerIndex + 1) == 'W')
66+
&& (input.charAt(triggerIndex + 2) == 'w' || input.charAt(triggerIndex + 2) == 'W')
67+
&& input.charAt(triggerIndex + 3) == '.';
68+
}
4169
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@ public void notLinked() {
3434
assertNotLinked("w.bar.foo.co");
3535
assertNotLinked("www.something");
3636
assertNotLinked("www.go");
37+
assertNotLinked("foo.www.fo.uk");
38+
assertNotLinked("www..com");
39+
assertNotLinked("wwww.toomany.com");
3740
}
3841

3942
@Test
4043
public void linked() {
4144
assertLinked("www.s.com","|www.s.com|");
4245
assertLinked("www.fo.uk","|www.fo.uk|");
46+
assertLinked("foo:www.fo.uk","foo:|www.fo.uk|");
47+
assertLinked("foo-www.fo.uk","foo-|www.fo.uk|");
48+
assertLinked("WWW.s.com","|WWW.s.com|");
49+
assertLinked("Www.s.com","|Www.s.com|");
4350
}
4451

4552
@Test

0 commit comments

Comments
 (0)