~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/irc/xul/lib/munger.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is ChatZilla
 
14
 *
 
15
 * The Initial Developer of the Original Code is New Dimensions Consulting,
 
16
 * Inc. Portions created by New Dimensions Consulting, Inc. are
 
17
 * Copyright (C) 1999 New Dimenstions Consulting, Inc. All
 
18
 * Rights Reserved.
 
19
 *
 
20
 * Contributor(s):
 
21
 *  Robert Ginda, rginda@ndcico.com, original author
 
22
 *  Samuel Sieb, samuel@sieb.net, MIRC color codes
 
23
 */
 
24
 
 
25
function initMunger()
 
26
{
 
27
    client.linkRE =
 
28
        /((\w[\w-]+):[^<>\[\]()\'\"\s\u201d]+|www(\.[^.<>\[\]()\'\"\s\u201d]+){2,})/;    
 
29
 
 
30
    var munger = client.munger = new CMunger();
 
31
    munger.addRule ("quote", /(``|'')/, insertQuote);
 
32
    munger.addRule ("bold", /(?:\s|^)(\*[^*()]*\*)(?:[\s.,]|$)/, 
 
33
                    "chatzilla-bold");
 
34
    munger.addRule ("underline", /(?:\s|^)(\_[^_()]*\_)(?:[\s.,]|$)/,
 
35
                    "chatzilla-underline");
 
36
    munger.addRule ("italic", /(?:\s|^)(\/[^\/()]*\/)(?:[\s.,]|$)/,
 
37
                    "chatzilla-italic");
 
38
    /* allow () chars inside |code()| blocks */
 
39
    munger.addRule ("teletype", /(?:\s|^)(\|[^|]*\|)(?:[\s.,]|$)/,
 
40
                    "chatzilla-teletype");
 
41
    munger.addRule (".mirc-colors", /(\x03((\d{1,2})(,\d{1,2}|)|))/,
 
42
                    mircChangeColor);
 
43
    munger.addRule (".mirc-bold", /(\x02)/, mircToggleBold);
 
44
    munger.addRule (".mirc-underline", /(\x1f)/, mircToggleUnder);
 
45
    munger.addRule (".mirc-color-reset", /(\x0f)/, mircResetColor);
 
46
    munger.addRule (".mirc-reverse", /(\x16)/, mircReverseColor);
 
47
    munger.addRule ("ctrl-char", /([\x01-\x1f])/, showCtrlChar);
 
48
    munger.addRule ("link", client.linkRE, insertLink);
 
49
    munger.addRule ("mailto",
 
50
       /(?:\s|\W|^)((mailto:)?[^<>\[\]()\'\"\s\u201d]+@[^.<>\[\]()\'\"\s\u201d]+\.[^<>\[\]()\'\"\s\u201d]+)/i,
 
51
                    insertMailToLink);
 
52
    munger.addRule ("bugzilla-link", /(?:\s|\W|^)(bug\s+#?\d{3,6})/i,
 
53
                    insertBugzillaLink);
 
54
    munger.addRule ("channel-link",
 
55
                /(?:\s|\W|^)[@+]?(#[^<>\[\](){}\"\s\u201d]*[^:,.<>\[\](){}\'\"\s\u201d])/i,
 
56
                    insertChannelLink);
 
57
    
 
58
    munger.addRule ("face",
 
59
         /((^|\s)[\<\>]?[\;\=\:]\~?[\-\^\v]?[\)\|\(pP\<\>oO0\[\]\/\\](\s|$))/,
 
60
         insertSmiley);
 
61
    munger.addRule ("ear", /(?:\s|^)(\(\*)(?:\s|$)/, insertEar, false);
 
62
    munger.addRule ("rheet", /(?:\s|\W|^)(rhee+t\!*)(?:\s|$)/i, insertRheet);
 
63
    munger.addRule ("word-hyphenator",
 
64
                    new RegExp ("(\\S{" + client.MAX_WORD_DISPLAY + ",})"),
 
65
                    insertHyphenatedWord);
 
66
 
 
67
    client.enableColors = client.prefs["munger.colorCodes"];
 
68
    for (var entry in client.munger.entries)
 
69
    {
 
70
        var branch = client.prefManager.prefBranch;
 
71
        if (entry[0] != ".")
 
72
        {
 
73
            try
 
74
            {
 
75
                munger.entries[entry].enabled = 
 
76
                    branch.getBoolPref("munger." + entry);
 
77
            }
 
78
            catch (ex)
 
79
            {
 
80
                // nada
 
81
            }
 
82
        }
 
83
    }
 
84
}
 
85
 
 
86
function CMungerEntry (name, regex, className, enable, tagName)
 
87
{
 
88
    this.name = name;
 
89
    if (name[0] != ".")
 
90
        this.description = getMsg("munger." + name, null, null);
 
91
    this.enabled = (typeof enable == "undefined" ? true : enable);
 
92
    this.enabledDefault = this.enabled;
 
93
    this.tagName = (tagName) ? tagName : "html:span";
 
94
 
 
95
    if (regex instanceof RegExp)
 
96
        this.regex = regex;
 
97
    else
 
98
        this.lambdaMatch = regex;
 
99
    
 
100
    if (typeof className == "function")
 
101
        this.lambdaReplace = className;
 
102
    else 
 
103
        this.className = className;
 
104
}
 
105
 
 
106
function CMunger () 
 
107
{
 
108
    this.entries = new Object();
 
109
    this.tagName = "html:span";
 
110
    this.enabled = true;
 
111
}
 
112
 
 
113
CMunger.prototype.enabled = true;
 
114
 
 
115
CMunger.prototype.addRule =
 
116
function mng_addrule (name, regex, className, enable)
 
117
{
 
118
    this.entries[name] = new CMungerEntry (name, regex, className, enable);
 
119
}
 
120
 
 
121
CMunger.prototype.delRule =
 
122
function mng_delrule (name)
 
123
{
 
124
    delete this.entries[name];
 
125
}
 
126
 
 
127
CMunger.prototype.munge =
 
128
function mng_munge (text, containerTag, data)
 
129
{
 
130
    var entry;
 
131
    var ary;
 
132
    var wbr, newClass;
 
133
    
 
134
    if (!containerTag)
 
135
    {
 
136
        containerTag =
 
137
            document.createElementNS ("http://www.w3.org/1999/xhtml",
 
138
                                      this.tagName);
 
139
    }
 
140
 
 
141
    if (this.enabled)
 
142
    {
 
143
        for (entry in this.entries)
 
144
        {
 
145
            if (this.entries[entry].enabled)
 
146
            {
 
147
                if (typeof this.entries[entry].lambdaMatch == "function")
 
148
                {
 
149
                    var rval;
 
150
 
 
151
                    rval = this.entries[entry].lambdaMatch(text, containerTag,
 
152
                                                           data,
 
153
                                                           this.entries[entry]);
 
154
                    if (rval)
 
155
                        ary = [(void 0), rval];
 
156
                    else
 
157
                        ary = null;
 
158
                }
 
159
                else
 
160
                    ary = text.match(this.entries[entry].regex);
 
161
 
 
162
                if ((ary != null) && (ary[1]))
 
163
                {
 
164
                    var startPos = text.indexOf(ary[1]);
 
165
 
 
166
                    if (typeof this.entries[entry].lambdaReplace == "function")
 
167
                    {
 
168
                        this.munge (text.substr(0,startPos), containerTag,
 
169
                                    data);
 
170
                        this.entries[entry].lambdaReplace (ary[1], containerTag,
 
171
                                                           data,
 
172
                                                           this.entries[entry]);
 
173
                        this.munge (text.substr (startPos + ary[1].length,
 
174
                                                 text.length), containerTag,
 
175
                                    data);
 
176
 
 
177
                        return containerTag;
 
178
                    }
 
179
                    else
 
180
                    {
 
181
                        this.munge (text.substr(0,startPos), containerTag,
 
182
                                    data);
 
183
 
 
184
                        var subTag = document.createElementNS
 
185
                            ("http://www.w3.org/1999/xhtml",
 
186
                             this.entries[entry].tagName);
 
187
 
 
188
                        newClass = this.entries[entry].className;
 
189
 
 
190
                        if ("hasColorInfo" in data)
 
191
                        {
 
192
                            if ("currFgColor" in data)
 
193
                                newClass += " chatzilla-fg" + data.currFgColor;
 
194
                            if ("currBgColor" in data)
 
195
                                newClass += " chatzilla-bg" + data.currBgColor;
 
196
                            if ("isBold" in data)
 
197
                                newClass += " chatzilla-bold";
 
198
                            if ("isUnderline" in data)
 
199
                                newClass += " chatzilla-underline";
 
200
                        }
 
201
 
 
202
                        subTag.setAttribute ("class", newClass);
 
203
 
 
204
                        /* don't let this rule match again */
 
205
                        this.entries[entry].enabled = false;
 
206
                        this.munge(ary[1], subTag, data);
 
207
                        this.entries[entry].enabled = true;
 
208
 
 
209
                        containerTag.appendChild (subTag);
 
210
 
 
211
                        this.munge (text.substr (startPos + ary[1].length,
 
212
                                                 text.length), containerTag,
 
213
                                                 data);
 
214
 
 
215
                        return containerTag;
 
216
                    }
 
217
                }
 
218
            }
 
219
        }
 
220
    }
 
221
 
 
222
    var textNode = document.createTextNode (text);
 
223
 
 
224
    if ("hasColorInfo" in data)
 
225
    {
 
226
 
 
227
        newClass = "";
 
228
        if ("currFgColor" in data)
 
229
            newClass = "chatzilla-fg" + data.currFgColor;
 
230
        if ("currBgColor" in data)
 
231
            newClass += " chatzilla-bg" + data.currBgColor;
 
232
        if ("isBold" in data)
 
233
            newClass += " chatzilla-bold";
 
234
        if ("isUnderline" in data)
 
235
            newClass += " chatzilla-underline";
 
236
        if (newClass != "")
 
237
        {
 
238
            var newTag = document.createElementNS
 
239
                ("http://www.w3.org/1999/xhtml",
 
240
                 "html:span");
 
241
            newTag.setAttribute ("class", newClass);
 
242
            newTag.appendChild (textNode);
 
243
            containerTag.appendChild (newTag);
 
244
        }
 
245
        else
 
246
        {
 
247
            delete data.hasColorInfo;
 
248
            containerTag.appendChild (textNode);
 
249
        }
 
250
        wbr = document.createElementNS ("http://www.w3.org/1999/xhtml",
 
251
                                        "html:wbr");
 
252
        containerTag.appendChild (wbr);
 
253
    }
 
254
    else
 
255
        containerTag.appendChild (textNode);
 
256
 
 
257
    return containerTag;
 
258
}