diff --git a/jquery.linkify.js b/jquery.linkify.js index a148e48..0b98685 100644 --- a/jquery.linkify.js +++ b/jquery.linkify.js @@ -3,22 +3,15 @@ function linkify(string, buildHashtagUrl, includeW3, target, noFollow) { if (noFollow) { relNoFollow = " rel=\"nofollow\""; } - - string = string.replace(/((http|https|ftp)\:\/\/|\bw{3}\.)[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z\u00C0-\u017F0-9\-\._\?\,\'\/\\\+&%\$#\=~])*/gi, function(captured) { - var uri; - if (captured.toLowerCase().indexOf("www.") == 0) { - if (!includeW3) { - return captured; - } - uri = "http://" + captured; - } else { - uri = captured; - } - return "" + captured + ""; - }); - + + if (string.toLowerCase().indexOf("www.") === 0 && includeW3) { + string = '' + string + ''; + } else { + string = '' + string + ''; + } + if (buildHashtagUrl) { - string = string.replace(/\B#(\w+)/g, "#$1"); + string = string.replace(/\B#(\w+)/g, '#$1'); } return string; } @@ -26,11 +19,17 @@ function linkify(string, buildHashtagUrl, includeW3, target, noFollow) { (function($) { $.fn.linkify = function(opts) { return this.each(function() { - var $this = $(this); var buildHashtagUrl; var includeW3 = true; var target = '_self'; var noFollow = true; + var regex = /((http|https|ftp)\:\/\/|\bw{3}\.)[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z\u00C0-\u017F0-9\-\._\?\,\'\/\\\+&%\$#\=~])*/gi; + var txt = this.innerHTML; + var output = ''; + var replacement; + var matchLen; + var lastIndex = 0; + if (opts) { if (typeof opts == "function") { buildHashtagUrl = opts; @@ -49,18 +48,20 @@ function linkify(string, buildHashtagUrl, includeW3, target, noFollow) { } } } - $this.html( - $.map( - $this.contents(), - function(n, i) { - if (n.nodeType == 3) { - return linkify(n.data, buildHashtagUrl, includeW3, target, noFollow); - } else { - return n.outerHTML; - } - } - ).join("") - ); + + while ((match = regex.exec(txt)) !== null) { + matchLen = match[0].length; + replacement = linkify(match[0], buildHashtagUrl, includeW3, target, noFollow); + output += txt.substring(lastIndex, match.index + matchLen).replace(match[0], replacement); + lastIndex = match.index + matchLen; + } + + // Include the rest of the text. + if (lastIndex !== txt.length) { + output += txt.substring(lastIndex); + } + + $(this).html(output); }); - } -})(jQuery); \ No newline at end of file + }; +})(jQuery); diff --git a/jquery.linkify.min.js b/jquery.linkify.min.js index b9f7b11..8fc927b 100644 --- a/jquery.linkify.min.js +++ b/jquery.linkify.min.js @@ -1,4 +1 @@ -function linkify(string,buildHashtagUrl,includeW3,target,noFollow){relNoFollow="";if(noFollow)relNoFollow=' rel="nofollow"';string=string.replace(/((http|https|ftp)\:\/\/|\bw{3}\.)[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z\u00C0-\u017F0-9\-\._\?\,\'\/\\\+&%\$#\=~])*/gi,function(captured){var uri;if(captured.toLowerCase().indexOf("www.")==0){if(!includeW3)return captured;uri="http://"+captured}else uri=captured;return'"+captured+""}); -if(buildHashtagUrl)string=string.replace(/\B#(\w+)/g,"#$1");return string} -(function($){$.fn.linkify=function(opts){return this.each(function(){var $this=$(this);var buildHashtagUrl;var includeW3=true;var target="_self";var noFollow=true;if(opts)if(typeof opts=="function")buildHashtagUrl=opts;else{if(typeof opts.hashtagUrlBuilder=="function")buildHashtagUrl=opts.hashtagUrlBuilder;if(typeof opts.includeW3=="boolean")includeW3=opts.includeW3;if(typeof opts.target=="string")target=opts.target;if(typeof opts.noFollow=="boolean")noFollow=opts.noFollow}$this.html($.map($this.contents(), -function(n,i){if(n.nodeType==3)return linkify(n.data,buildHashtagUrl,includeW3,target,noFollow);else return n.outerHTML}).join(""))})}})(jQuery); \ No newline at end of file +function linkify(t,e,o,l,n){return relNoFollow="",n&&(relNoFollow=' rel="nofollow"'),t=0===t.toLowerCase().indexOf("www.")&&o?'"+t+"":'"+t+"",e&&(t=t.replace(/\B#(\w+)/g,"#$1")),t}!function(t){t.fn.linkify=function(e){return this.each(function(){var o,l,n,a=!0,r="_self",i=!0,f=/((http|https|ftp)\:\/\/|\bw{3}\.)[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z\u00C0-\u017F0-9\-\._\?\,\'\/\\\+&%\$#\=~])*/gi,h=this.innerHTML,c="",u=0;for(e&&("function"==typeof e?o=e:("function"==typeof e.hashtagUrlBuilder&&(o=e.hashtagUrlBuilder),"boolean"==typeof e.includeW3&&(a=e.includeW3),"string"==typeof e.target&&(r=e.target),"boolean"==typeof e.noFollow&&(i=e.noFollow)));null!==(match=f.exec(h));)n=match[0].length,l=linkify(match[0],o,a,r,i),c+=h.substring(u,match.index+n).replace(match[0],l),u=match.index+n;u!==h.length&&(c+=h.substring(u)),t(this).html(c)})}}(jQuery);