]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/url.js
Better URL/channel parsing.
[irc/quakenet/qwebirc.git] / js / ui / url.js
1 function urlificate(element, text, execfn) {
2 var punct_re = /(\.*|\,|;)$/;
3
4 var txtprocess = function(text, regex, appendfn, matchfn) {
5 for(;;) {
6 var index = text.search(regex);
7 if(index == -1) {
8 appendfn(text);
9 break;
10 }
11 var match = text.match(regex);
12
13 var before = text.substring(0, index);
14 var matched = match[0];
15 var after = text.substring(index + matched.length);
16
17 appendfn(before);
18 var more = matchfn(matched);
19 if(!more)
20 more = "";
21 text = more + after;
22 }
23 };
24
25 var appendText = function(text) {
26 element.appendChild(document.createTextNode(text));
27 };
28
29 var appendChan = function(text) {
30 var newtext = text.replace(punct_re, "");
31 var punct = text.substring(newtext.length);
32
33 var a = document.createElement("a");
34 a.href = "#";
35 a.addEvent("click", function(e) {
36 new Event(e).stop();
37 execfn("/JOIN " + newtext);
38 });
39 a.appendChild(document.createTextNode(newtext));
40 element.appendChild(a);
41
42 return punct;
43 };
44
45 var appendURL = function(text) {
46 var newtext = text.replace(punct_re, "");
47 var punct = text.substring(newtext.length);
48
49 var a = document.createElement("a");
50 a.href = newtext;
51 a.target = "new";
52 a.appendChild(document.createTextNode(newtext));
53 element.appendChild(a);
54
55 return punct;
56 };
57
58 txtprocess(text, /\bhttp:\/\/[^ ]+/, function(text) {
59 txtprocess(text, /\B#[^ ,]+/, appendText, appendChan);
60 }, appendURL);
61 }