home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EditorCommandsDebug.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  12.2 KB  |  427 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 2000
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /* Main Composer window debug menu functions */
  40.  
  41. // --------------------------- Output ---------------------------
  42.  
  43.  
  44. function EditorGetText()
  45. {
  46.   if (editorShell) {
  47.     dump("Getting text\n");
  48.     var  outputText = editorShell.GetContentsAs("text/plain", 2);
  49.     dump("<<" + outputText + ">>\n");
  50.   }
  51. }
  52.  
  53. function EditorGetHTML()
  54. {
  55.   if (editorShell) {
  56.     dump("Getting HTML\n");
  57.     var  outputHTML = editorShell.GetContentsAs("text/html", 256);
  58.     dump(outputHTML + "\n");
  59.   }
  60. }
  61.  
  62. function EditorDumpContent()
  63. {
  64.   if (window.editorShell) {
  65.     dump("==============  Content Tree: ================\n");
  66.     window.editorShell.DumpContentTree();
  67.   }
  68. }
  69.  
  70. function EditorInsertText(textToInsert)
  71. {
  72.   editorShell.InsertText(textToInsert);
  73. }
  74.  
  75. function EditorTestSelection()
  76. {
  77.   dump("Testing selection\n");
  78.   var selection = editorShell.editorSelection;
  79.   if (!selection)
  80.   {
  81.     dump("No selection!\n");
  82.     return;
  83.   }
  84.  
  85.   dump("Selection contains:\n");
  86.   // 3rd param = column to wrap
  87.   dump(selection.toStringWithFormat("text/plain",
  88.                                     3,  // OutputFormatted & gOutputSelectionOnly
  89.                                     0) + "\n");
  90.  
  91.   var output;
  92.  
  93.   dump("====== Selection as unformatted text ==========\n");
  94.   output = editorShell.GetContentsAs("text/plain", 1);
  95.   dump(output + "\n\n");
  96.  
  97.   dump("====== Selection as formatted text ============\n");
  98.   output = editorShell.GetContentsAs("text/plain", 3);
  99.   dump(output + "\n\n");
  100.  
  101.   dump("====== Selection as HTML ======================\n");
  102.   output = editorShell.GetContentsAs("text/html", 1);
  103.   dump(output + "\n\n");  
  104.  
  105.   dump("====== Selection as prettyprinted HTML ========\n");
  106.   output = editorShell.GetContentsAs("text/html", 3);
  107.   dump(output + "\n\n");  
  108.  
  109.   dump("====== Length and status =====================\n");
  110.   output = "Document is ";
  111.   if (editorShell.documentIsEmpty)
  112.     output += "empty\n";
  113.   else
  114.     output += "not empty\n";
  115.   output += "Document length is " + editorShell.documentLength + " characters";
  116.   dump(output + "\n\n");
  117. }
  118.  
  119. function EditorTestTableLayout()
  120. {
  121.   dump("\n\n\n************ Dump Selection Ranges ************\n");
  122.   var selection = editorShell.editorSelection;
  123.   for (i = 0; i < selection.rangeCount; i++)
  124.   {
  125.     var range = selection.getRangeAt(i);
  126.     if (range)
  127.     {
  128.       dump("Range "+i+": StartParent="+range.startParent+", offset="+range.startOffset+"\n");
  129.     }
  130.   }
  131.   dump("\n\n");
  132.  
  133.   var table = editorShell.GetElementOrParentByTagName("table", null);
  134.   if (!table) {
  135.     dump("Enclosing Table not found: Place caret in a table cell to do this test\n\n");
  136.     return;
  137.   }
  138.     
  139.   var cell;
  140.   var startRowIndexObj = new Object();
  141.   var startColIndexObj = new Object();
  142.   var rowSpanObj = new Object();
  143.   var colSpanObj = new Object();
  144.   var actualRowSpanObj = new Object();
  145.   var actualColSpanObj = new Object();
  146.   var isSelectedObj = new Object();
  147.   var startRowIndex = 0;
  148.   var startColIndex = 0;
  149.   var rowSpan;
  150.   var colSpan;
  151.   var actualRowSpan;
  152.   var actualColSpan;
  153.   var isSelected;
  154.   var col = 0;
  155.   var row = 0;
  156.   var rowCount = 0;
  157.   var maxColCount = 0;
  158.   var doneWithRow = false;
  159.   var doneWithCol = false;
  160.  
  161.   dump("\n\n\n************ Starting Table Layout test ************\n");
  162.  
  163.   // Note: We could also get the number of rows, cols and use for loops,
  164.   //   but this tests using out-of-bounds offsets to detect end of row or column
  165.  
  166.   while (!doneWithRow)  // Iterate through rows
  167.   {  
  168.     dump("* Data for ROW="+row+":\n");
  169.     while(!doneWithCol)  // Iterate through cells in the row
  170.     {
  171.       try {
  172.         cell = editorShell.GetCellDataAt(table, row, col, startRowIndexObj, startColIndexObj,
  173.                                          rowSpanObj, colSpanObj, actualRowSpanObj, actualColSpanObj, 
  174.                                          isSelectedObj);
  175.  
  176.         if (cell)
  177.         {
  178.           rowSpan = rowSpanObj.value;
  179.           colSpan = colSpanObj.value;
  180.           actualRowSpan = actualRowSpanObj.value;
  181.           actualColSpan = actualColSpanObj.value;
  182.           isSelected = isSelectedObj.value;
  183.           
  184.           dump(" Row="+row+", Col="+col+"  StartRow="+startRowIndexObj.value+", StartCol="+startColIndexObj.value+"\n");
  185.           dump("  RowSpan="+rowSpan+", ColSpan="+colSpan+"  ActualRowSpan="+actualRowSpan+", ActualColSpan="+actualColSpan);
  186.           if (isSelected)
  187.             dump("  Cell is selected\n");
  188.           else
  189.             dump("  Cell is NOT selected\n");
  190.  
  191.           // Save the indexes of a cell that will span across the cellmap grid
  192.           if (rowSpan > 1)
  193.             startRowIndex = startRowIndexObj.value;
  194.           if (colSpan > 1)
  195.             startColIndex = startColIndexObj.value;
  196.  
  197.           // Initialize these for efficient spanned-cell search
  198.           startRowIndexObj.value = startRowIndex;
  199.           startColIndexObj.value = startColIndex;
  200.  
  201.           col++;
  202.         } else {
  203.           doneWithCol = true;
  204.           // Get maximum number of cells in any row
  205.           if (col > maxColCount)
  206.             maxColCount = col;
  207.           dump("  End of row found\n\n");
  208.         }
  209.       }
  210.       catch (e) {
  211.         dump("  *** GetCellDataAt failed at Row="+row+", Col="+col+" ***\n\n");
  212.         return;
  213.       }
  214.     }
  215.     if (col == 0) {
  216.       // Didn't find a cell in the first col of a row,
  217.       // thus no more rows in table
  218.       doneWithRow = true;
  219.       rowCount = row;
  220.       dump("No more rows in table\n\n");
  221.     } else {
  222.       // Setup for next row
  223.       col = 0;
  224.       row++;
  225.       doneWithCol = false;
  226.     }      
  227.   }
  228.   dump("Counted during scan: Number of rows="+rowCount+" Number of Columns="+maxColCount+"\n");
  229.   rowCount = editorShell.GetTableRowCount(table);
  230.   maxColCount = editorShell.GetTableColumnCount(table);
  231.   dump("From nsITableLayout: Number of rows="+rowCount+" Number of Columns="+maxColCount+"\n****** End of Table Layout Test *****\n\n");
  232. }
  233.  
  234. function EditorShowEmbeddedObjects()
  235. {
  236.   dump("\nEmbedded Objects:\n");
  237.   var objectArray = editorShell.GetEmbeddedObjects();
  238.   dump(objectArray.Count() + " embedded objects\n");
  239.   for (var i=0; i < objectArray.Count(); ++i)
  240.     dump(objectArray.GetElementAt(i) + "\n");
  241. }
  242.  
  243. function EditorUnitTests()
  244. {
  245.   dump("Running Unit Tests\n");
  246.   editorShell.RunUnitTests();
  247. }
  248.  
  249. function EditorTestDocument()
  250. {
  251.   dump("Getting document\n");
  252.   var theDoc = editorShell.editorDocument;
  253.   if (theDoc)
  254.   {
  255.     dump("Got the doc\n");
  256.     dump("Document name:" + theDoc.nodeName + "\n");
  257.     dump("Document type:" + theDoc.doctype + "\n");
  258.   }
  259.   else
  260.   {
  261.     dump("Failed to get the doc\n");
  262.   }
  263. }
  264.  
  265. // --------------------------- Logging stuff ---------------------------
  266.  
  267. function EditorExecuteScript(theFile)
  268. {
  269.   var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance();
  270.   inputStream = inputStream.QueryInterface(Components.interfaces.nsIFileInputStream);
  271.  
  272.   inputStream.init(theFile, 1, 0, false);    // open read only
  273.   
  274.   var scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance();
  275.   scriptableInputStream = scriptableInputStream.QueryInterface(Components.interfaces.nsIScriptableInputStream);
  276.   
  277.   scriptableInputStream.init(inputStream);    // open read only
  278.   
  279.   var buf         = { value:null };
  280.   var tmpBuf      = { value:null };
  281.   var didTruncate = { value:false };
  282.   var lineNum     = 0;
  283.   var ex;
  284.  
  285. /*
  286.   // Log files can be quite huge, so read in a line
  287.   // at a time and execute it:
  288.  
  289.   while (!inputStream.eof())
  290.   {
  291.     buf.value         = "";
  292.     didTruncate.value = true;
  293.  
  294.     // Keep looping until we get a complete line of
  295.     // text, or we hit the end of file:
  296.  
  297.     while (didTruncate.value && !inputStream.eof())
  298.     {
  299.       didTruncate.value = false;
  300.       fileSpec.readLine(tmpBuf, 1024, didTruncate);
  301.       buf.value += tmpBuf.value;
  302.  
  303.       // XXX Need to null out tmpBuf.value to avoid crashing
  304.       // XXX in some JavaScript string allocation method.
  305.       // XXX This is probably leaking the buffer allocated
  306.       // XXX by the readLine() implementation.
  307.  
  308.       tmpBuf.value = null;
  309.     }
  310.  
  311.     ++lineNum;
  312. */
  313.   {
  314.     // suck in the entire file
  315.     var fileSize = scriptableInputStream.available();
  316.     var fileContents = scriptableInputStream.read(fileSize);
  317.     
  318.     dump(fileContents);
  319.     
  320.     try       { eval(fileContents); }
  321.     catch(ex) { dump("Playback ERROR: Line " + lineNum + "  " + ex + "\n"); return; }
  322.   }
  323.  
  324.   buf.value = null;
  325. }
  326.  
  327. function EditorGetScriptFileSpec()
  328. {
  329.   var dirServ = Components.classes['@mozilla.org/file/directory_service;1'].createInstance();
  330.   dirServ = dirServ.QueryInterface(Components.interfaces.nsIProperties);
  331.   var processDir = dirServ.get("Home", Components.interfaces.nsIFile);
  332.   processDir.append("journal.js");
  333.   return processDir;
  334. }
  335.  
  336. function EditorStartLog()
  337. {
  338.   var fs;
  339.  
  340.   fs = EditorGetScriptFileSpec();
  341.   editorShell.StartLogging(fs);
  342.   window._content.focus();
  343.  
  344.   fs = null;
  345. }
  346.  
  347. function EditorStopLog()
  348. {
  349.   editorShell.StopLogging();
  350.   window._content.focus();
  351. }
  352.  
  353. function EditorRunLog()
  354. {
  355.   var fs;
  356.   fs = EditorGetScriptFileSpec();
  357.   EditorExecuteScript(fs);
  358.   window._content.focus();
  359. }
  360.  
  361. // --------------------------- TransactionManager ---------------------------
  362.  
  363.  
  364. function DumpUndoStack()
  365. {
  366.   try {
  367.     var txmgr = editorShell.transactionManager;
  368.  
  369.     if (!txmgr)
  370.     {
  371.       dump("**** Editor has no TransactionManager!\n");
  372.       return;
  373.     }
  374.  
  375.     dump("---------------------- BEGIN UNDO STACK DUMP\n");
  376.     dump("<!-- Bottom of Stack -->\n");
  377.     PrintTxnList(txmgr.getUndoList(), "");
  378.     dump("<!--  Top of Stack  -->\n");
  379.     dump("Num Undo Items: " + txmgr.numberOfUndoItems + "\n");
  380.     dump("---------------------- END   UNDO STACK DUMP\n");
  381.   } catch (e) {
  382.     dump("ERROR: DumpUndoStack() failed: " + e);
  383.   }
  384. }
  385.  
  386. function DumpRedoStack()
  387. {
  388.   try {
  389.     var txmgr = editorShell.transactionManager;
  390.  
  391.     if (!txmgr)
  392.     {
  393.       dump("**** Editor has no TransactionManager!\n");
  394.       return;
  395.     }
  396.  
  397.     dump("---------------------- BEGIN REDO STACK DUMP\n");
  398.     dump("<!-- Bottom of Stack -->\n");
  399.     PrintTxnList(txmgr.getRedoList(), "");
  400.     dump("<!--  Top of Stack  -->\n");
  401.     dump("Num Redo Items: " + txmgr.numberOfRedoItems + "\n");
  402.     dump("---------------------- END   REDO STACK DUMP\n");
  403.   } catch (e) {
  404.     dump("ERROR: DumpUndoStack() failed: " + e);
  405.   }
  406. }
  407.  
  408. function PrintTxnList(txnList, prefixStr)
  409. {
  410.   var i;
  411.  
  412.   for (i=0 ; i < txnList.numItems; i++)
  413.   {
  414.     var txn = txnList.getItem(i);
  415.     var desc = "TXMgr Batch";
  416.  
  417.     if (txn)
  418.     {
  419.       txn = txn.QueryInterface(Components.interfaces.nsPIEditorTransaction);
  420.       desc = txn.txnDescription;
  421.     }
  422.     dump(prefixStr + "+ " + desc + "\n");
  423.     PrintTxnList(txnList.getChildListForItem(i), prefixStr + "|    ");
  424.   }
  425. }
  426.  
  427.