]>
Commit | Line | Data |
---|---|---|
83f9b8b2 CP |
1 | var url_re = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/; |
2 | ||
3 | function urlificate(element, text) { | |
4 | function appendText(text) { | |
5 | element.appendChild(document.createTextNode(text)); | |
6 | } | |
7 | function appendA(text) { | |
8 | var a = document.createElement("a"); | |
9 | a.href = text; | |
10 | a.target = "new"; | |
11 | a.appendChild(document.createTextNode(text)); | |
12 | ||
13 | element.appendChild(a); | |
14 | } | |
15 | ||
16 | for(;;) { | |
17 | var index = text.search(url_re); | |
18 | if(index == -1) { | |
19 | appendText(text); | |
20 | break; | |
21 | } | |
22 | var match = text.match(url_re); | |
23 | ||
24 | before = text.substring(0, index); | |
25 | matched = match[0]; | |
26 | after = text.substring(index + matched.length); | |
27 | text = after; | |
28 | appendText(before); | |
29 | appendA(matched, before); | |
30 | } | |
31 | } |