home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / chatzilla.jar / content / chatzilla / lib / xul / munger.js
Encoding:
JavaScript  |  2002-04-09  |  6.8 KB  |  206 lines

  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 CMungerEntry (name, regex, className, enable, tagName)
  26. {
  27.     
  28.     this.name = name;
  29.     if (name[0] != ".")
  30.         this.description = getMsg("rule_" + name);
  31.     this.enabled = (typeof enable == "undefined" ? true : enable);
  32.     this.tagName = (tagName) ? tagName : "html:span";
  33.  
  34.     if (regex instanceof RegExp)
  35.         this.regex = regex;
  36.     else
  37.         this.lambdaMatch = regex;
  38.     
  39.     if (typeof className == "function")
  40.         this.lambdaReplace = className;
  41.     else 
  42.         this.className = className;
  43.     
  44. }
  45.  
  46. function CMunger () 
  47. {
  48.     
  49.     this.entries = new Object();
  50.     
  51. }
  52.  
  53. CMunger.prototype.enabled = true;
  54.  
  55. CMunger.prototype.addRule =
  56. function mng_addrule (name, regex, className, enable)
  57. {
  58.     
  59.     this.entries[name] = new CMungerEntry (name, regex, className, enable);
  60.     
  61. }
  62.  
  63. CMunger.prototype.delRule =
  64. function mng_delrule (name)
  65. {
  66.  
  67.     delete this.entries[name];
  68.     
  69. }
  70.  
  71. CMunger.prototype.munge =
  72. function mng_munge (text, containerTag, data)
  73. {
  74.     var entry;
  75.     var ary;
  76.     var wbr, newClass;
  77.     
  78.     if (!containerTag)
  79.         containerTag =
  80.             document.createElementNS ("http://www.w3.org/1999/xhtml",
  81.                                       this.tagName);
  82.  
  83.     if (this.enabled)
  84.     {
  85.         for (entry in this.entries)
  86.         {
  87.             if (this.entries[entry].enabled)
  88.             {
  89.                 if (typeof this.entries[entry].lambdaMatch == "function")
  90.                 {
  91.                     var rval;
  92.  
  93.                     rval = this.entries[entry].lambdaMatch(text, containerTag,
  94.                                                            data,
  95.                                                            this.entries[entry]);
  96.                     if (rval)
  97.                         ary = [(void 0), rval];
  98.                     else
  99.                         ary = null;
  100.                 }
  101.                 else
  102.                     ary = text.match(this.entries[entry].regex);
  103.  
  104.                 if ((ary != null) && (ary[1]))
  105.                 {
  106.                     var startPos = text.indexOf(ary[1]);
  107.  
  108.                     if (typeof this.entries[entry].lambdaReplace == "function")
  109.                     {
  110.                         this.munge (text.substr(0,startPos), containerTag,
  111.                                     data);
  112.                         this.entries[entry].lambdaReplace (ary[1], containerTag,
  113.                                                            data,
  114.                                                            this.entries[entry]);
  115.                         this.munge (text.substr (startPos + ary[1].length,
  116.                                                  text.length), containerTag,
  117.                                     data);
  118.  
  119.                         return containerTag;
  120.                     }
  121.                     else
  122.                     {
  123.                         this.munge (text.substr(0,startPos), containerTag,
  124.                                     data);
  125.  
  126.                         var subTag = document.createElementNS
  127.                             ("http://www.w3.org/1999/xhtml",
  128.                              this.entries[entry].tagName);
  129.  
  130.                         newClass = this.entries[entry].className;
  131.  
  132.                         if ("hasColorInfo" in data)
  133.                         {
  134.                             if ("currFgColor" in data)
  135.                                 newClass += " chatzilla-fg" + data.currFgColor;
  136.                             if ("currBgColor" in data)
  137.                                 newClass += " chatzilla-bg" + data.currBgColor;
  138.                             if ("isBold" in data)
  139.                                 newClass += " chatzilla-bold";
  140.                             if ("isUnderline" in data)
  141.                                 newClass += " chatzilla-underline";
  142.                         }
  143.  
  144.                         subTag.setAttribute ("class", newClass);
  145.  
  146.                         var wordParts = splitLongWord (ary[1],
  147.                                                        client.MAX_WORD_DISPLAY);
  148.                         for (var i in wordParts)
  149.                         {
  150.                             subTag.appendChild (document.createTextNode (wordParts[i]));
  151.                             wbr = document.createElementNS ("http://www.w3.org/1999/xhtml",
  152.                                                             "html:wbr");
  153.                             subTag.appendChild (wbr);
  154.                         }
  155.  
  156.                         containerTag.appendChild (subTag);
  157.                         this.munge (text.substr (startPos + ary[1].length,
  158.                                                  text.length), containerTag,
  159.                                                  data);
  160.  
  161.                         return containerTag;
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.     }
  167.  
  168.     var textNode = document.createTextNode (text);
  169.  
  170.     if ("hasColorInfo" in data)
  171.     {
  172.  
  173.         newClass = "";
  174.         if ("currFgColor" in data)
  175.             newClass = "chatzilla-fg" + data.currFgColor;
  176.         if ("currBgColor" in data)
  177.             newClass += " chatzilla-bg" + data.currBgColor;
  178.         if ("isBold" in data)
  179.             newClass += " chatzilla-bold";
  180.         if ("isUnderline" in data)
  181.             newClass += " chatzilla-underline";
  182.         if (newClass != "")
  183.         {
  184.             var newTag = document.createElementNS
  185.                 ("http://www.w3.org/1999/xhtml",
  186.                  "html:span");
  187.             newTag.setAttribute ("class", newClass);
  188.             newTag.appendChild (textNode);
  189.             containerTag.appendChild (newTag);
  190.         }
  191.         else
  192.         {
  193.             delete data.hasColorInfo;
  194.             containerTag.appendChild (textNode);
  195.         }
  196.         wbr = document.createElementNS ("http://www.w3.org/1999/xhtml",
  197.                                         "html:wbr");
  198.         containerTag.appendChild (wbr);
  199.     }
  200.     else
  201.         containerTag.appendChild (textNode);
  202.  
  203.     return containerTag;
  204.     
  205. }
  206.