blob: 2993954ab63b8ab53221f2a89b76e3dd6c1fdcee (
plain)
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
|
(function () {
var re = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+:=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;
function textNodesUnder(node) {
var textNodes = [];
if(typeof document.createTreeWalker === 'function') {
// Efficient TreeWalker
var currentNode, walker;
walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false);
while(currentNode = walker.nextNode()) {
textNodes.push(currentNode);
}
} else {
// Less efficient recursive function
for(node = node.firstChild; node; node = node.nextSibling) {
if(node.nodeType === 3) {
textNodes.push(node);
} else {
textNodes = textNodes.concat(textNodesUnder(node));
}
}
}
return textNodes;
}
function processNode(node) {
re.lastIndex = 0;
var results = re.exec(node.textContent);
if(results !== null) {
if($(node).parents().filter('code').length === 0) {
$(node).replaceWith(
$('<span />').html(
node.nodeValue.replace(re, '<a href="$1">$1</a>')
)
);
}
}
}
jQuery.fn.autolink = function () {
this.each(function () {
textNodesUnder(this).forEach(processNode);
});
return this;
};
})();
|