@@ -12,12 +12,16 @@ public class WwwUrlScanner implements Scanner {
12
12
13
13
@ Override
14
14
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 ) ) {
17
17
return null ;
18
18
}
19
19
20
- int first = triggerIndex ;
20
+ final int first = findFirst (input , triggerIndex , rewindIndex );
21
+ if (first == -1 ) {
22
+ return null ;
23
+ }
24
+
21
25
int last = findLast (input , afterDot );
22
26
if (last == -1 ) {
23
27
return null ;
@@ -26,8 +30,21 @@ public LinkSpan scan(final CharSequence input, int triggerIndex, int rewindIndex
26
30
return new LinkSpanImpl (LinkType .WWW , first , last + 1 );
27
31
}
28
32
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 );
31
48
32
49
// Make sure there is at least one dot after the first dot,
33
50
// so www.something is not allowed, but www.something.co.uk is
@@ -38,4 +55,15 @@ private int findLast(CharSequence input, int beginIndex) {
38
55
39
56
return -1 ;
40
57
}
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
+ }
41
69
}
0 commit comments