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