/
jquery.linkify.js
executable file
·158 lines (134 loc) · 5.19 KB
/
jquery.linkify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* encoding: utf-8
**** linkify plugin for jQuery - automatically finds and changes URLs in text content into proper hyperlinks ****
Version: 1.0
Copyright (c) 2009
Már Ãrlygsson (http://mar.anomy.net/) &
Hugsmiðjan ehf. (http://www.hugsmidjan.is)
Dual licensed under a MIT licence (http://en.wikipedia.org/wiki/MIT_License)
and GPL 2.0 or above (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
-----------------------------------------------------------------------------
Demo and Qunit-tests:
* <./jquery.linkify-1.0-demo.html>
* <./jquery.linkify-1.0-test.html>
Documentation:
* ...
Get updates from:
* <http://github.com/maranomynet/linkify/>
* <git://github.com/maranomynet/linkify.git>
-----------------------------------------------------------------------------
Requires:
* jQuery (1.2.6 or later)
Usage:
jQuery('.articlebody').linkify();
// adding plugins:
jQuery.extend( jQuery.fn.linkify.plugins, {
name1: {
re: RegExp
tmpl: String/Function
},
name2: function(html){ return html; }
});
// Uses all plugins by default:
jQuery('.articlebody').linkify();
// Use only certain plugins:
jQuery('.articlebody').linkify('name1,name2');
jQuery('.articlebody').linkify({ use: 'name1,name2' });
jQuery('.articlebody').linkify({ use: ['name1','name2'] });
*/
(function($){
var noProtocolUrl = /(^|["'(]|<|\s)(www\..+?\..+?)(([:?]|\.+)?(\s|$)|>|[)"',])/g,
httpOrMailtoUrl = /(^|["'(]|<|\s)(((https?|ftp):\/\/|mailto:).+?)(([:?]|\.+)?(\s|$)|>|[)"',])/g,
linkifier = function ( html ) {
return html
.replace( noProtocolUrl, '$1<a href="<``>://$2">$2</a>$3' ) // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
.replace( httpOrMailtoUrl, '$1<a href="$2">$2</a>$5' )
.replace( /"<``>/g, '"http' ); // reinsert `"http`
},
linkify = $.fn.linkify = function ( cfg ) {
cfg = cfg || {};
if ( typeof cfg == 'string' )
{
cfg = { use:cfg };
}
var use = cfg.use,
allPlugins = linkify.plugins || {},
plugins = [linkifier],
tmpCont;
if ( use )
{
use = $.isArray( use ) ? use : $.trim(use).split( / *, */ );
var plugin,
name;
for ( var i=0, l=use.length; i<l; i++ )
{
name = use[i];
plugin = allPlugins[name];
if ( plugin )
{
plugins.push( plugin );
}
}
}
else
{
for ( var name in allPlugins )
{
plugins.push( allPlugins[name] );
}
}
return this.each(function () {
var childNodes = this.childNodes,
i = childNodes.length;
while ( i-- )
{
var n = childNodes[i];
if ( n.nodeType == 3 )
{
var html = n.nodeValue;
if ( html.length>1 && /\S/.test(html) )
{
var htmlChanged,
preHtml;
tmpCont = tmpCont || $('<div/>')[0];
tmpCont.innerHTML = '';
tmpCont.appendChild( n.cloneNode(false) );
var tmpContNodes = tmpCont.childNodes;
for (var j=0, plugin; (plugin = plugins[j]); j++)
{
var k = tmpContNodes.length,
tmpNode;
while ( k-- )
{
tmpNode = tmpContNodes[k];
if ( tmpNode.nodeType == 3 )
{
html = tmpNode.nodeValue;
if ( html.length>1 && /\S/.test(html) )
{
preHtml = html;
html = html
.replace( /&/g, '&' )
.replace( /</g, '<' )
.replace( />/g, '>' );
html = $.isFunction( plugin ) ?
plugin( html ):
html.replace( plugin.re, plugin.tmpl );
htmlChanged = htmlChanged || preHtml!=html;
preHtml!=html && $(tmpNode).after(html).remove();
}
}
}
}
html = tmpCont.innerHTML;
htmlChanged && $(n).after(html).remove();
}
}
else if ( n.nodeType == 1 && !/^(a|button|textarea)$/i.test(n.tagName) )
{
arguments.callee.call( n );
}
};
});
};
linkify.plugins = {};
})(jQuery);