home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-mac-0.9.sea.hqx / mozilla-mac-0.9 / Chrome / chatzilla.jar / content / chatzilla / lib / xul / munger.js < prev   
Text File  |  2001-05-05  |  5KB  |  149 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.  */
  23.  
  24. function CMungerEntry (name, regex, className, tagName)
  25. {
  26.     
  27.     this.name = name;
  28.     this.tagName = (tagName) ? tagName : "html:span";
  29.  
  30.     if (regex instanceof RegExp)
  31.         this.regex = regex;
  32.     else
  33.         this.lambdaMatch = regex;
  34.     
  35.     if (typeof className == "function")
  36.         this.lambdaReplace = className;
  37.     else 
  38.         this.className = className;
  39.     
  40. }
  41.  
  42. function CMunger () 
  43. {
  44.     
  45.     this.entries = new Object();
  46.     
  47. }
  48.  
  49. CMunger.prototype.enabled = true;
  50.  
  51. CMunger.prototype.addRule =
  52. function mng_addrule (name, regex, className)
  53. {
  54.     
  55.     this.entries[name] = new CMungerEntry (name, regex, className);
  56.     
  57. }
  58.  
  59. CMunger.prototype.delRule =
  60. function mng_delrule (name)
  61. {
  62.  
  63.     delete this.entries[name];
  64.     
  65. }
  66.  
  67. CMunger.prototype.munge =
  68. function mng_munge (text, containerTag, data)
  69. {
  70.     var entry;
  71.     var ary;
  72.     
  73.     if (!containerTag)
  74.         containerTag =
  75.             document.createElementNS ("http://www.w3.org/1999/xhtml",
  76.                                       this.tagName);
  77.  
  78.     if (this.enabled)
  79.     {
  80.         for (entry in this.entries)
  81.         {
  82.             if (typeof this.entries[entry].lambdaMatch == "function")
  83.             {
  84.                 var rval;
  85.                 
  86.                 rval = this.entries[entry].lambdaMatch(text, containerTag,
  87.                                                        data,
  88.                                                        this.entries[entry]);
  89.                 if (rval)
  90.                     ary = [(void 0), rval];
  91.                 else
  92.                     ary = null;
  93.             }
  94.             else
  95.                 ary = text.match(this.entries[entry].regex);
  96.             
  97.             if ((ary != null) && (ary[1]))
  98.             {
  99.                 var startPos = text.indexOf(ary[1]);
  100.                 
  101.                 if (typeof this.entries[entry].lambdaReplace == "function")
  102.                 {
  103.                     this.munge (text.substr(0,startPos), containerTag,
  104.                                 data);
  105.                     this.entries[entry].lambdaReplace (ary[1], containerTag,
  106.                                                        data,
  107.                                                        this.entries[entry]);
  108.                     this.munge (text.substr (startPos + ary[1].length,
  109.                                              text.length), containerTag,
  110.                                 data);
  111.                 
  112.                     return containerTag;
  113.                 }
  114.                 else
  115.                 {
  116.                     this.munge (text.substr(0,startPos), containerTag,
  117.                                 data);
  118.                     
  119.                     var subTag = document.createElementNS
  120.                         ("http://www.w3.org/1999/xhtml",
  121.                          this.entries[entry].tagName);
  122.  
  123.                     subTag.setAttribute ("class",
  124.                                          this.entries[entry].className);
  125.                     var wordParts = splitLongWord (ary[1],
  126.                                                    client.MAX_WORD_DISPLAY);
  127.                     for (var i in wordParts)
  128.                     {
  129.                         subTag.appendChild (document.createTextNode (wordParts[i]));
  130.                         var img = document.createElementNS ("http://www.w3.org/1999/xhtml",
  131.                                                             "html:img");
  132.                         subTag.appendChild (img);
  133.                     }
  134.                     
  135.                     containerTag.appendChild (subTag);
  136.                     this.munge (text.substr (startPos + ary[1].length,
  137.                                              text.length), containerTag, data);
  138.  
  139.                     return containerTag;
  140.                 }
  141.             }
  142.         }
  143.     }
  144.  
  145.     containerTag.appendChild (document.createTextNode (text));
  146.     return containerTag;
  147.     
  148. }
  149.