Created
January 15, 2009 08:21
-
-
Save higeorange/47317 to your computer and use it in GitHub Desktop.
jQuery AutoLink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
jQuery AutoLink | |
MIT License | |
*/ | |
(function($) { | |
var url_regexp = /(https?:\/\/[ -熙ぁ-ヴーA-Za-z0-9~\/._\?\&=\-%#\+:\;,\@\']+)/; | |
$.fn.autolink = function() { | |
return this.each(function(){ | |
var desc = $(this); | |
desc.textNodes().each(function(){ | |
var text = $(this); | |
if(text.parent().get(0).nodeName != 'A') { | |
text.replaceWith(this.data.replace(url_regexp, function($0, $1) { | |
return '<a href="' + $1 +'">' + $1 + '</a>'; | |
})); | |
} | |
}); | |
}); | |
} | |
$.fn.textNodes = function() { | |
var ret = []; | |
(function(el){ | |
if (!el) return; | |
if ((el.nodeType == 3)) { | |
ret.push(el); | |
} else { | |
for (var i=0; i < el.childNodes.length; ++i) { | |
arguments.callee(el.childNodes[i]); | |
} | |
})(this[0]); | |
return $(ret); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment