@@ -3,34 +3,33 @@ function linkify(string, buildHashtagUrl, includeW3, target, noFollow) {
3
3
if ( noFollow ) {
4
4
relNoFollow = " rel=\"nofollow\"" ;
5
5
}
6
-
7
- string = string . replace ( / ( ( h t t p | h t t p s | f t p ) \: \/ \/ | \b w { 3 } \. ) [ a - z 0 - 9 \- \. ] + \. [ a - z ] { 2 , 3 } ( : [ a - z 0 - 9 ] * ) ? \/ ? ( [ a - z \u00C0 - \u017F 0 - 9 \- \. _ \? \, \' \/ \\ \+ & a m p ; % \$ # \= ~ ] ) * / gi, function ( captured ) {
8
- var uri ;
9
- if ( captured . toLowerCase ( ) . indexOf ( "www." ) == 0 ) {
10
- if ( ! includeW3 ) {
11
- return captured ;
12
- }
13
- uri = "http://" + captured ;
14
- } else {
15
- uri = captured ;
16
- }
17
- return "<a href=\"" + uri + "\" target=\"" + target + "\"" + relNoFollow + ">" + captured + "</a>" ;
18
- } ) ;
19
-
6
+
7
+ if ( string . toLowerCase ( ) . indexOf ( "www." ) === 0 && includeW3 ) {
8
+ string = '<a href="http://' + string + '" target="' + target + '"' + relNoFollow + '>' + string + '</a>' ;
9
+ } else {
10
+ string = '<a href="' + string + '" target="' + target + '"' + relNoFollow + '>' + string + '</a>' ;
11
+ }
12
+
20
13
if ( buildHashtagUrl ) {
21
- string = string . replace ( / \B # ( \w + ) / g, " <a href=" + buildHashtagUrl ( "$1" ) + " target=\"" + target + "\"" + relNoFollow + " >#$1</a>" ) ;
14
+ string = string . replace ( / \B # ( \w + ) / g, ' <a href=' + buildHashtagUrl ( "$1" ) + ' target="' + target + '"' + relNoFollow + ' >#$1</a>' ) ;
22
15
}
23
16
return string ;
24
17
}
25
18
26
19
( function ( $ ) {
27
20
$ . fn . linkify = function ( opts ) {
28
21
return this . each ( function ( ) {
29
- var $this = $ ( this ) ;
30
22
var buildHashtagUrl ;
31
23
var includeW3 = true ;
32
24
var target = '_self' ;
33
25
var noFollow = true ;
26
+ var regex = / ( ( h t t p | h t t p s | f t p ) \: \/ \/ | \b w { 3 } \. ) [ a - z 0 - 9 \- \. ] + \. [ a - z ] { 2 , 3 } ( : [ a - z 0 - 9 ] * ) ? \/ ? ( [ a - z \u00C0 - \u017F 0 - 9 \- \. _ \? \, \' \/ \\ \+ & a m p ; % \$ # \= ~ ] ) * / gi;
27
+ var txt = this . innerHTML ;
28
+ var output = '' ;
29
+ var replacement ;
30
+ var matchLen ;
31
+ var lastIndex = 0 ;
32
+
34
33
if ( opts ) {
35
34
if ( typeof opts == "function" ) {
36
35
buildHashtagUrl = opts ;
@@ -49,18 +48,20 @@ function linkify(string, buildHashtagUrl, includeW3, target, noFollow) {
49
48
}
50
49
}
51
50
}
52
- $this . html (
53
- $ . map (
54
- $this . contents ( ) ,
55
- function ( n , i ) {
56
- if ( n . nodeType == 3 ) {
57
- return linkify ( n . data , buildHashtagUrl , includeW3 , target , noFollow ) ;
58
- } else {
59
- return n . outerHTML ;
60
- }
61
- }
62
- ) . join ( "" )
63
- ) ;
51
+
52
+ while ( ( match = regex . exec ( txt ) ) !== null ) {
53
+ matchLen = match [ 0 ] . length ;
54
+ replacement = linkify ( match [ 0 ] , buildHashtagUrl , includeW3 , target , noFollow ) ;
55
+ output += txt . substring ( lastIndex , match . index + matchLen ) . replace ( match [ 0 ] , replacement ) ;
56
+ lastIndex = match . index + matchLen ;
57
+ }
58
+
59
+ // Include the rest of the text.
60
+ if ( lastIndex !== txt . length ) {
61
+ output += txt . substring ( lastIndex ) ;
62
+ }
63
+
64
+ $ ( this ) . html ( output ) ;
64
65
} ) ;
65
- }
66
- } ) ( jQuery ) ;
66
+ } ;
67
+ } ) ( jQuery ) ;
0 commit comments