home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 December / DPPCPRO1202.ISO / Extras / Assistum / setup.exe / Msointl.dll / HTML / HALTEST.JS < prev    next >
Encoding:
JavaScript  |  2001-02-13  |  12.6 KB  |  462 lines

  1. // ---------------------------------------------------------------------------
  2. //
  3. // Terry Farrell (html_autolayout@hotmail.com)
  4. // Button Dialog Testing Code
  5. //
  6. // Created: December 1998
  7. // Last Updated: May 4th 1999
  8. //
  9. // ---------------------------------------------------------------------------
  10.  
  11. // ---------------------------------------------------------------------------
  12. // Global Variables
  13.  
  14. var usedHotkeys;              // Keep track of used hotkeys for the dialog
  15.  
  16. // ---------------------------------------------------------------------------
  17. // testItems
  18. // 
  19. // This cycles through the dialog inserting random text into the controls.
  20. // This is very useful in checking if the HAL rules have been followed.
  21. //
  22. // inputs:
  23. //    doc      The document object for the dialog
  24. //    option   Testing option (see code for details)
  25. //    debugstr A debug string to use (prepended or appended)  
  26. //
  27. // result: none
  28. // ---------------------------------------------------------------------------
  29. function testItems(doc, option, debugstr){
  30.  
  31.     var len = doc.length;
  32.     var i;
  33.     var elem;
  34.     var elemID;
  35.  
  36.  
  37.     for (i=0; i < len; i++)
  38.        {
  39.         elem = doc(i);
  40.         
  41.         // Handle lists first
  42.         if (elem != null && elem.tagName == "SELECT")
  43.             {
  44.             var l;
  45.             for (l=0; l < elem.length; l++)
  46.                 elem[l].text = getRandomString(elem[l].text);;
  47.             }
  48.         
  49.         if (elem != null)
  50.             {
  51.             
  52.             if ((elem.tagName == "BUTTON" || elem.tagName == "LABEL" || elem.tagName == "LEGEND") ||
  53.                 ( (elem.tagName == "TD" || elem.tagName == "DIV") && 
  54.                 elem.innerText.length != 0 && elem.innerHTML == elem.innerText))
  55.                {
  56.  
  57.             // always switch to <label> if it's present
  58.             if (elem.children.length > 0 && elem.children[0].tagName == "LABEL")    
  59.                 elem = elem.children[0];        
  60.  
  61.                if (option == "1")    // uppercase
  62.                    elem.innerHTML = elem.innerHTML.toUpperCase();
  63.                else if (option == "2") // lowercase
  64.                    elem.innerHTML = elem.innerHTML.toLowerCase();
  65.                else if (option == "4") // append
  66.                    elem.innerHTML = elem.innerHTML + debugstr;
  67.                else if (option == "3") // prepend
  68.                    elem.innerHTML = debugstr + elem.innerHTML;
  69.                else if (option == "0") // random
  70.                    {
  71.                            elem.innerHTML = getRandomString(elem.innerHTML);
  72.                    }
  73.                }
  74.                
  75.            }
  76.         }
  77.                     
  78.  
  79. }
  80.  
  81. // ---------------------------------------------------------------------------
  82. // Create a random string.
  83. //
  84. // Inputs:
  85. //    1. Original string
  86. // ---------------------------------------------------------------------------
  87. function getRandomString(str) {
  88.  
  89.     var strlen = str.length;
  90.  
  91.     // extract hotkey if there is one
  92.     var strHotkey = "";
  93.     str = str.toUpperCase();
  94.     var starthk = str.search("<U>");
  95.  
  96.     //if (starthk != -1)
  97.     //    strHotkey = str.substr(starthk, 8);
  98.                               
  99.                            
  100.     // 75% of the time, pick a random length
  101.     if (parseInt(Math.random() * 100) % 3 != 1)
  102.         {
  103.         // pick random growth
  104.         var growth = Math.random();
  105.                            
  106.         if (strlen < 10)
  107.             growth = growth * 2; 
  108.  
  109.         strlen = parseInt(strlen * growth);
  110.                            
  111.         if (strlen < 2)    // no zero allowed
  112.             strlen = 5    // good default
  113.         }
  114.                            
  115.  
  116.     //alert(elem.innerHTML + " : " + strlen);
  117.     var j;
  118.     var newStr = "";
  119.     for (j = 0; j < strlen; j++)
  120.         {
  121.         if (j==0 && starthk != -1)        // stick a hotkey at the start
  122.             {
  123.             newStr = newStr + String.fromCharCode(97 + (Math.random() * 100 % 5));
  124.                newStr = "<U>" + newStr.toUpperCase() + "</U>";
  125.                }
  126.         else
  127.             newStr = newStr + String.fromCharCode(97 + (Math.random() * 100 % 26));
  128.                        
  129.         if (j && j % 5 == 0)    // add space every 5
  130.             newStr = newStr + " ";
  131.         }
  132.  
  133.     return newStr;
  134.  
  135. }
  136.  
  137. // ---------------------------------------------------------------------------
  138. // showTableBorders
  139. // 
  140. // Toggle all the table borders in the document on or off.
  141. // This is very useful for seeing the structure of the dialog, which in
  142. // turn is useful for seeing of HAL rules have been used.
  143. //
  144. // Inputs:
  145. //    doc   The document object for the dialog
  146. // ---------------------------------------------------------------------------
  147. function showTableBorders(doc){
  148.  
  149.     var i;
  150.     var doc_tables = doc.all.tags("TABLE");
  151.    var colors = new Array("blue", "green", "brown", "cyan", "KHAKI", "lawngreen", "steelblue", "yellow", "palegreen", "mediumpurple");
  152.  
  153.     for (i=0; i<doc_tables.length; i++)
  154.         {
  155.         doc_tables(i).border = 1 - doc_tables(i).border;
  156.         doc_tables(i).style.borderColor = colors[i % 9];
  157.         }
  158. }
  159.  
  160.  
  161. // ---------------------------------------------------------------------------
  162. // Detect and fix duplicate hotkeys
  163. // ---------------------------------------------------------------------------
  164. function fixHotkeys(doc) {
  165.     var len = doc.length;
  166.     var i;
  167.     var elem;
  168.     var boolAssignedKey = true;
  169.     var badHotkeys = "gijlpqy";
  170.     var dupHotkeys = new Array();        // list of elements that are dup
  171.     var numDupHotkeys = 0;                // no of duplicate hotkeys
  172.     var posUsedHotkeys = new Array(); // store position of used hotkeys
  173.     var numUsedHotkeys = 0;
  174.     
  175.     usedHotkeys = "";
  176.  
  177.     // first pass: find all duplicate hotkeys
  178.     for (i=0; i < len && boolAssignedKey == true; i++)
  179.     {
  180.         elem = doc(i);
  181.         
  182.         if (elem !=null && 
  183.            ((elem.htmlFor != null && elem.htmlFor.length != 0) ||
  184.            (elem.tagName == "BUTTON")))
  185.            {
  186.            // This is most likely a label
  187.            var strHotkey = "";
  188.                
  189.            var hotkeyFor;
  190.  
  191.            // see if it was a FOR item
  192.            if (elem.htmlFor != null && elem.htmlFor.length)
  193.                hotkeyFor = doc(elem.htmlFor);
  194.            else    // or a self contained hotkey
  195.                hotkeyFor = elem;
  196.  
  197.            if (elem.innerHTML.length != 0)
  198.                {
  199.                var str = elem.innerHTML;
  200.                var strlen = str.length;
  201.                    
  202.                // extract hotkey if there is one
  203.                //str = str.toUpperCase();
  204.                var starthk = str.search("<U>");
  205.                if (starthk != -1)
  206.                  {
  207.                  strHotkey = str.substr(starthk+3, 1);
  208.                  //strHotkey = strHotkey.toUpperCase();
  209.  
  210.                     if (usedHotkeys.search(strHotkey.toUpperCase()) != -1)
  211.                        {
  212.                        // duplicate hotkey found
  213.                        //elem.style.color = "red";
  214.                        dupHotkeys[numDupHotkeys] = i;    // store bad element
  215.                        numDupHotkeys = numDupHotkeys + 1;
  216.                        }
  217.                     else
  218.                         {
  219.                         usedHotkeys = usedHotkeys + strHotkey.toUpperCase();
  220.                         posUsedHotkeys[numUsedHotkeys] = i;
  221.                         numUsedHotkeys = numUsedHotkeys + 1;
  222.                         }
  223.                         
  224.                     // check for bogus hotkeys
  225.                     //if (badHotkeys.search(strHotkey) != -1)
  226.                     //    elem.style.background = "white";
  227.                    
  228.                    str = elem.innerText;
  229.                    } // end (if there is a hotkey)
  230.                    
  231.                } // end (if the element has text)
  232.                
  233.            } // end (if it's the type of item that normally has hotkeys)
  234.                
  235.     } // end (for loop running through the doc)
  236.         
  237.         
  238.     // Second pass, fix the duplicates - go top to bottom
  239.     var boolAssignedKey = true;
  240.     for (i=0; i < numDupHotkeys; i++)
  241.         {
  242.         elem = doc(dupHotkeys[i]);
  243.         str = elem.innerText;
  244.  
  245.         var hotkeyPos;
  246.         hotkeyPos = findHotkey(str, false);
  247.  
  248.         if (hotkeyPos == -1)
  249.             {
  250.             // try again, but allow "bad" characters
  251.             hotkeyPos = findHotkey(str, true);
  252.             }
  253.  
  254.         if (hotkeyPos == -1)        // no hotkey available
  255.             {
  256.             // try going through every character, go to the other
  257.             // string that uses that hotkey and see if it can use a different hotkey
  258.             
  259.             str = str.toUpperCase();
  260.         
  261.             //for (j=0; j<numUsedHotkeys; j++)
  262.             //    alert("used hotkeys: " + usedHotkeys.substr(j, 1) + " " + posUsedHotkeys[j] + " " + doc(posUsedHotkeys[j]).innerText);
  263.             
  264.             for (j=0; j < str.length; j++)
  265.                 {
  266.                 var posOtherElem = usedHotkeys.search(str.substr(j, 1));
  267.                 if (posOtherElem != -1)
  268.                     {
  269.                     //alert("char = " + j + str.substr(j, 1) + " " +  doc(posUsedHotkeys[posOtherElem]).innerText);
  270.                     }
  271.                 }
  272.                         
  273.             elem.style.color = "red";
  274.             //alert(usedHotkeys.length + ": " + usedHotkeys)
  275.             }
  276.         else
  277.             {
  278.             var oldText = elem.innerText;
  279.             str = elem.innerText;
  280.             
  281.             elem.innerHTML = oldText.substr(0, hotkeyPos) + "<U>" + oldText.substr(hotkeyPos, 1) + "</U>" + oldText.substr(hotkeyPos+1, oldText.length - hotkeyPos);
  282.             }
  283.         }
  284.  
  285. }
  286.  
  287. // ---------------------------------------------------------------------------
  288. // findHotkey
  289. // returns: the position of the hotkey, or -1 if not hotkey is found
  290. //        New hotkeys are also appended to the global list of usedHotkeys
  291. //
  292. // ---------------------------------------------------------------------------
  293.  
  294. function findHotkey(str, tryBadKeys)
  295. {
  296.     var hotkeyPos;
  297.     hotkeyPos = -1;
  298.     var badHotkeys = "gijlpqy";
  299.     var invalidHotkeys = " -:\t";
  300.  
  301.  
  302.     for (j = 0, newAccessKey = ""; j < str.length && newAccessKey == ""; j++)
  303.         {
  304.         var testChar = str.substr(j, 1);
  305.         if (invalidHotkeys.search(testChar) == -1 && // not bogus
  306.                 usedHotkeys.search(testChar.toUpperCase()) == -1)  // not used
  307.             {
  308.             if ((tryBadKeys == false && badHotkeys.search(testChar) == -1)    ||
  309.                 tryBadKeys == true )
  310.                 {
  311.                 hotkeyPos = j;
  312.  
  313.                 newAccessKey = testChar.toUpperCase();
  314.                 usedHotkeys = usedHotkeys + newAccessKey;
  315.  
  316.                 //alert(usedHotkeys + " " + newAccessKey);
  317.                 }
  318.             }
  319.         }
  320.  
  321.     return (hotkeyPos);
  322.  
  323. }
  324.  
  325. // ---------------------------------------------------------------------------
  326. // assignhotkeys
  327. //
  328. // This function assigns hotkeys to all labels in the document and the controls
  329. // they apply to.
  330. // A very simple rule of attempting to use the first available letter is used.
  331. // Bad hotkeys are avoided.
  332. //
  333. //    All controls *must* have a <label for=xxx>, except buttons
  334. //
  335. // ---------------------------------------------------------------------------
  336.  
  337. function assignHotkeys(doc){
  338.  
  339.     var len = doc.length;
  340.     var i;
  341.     var elem;
  342.     var boolAssignedKey = true;
  343.     
  344.     usedHotkeys = "";
  345.  
  346.     // first pass, top to bottom
  347.     for (i=0; i < len && boolAssignedKey == true; i++)
  348.     {
  349.         elem = doc(i);
  350.         boolAssignedKey = assignHotkey(elem, doc);
  351.     }
  352.  
  353.     // do we need to try again? bottom up
  354.     if (boolAssignedKey == false)    // failed to get a key
  355.     {
  356.         //alert("need second pass");
  357.         usedHotkeys = "";
  358.  
  359.         for (i=len-1; i > 0; i--)  // work from bottom to top
  360.         {
  361.             elem = doc(i);
  362.             boolAssignedKey = assignHotkey(elem, doc);
  363.         }
  364.     }
  365.     
  366.  
  367. }
  368.  
  369. // ---------------------------------------------------------------------------
  370. // assignHotkey
  371. //
  372. // Try to find an unused hotkey for the item.
  373. // Underline the character in the document.
  374. // TODO: set the accessKey attribute.
  375. //
  376. // return value: true or false
  377. // ---------------------------------------------------------------------------
  378. function assignHotkey(elem, doc)
  379. {
  380.     var returnVal = true;        // assume a good outcome
  381.  
  382.     if (elem !=null && 
  383.         ((elem.htmlFor != null && elem.htmlFor.length != 0) ||
  384.         (elem.tagName == "BUTTON")))
  385.         {
  386.         // This is most likely a label
  387.         var strHotkey = "";
  388.             
  389.         var hotkeyFor;
  390.  
  391.         // see if it was a FOR item
  392.         if (elem.htmlFor != null && elem.htmlFor.length)
  393.             hotkeyFor = doc(elem.htmlFor);
  394.         else    // or a self contained hotkey
  395.             hotkeyFor = elem;
  396.  
  397.         if (elem.innerHTML.length != 0)
  398.             {
  399.             var str = elem.innerHTML;
  400.             var strlen = str.length;
  401.                 
  402.             // extract hotkey if there is one
  403.             //str = str.toUpperCase();
  404.             //var starthk = str.search("<U>");
  405.             //if (starthk != -1)
  406.             //  {
  407.             //   strHotkey = str.substr(starthk+3, 1);
  408.             //  }
  409.                 
  410.             str = elem.innerText;
  411.  
  412.             var hotkeyPos;
  413.             hotkeyPos = findHotkey(str, false);
  414.  
  415.             if (hotkeyPos == -1)
  416.                 {
  417.                 // try again, but allow "bad" characters
  418.                 hotkeyPos = findHotkey(str, true);
  419.                 }
  420.  
  421.             if (hotkeyPos == -1)
  422.                 {    // failed
  423.                 elem.innerHTML = "<font color=red>" + elem.innerText + "</font>";
  424.                                 elem.accessKey = null;
  425.                 returnVal = false;    // bad outcome
  426.                 }
  427.             else
  428.                 {
  429.                 var oldText = elem.innerText;
  430.                 str = elem.innerText;
  431.                 
  432.                 elem.innerHTML = oldText.substr(0, hotkeyPos) + "<U>" + oldText.substr(hotkeyPos, 1) + "</U>" + oldText.substr(hotkeyPos+1, oldText.length - hotkeyPos);
  433.                                 elem.accessKey = oldText.substr(hotkeyPos, 1);
  434.                 }
  435.             }
  436.  
  437.  
  438.         //alert("Current tag: <" + elem.tagName + "> htmlFor: " + 
  439.         //elem.htmlFor + " targetTag: <" + hotkeyFor.tagName + 
  440.         //"> targetHK = " + hotkeyFor.accessKey + " Underline on: " + strHotkey);
  441.  
  442.  
  443.         }
  444.  
  445.     return (returnVal);
  446. }
  447.  
  448.  
  449. // ---------------------------------------------------------------------------
  450. // Flip dialog
  451. //     Just set the reading order on the body to rtl
  452. //    Good for testing if the dialog will work for mideast.
  453. // ---------------------------------------------------------------------------
  454. function flipDialog(doc){
  455.  
  456.     if (doc.body.dir == null || doc.body.dir == "" || doc.body.dir == "ltr")
  457.         doc.body.dir = "rtl";
  458.     else
  459.         doc.body.dir = "ltr";
  460. }
  461.  
  462.