home *** CD-ROM | disk | FTP | other *** search
/ Thomson (Residential) / TGSTPv7203.iso / mac / Documentation / HTML / TG780-BUS_it / wwhelp / wwhimpl / common / scripts / strutils.js < prev    next >
Text File  |  2007-06-22  |  12KB  |  464 lines

  1. // Copyright (c) 2000-2001 Quadralay Corporation.  All rights reserved.
  2. //
  3.  
  4. function  WWHStringUtilities_SearchReplace(ParamString,
  5.                                            ParamSearchString,
  6.                                            ParamReplaceString)
  7. {
  8.   var  ResultString;
  9.   var  Index;
  10.  
  11.  
  12.   ResultString = ParamString;
  13.  
  14.   if ((ParamSearchString.length > 0) &&
  15.       (ResultString.length > 0))
  16.   {
  17.     Index = 0;
  18.     while ((Index = ResultString.indexOf(ParamSearchString, Index)) != -1)
  19.     {
  20.       ResultString = ResultString.substring(0, Index) + ParamReplaceString + ResultString.substring(Index + ParamSearchString.length, ResultString.length);
  21.       Index += ParamReplaceString.length;
  22.     }
  23.   }
  24.  
  25.   return ResultString;
  26. }
  27.  
  28. function  WWHStringUtilities_EscapeHTML(ParamHTML)
  29. {
  30.   var  EscapedHTML = ParamHTML;
  31.  
  32.  
  33.   // Escape problematic characters
  34.   // & < > "
  35.   //
  36.   EscapedHTML = WWHStringUtilities_SearchReplace(EscapedHTML, "&", "&");
  37.   EscapedHTML = WWHStringUtilities_SearchReplace(EscapedHTML, "<", "<");
  38.   EscapedHTML = WWHStringUtilities_SearchReplace(EscapedHTML, ">", ">");
  39.   EscapedHTML = WWHStringUtilities_SearchReplace(EscapedHTML, "\"", """);
  40.  
  41.   return EscapedHTML;
  42. }
  43.  
  44. function  WWHStringUtilities_UnescapeHTML(ParamHTML)
  45. {
  46.   var  Text = ParamHTML;
  47.   var  EscapedExpression;
  48.   var  EscapedCharacter;
  49.   var  CharacterCode;
  50.   var  JavaScriptCharacter;
  51.  
  52.  
  53.   // Unescape problematic characters
  54.   //
  55.   // & < > "
  56.   //
  57.   Text = WWHStringUtilities_SearchReplace(Text, "&", "&");
  58.   Text = WWHStringUtilities_SearchReplace(Text, "<", "<");
  59.   Text = WWHStringUtilities_SearchReplace(Text, ">", ">");
  60.   Text = WWHStringUtilities_SearchReplace(Text, """, "\"");
  61.  
  62.   // If any still exist, replace them with normal character
  63.   //
  64.   if (Text.indexOf("&#") != -1)
  65.   {
  66.     EscapedExpression = new RegExp("&#([0-9]+);");
  67.  
  68.     while (EscapedExpression.test(Text))
  69.     {
  70.       EscapedCharacter = EscapedExpression.lastMatch();
  71.       CharacterCode = parseInt(EscapedExpression.$1);
  72.  
  73.       // Turn character code into escaped JavaScript character
  74.       //
  75.       JavaScriptCharacter = eval("\\u" + WWHStringUtilities_DecimalToHex(CharacterCode));
  76.  
  77.       // Replace in string
  78.       //
  79.       Text = WWHStringUtilities_SearchReplace(Text, EscapedCharacter, JavaScriptCharacter);
  80.     }
  81.   }
  82.  
  83.   return Text;
  84. }
  85.  
  86. function  WWHStringUtilities_DecimalToHex(ParamNumber)
  87. {
  88.   var  HexNumber = "";
  89.  
  90.  
  91.   HexNumber += WWHStringUtilities_HexDigit(ParamNumber >> 12);
  92.   HexNumber += WWHStringUtilities_HexDigit(ParamNumber >>  8);
  93.   HexNumber += WWHStringUtilities_HexDigit(ParamNumber >>  4);
  94.   HexNumber += WWHStringUtilities_HexDigit(ParamNumber >>  0);
  95.  
  96.   return HexNumber;
  97. }
  98.  
  99. function  WWHStringUtilities_HexDigit(ParamDigit)
  100. {
  101.   var  HexDigit;
  102.   var  MaskedDigit = ParamDigit & 0x0F;
  103.  
  104.  
  105.   // Translate to hex characters 'a' - 'f' if necessary
  106.   //
  107.   if (MaskedDigit == 10)
  108.   {
  109.     HexDigit = "a";
  110.   }
  111.   else if (MaskedDigit == 11)
  112.   {
  113.     HexDigit = "b";
  114.   }
  115.   else if (MaskedDigit == 12)
  116.   {
  117.     HexDigit = "c";
  118.   }
  119.   else if (MaskedDigit == 13)
  120.   {
  121.     HexDigit = "d";
  122.   }
  123.   else if (MaskedDigit == 14)
  124.   {
  125.     HexDigit = "e";
  126.   }
  127.   else if (MaskedDigit == 15)
  128.   {
  129.     HexDigit = "f";
  130.   }
  131.   else
  132.   {
  133.     HexDigit = MaskedDigit;
  134.   }
  135.  
  136.   return HexDigit;
  137. }
  138.  
  139. function  WWHStringUtilities_NormalizeURL(ParamURL)
  140. {
  141.   var  URL = ParamURL;
  142.  
  143.  
  144.   // Unescape URL for most browsers
  145.   //
  146.   if (WWHFrame.WWHBrowserInfo.mbUnescapeHREFs)
  147.   {
  148.     URL = unescape(URL);
  149.   }
  150.   else  // IE unescapes everything automatically, except &
  151.   {
  152.     URL = WWHStringUtilities_SearchReplace(URL, "%26", "&");
  153.   }
  154.  
  155.   // Standardize protocol case
  156.   //
  157.   if (URL.indexOf(":") != -1)
  158.   {
  159.     var  MaxIndex;
  160.  
  161.  
  162.     Parts = URL.split(":");
  163.  
  164.     URL = Parts[0].toLowerCase();
  165.     for (MaxIndex = Parts.length, Index = 1 ; Index < MaxIndex ; Index++)
  166.     {
  167.       URL += ":" + Parts[Index];
  168.     }
  169.   }
  170.  
  171.   // Handle drive letters under Windows
  172.   //
  173.   if (WWHFrame.WWHBrowserInfo.mPlatform == 1)  // Shorthand for Windows
  174.   {
  175.     var  DrivePattern = new RegExp("^file:[/]+([a-zA-Z])[:\|][/](.*)$", "i");
  176.     var  DrivePatternMatch;
  177.  
  178.  
  179.     DrivePatternMatch = DrivePattern.exec(URL);
  180.     if (DrivePatternMatch != null)
  181.     {
  182.       URL = "file:///" + DrivePatternMatch[1] + ":/" + DrivePatternMatch[2];
  183.     }
  184.   }
  185.  
  186.   return URL;
  187. }
  188.  
  189. function  WWHStringUtilities_RestoreEscapedSpaces(ParamURL)
  190. {
  191.   // Workaround for stupid Netscape 4.x bug
  192.   //
  193.   var  StringWithSpace = "x x";
  194.   var  EscapedURL = ParamURL;
  195.  
  196.  
  197.   if (WWHFrame.WWHBrowserInfo.mbUnescapeHREFs)
  198.   {
  199.     EscapedURL = WWHStringUtilities_SearchReplace(EscapedURL, StringWithSpace.substring(1, 2), "%20");
  200.   }
  201.  
  202.   return EscapedURL;
  203. }
  204.  
  205. function  WWHStringUtilities_EscapeURLForJavaScriptAnchor(ParamURL)
  206. {
  207.   var  EscapedURL = ParamURL;
  208.  
  209.  
  210.   // Escape problematic characters
  211.   // \ " ' < >
  212.   //
  213.   EscapedURL = WWHStringUtilities_SearchReplace(EscapedURL, "\\", "\\\\");
  214.   EscapedURL = WWHStringUtilities_SearchReplace(EscapedURL, "\"", "\\u0022");
  215.   EscapedURL = WWHStringUtilities_SearchReplace(EscapedURL, "'", "\\u0027");
  216.   EscapedURL = WWHStringUtilities_SearchReplace(EscapedURL, "<", "\\u003c");
  217.   EscapedURL = WWHStringUtilities_SearchReplace(EscapedURL, ">", "\\u003e");
  218.  
  219.   return EscapedURL;
  220. }
  221.  
  222. function  WWHStringUtilities_EscapeForJavaScript(ParamString)
  223. {
  224.   var  EscapedString = ParamString;
  225.  
  226.  
  227.   // Escape problematic characters
  228.   // \ " '
  229.   //
  230.   EscapedString = WWHStringUtilities_SearchReplace(EscapedString, "\\", "\\\\");
  231.   EscapedString = WWHStringUtilities_SearchReplace(EscapedString, "\"", "\\u0022");
  232.   EscapedString = WWHStringUtilities_SearchReplace(EscapedString, "'", "\\u0027");
  233.   EscapedString = WWHStringUtilities_SearchReplace(EscapedString, "\n", "\\u000a");
  234.   EscapedString = WWHStringUtilities_SearchReplace(EscapedString, "\r", "\\u000d");
  235.  
  236.   return EscapedString;
  237. }
  238.  
  239. function  WWHStringUtilities_EscapeRegExp(ParamWord)
  240. {
  241.   var  WordRegExpPattern = ParamWord;
  242.  
  243.  
  244.   // Escape special characters
  245.   // \ ( ) [ ] . ? + ^ $
  246.   //
  247.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "\\", "\\\\");
  248.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, ".", "\\.");
  249.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "?", "\\?");
  250.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "+", "\\+");
  251.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "|", "\\|");
  252.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "^", "\\^");
  253.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "$", "\\$");
  254.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "(", "\\(");
  255.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, ")", "\\)");
  256.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "{", "\\{");
  257.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "}", "\\}");
  258.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "[", "\\[");
  259.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "]", "\\]");
  260.  
  261.   // Windows IE 4.0 is brain dead
  262.   //
  263.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "/", "[/]");
  264.  
  265.   // Convert * to .*
  266.   //
  267.   WordRegExpPattern = WWHStringUtilities_SearchReplace(WordRegExpPattern, "*", ".*");
  268.  
  269.   return WordRegExpPattern;
  270. }
  271.  
  272. function  WWHStringUtilities_WordToRegExpPattern(ParamWord)
  273. {
  274.   var  WordRegExpPattern;
  275.  
  276.  
  277.   // Escape special characters
  278.   // Convert * to .*
  279.   //
  280.   WordRegExpPattern = WWHStringUtilities_EscapeRegExp(ParamWord);
  281.  
  282.   // Add ^ and $ to force whole string match
  283.   //
  284.   WordRegExpPattern = "^" + WordRegExpPattern + "$";
  285.  
  286.   return WordRegExpPattern;
  287. }
  288.  
  289. function  WWHStringUtilities_WordToRegExpWithSpacePattern(ParamWord)
  290. {
  291.   var  WordRegExpPattern;
  292.  
  293.  
  294.   // Escape special characters
  295.   // Convert * to .*
  296.   //
  297.   WordRegExpPattern = WWHStringUtilities_EscapeRegExp(ParamWord);
  298.  
  299.   // Add ^ and $ to force whole string match
  300.   // Allow trailing whitespace
  301.   //
  302.   WordRegExpPattern = "^" + WordRegExpPattern + " *$";
  303.  
  304.   return WordRegExpPattern;
  305. }
  306.  
  307. function  WWHStringUtilities_ExtractStyleAttribute(ParamAttribute,
  308.                                                    ParamFontStyle)
  309. {
  310.   var  Attribute = "";
  311.   var  AttributeIndex;
  312.   var  AttributeStart;
  313.  
  314.  
  315.   AttributeIndex = ParamFontStyle.indexOf(ParamAttribute, 0);
  316.   if (AttributeIndex != -1)
  317.   {
  318.     AttributeStart = ParamFontStyle.indexOf(":", AttributeIndex);
  319.  
  320.     if (AttributeStart != -1)
  321.     {
  322.       AttributeStart += 1;
  323.  
  324.       AttributeEnd = ParamFontStyle.indexOf(";", AttributeStart);
  325.       if (AttributeEnd == -1)
  326.       {
  327.         AttributeEnd = ParamFontStyle.length;
  328.       }
  329.  
  330.       Attribute = ParamFontStyle.substring(AttributeStart + 1, AttributeEnd);
  331.     }
  332.   }
  333.  
  334.   return Attribute;
  335. }
  336.  
  337. function  WWHStringUtilities_NormalizeSearchWords(ParamSearchWords)
  338. {
  339.   // Workaround for stupid Netscape 4.x bug
  340.   //
  341.   var  StringWithSpace = "x x";
  342.   var  Parts;
  343.   var  MaxIndex;
  344.   var  Index;
  345.   var  bStripChar;
  346.   var  CharAsInt;
  347.   var  CharIndex;
  348.  
  349.  
  350.   // Handle some common cases
  351.   //
  352.   Parts = ParamSearchWords.split(StringWithSpace.substring(1, 2));
  353.   for (MaxIndex = Parts.length, Index = 0 ; Index < MaxIndex ; Index++)
  354.   {
  355.     // Remove leading characters @ % $
  356.     //
  357.     bStripChar = true;
  358.     while ((bStripChar) &&
  359.            (Parts[Index].length > 0))
  360.     {
  361.       bStripChar = false;
  362.  
  363.       if ((Parts[Index].indexOf("@") == 0) ||
  364.           (Parts[Index].indexOf("%") == 0))
  365.       {
  366.         bStripChar = true;
  367.       }
  368.       else if (Parts[Index].indexOf("$") == 0)
  369.       {
  370.         // Handle $ in front of numbers
  371.         //
  372.         if (Parts[Index].length > 1)
  373.         {
  374.           CharAsInt = parseInt(Parts[Index].substring(1, 2));
  375.           bStripChar = ((CharAsInt >= 0) && (CharAsInt <= 9)) ? false : true;
  376.         }
  377.         else
  378.         {
  379.           bStripChar = true;
  380.         }
  381.       }
  382.  
  383.       if (bStripChar)
  384.       {
  385.         Parts[Index] = Parts[Index].substring(1, Parts[Index].length);
  386.       }
  387.     }
  388.  
  389.     // Remove trailing characters ( ) % $ ; ,
  390.     //
  391.     bStripChar = true;
  392.     while ((bStripChar) &&
  393.            (Parts[Index].length > 0))
  394.     {
  395.       bStripChar = false;
  396.  
  397.       CharIndex = Parts[Index].length - 1;
  398.  
  399.       if ((Parts[Index].lastIndexOf("(") == CharIndex) ||
  400.           (Parts[Index].lastIndexOf(")") == CharIndex) ||
  401.           (Parts[Index].lastIndexOf("%") == CharIndex) ||
  402.           (Parts[Index].lastIndexOf("$") == CharIndex) ||
  403.           (Parts[Index].lastIndexOf(";") == CharIndex) ||
  404.           (Parts[Index].lastIndexOf(",") == CharIndex))
  405.       {
  406.         bStripChar = true;
  407.       }
  408.  
  409.       if (bStripChar)
  410.       {
  411.         Parts[Index] = Parts[Index].substring(0, CharIndex);
  412.       }
  413.     }
  414.  
  415.     if (Parts[Index].length > 0)
  416.     {
  417.       // Replace embedded characters @ : ,
  418.       //
  419.       Parts[Index] = WWHStringUtilities_SearchReplace(Parts[Index], "@", " ");
  420.       Parts[Index] = WWHStringUtilities_SearchReplace(Parts[Index], ":", " ");
  421.       Parts[Index] = WWHStringUtilities_SearchReplace(Parts[Index], ",", " ");
  422.     }
  423.   }
  424.  
  425.   return Parts.join(" ");
  426. }
  427.  
  428. function  WWHStringBuffer_Object()
  429. {
  430.   this.mStringList        = new Array();
  431.   this.mStringListEntries = 0;
  432.   this.mSize              = 0;
  433.  
  434.   this.fSize      = WWHStringBuffer_Size;
  435.   this.fReset     = WWHStringBuffer_Reset;
  436.   this.fAppend    = WWHStringBuffer_Append;
  437.   this.fGetBuffer = WWHStringBuffer_GetBuffer;
  438. }
  439.  
  440. function  WWHStringBuffer_Size()
  441. {
  442.   return this.mSize;
  443. }
  444.  
  445. function  WWHStringBuffer_Reset()
  446. {
  447.   this.mStringListEntries = 0;
  448.   this.mSize              = 0;
  449. }
  450.  
  451. function  WWHStringBuffer_Append(ParamString)
  452. {
  453.   this.mSize += ParamString.length;
  454.   this.mStringList[this.mStringListEntries] = ParamString;
  455.   this.mStringListEntries++;
  456. }
  457.  
  458. function  WWHStringBuffer_GetBuffer()
  459. {
  460.   this.mStringList.length = this.mStringListEntries;
  461.  
  462.   return this.mStringList.join("");
  463. }
  464.