@@ -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}
0 commit comments