home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / editor / EditorCommandsDebug.js < prev    next >
Text File  |  2003-06-08  |  14KB  |  454 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.   try {
  47.     dump("Getting text\n");
  48.     var  outputText = GetCurrentEditor().outputToString("text/plain", 2);
  49.     dump("<<" + outputText + ">>\n");
  50.   } catch (e) {}
  51. }
  52.  
  53. function EditorGetHTML()
  54. {
  55.   try {
  56.     dump("Getting HTML\n");
  57.     var  outputHTML = GetCurrentEditor().outputToString("text/html", 256);
  58.     dump(outputHTML + "\n");
  59.   } catch (e) {}
  60. }
  61.  
  62. function EditorDumpContent()
  63. {
  64.   dump("==============  Content Tree: ================\n");
  65.   GetCurrentEditor().dumpContentTree();
  66. }
  67.  
  68. function EditorInsertText(textToInsert)
  69. {
  70.   GetCurrentEditor().insertText(textToInsert);
  71. }
  72.  
  73. function EditorTestSelection()
  74. {
  75.   dump("Testing selection\n");
  76.   var selection = GetCurrentEditor().selection;
  77.   if (!selection)
  78.   {
  79.     dump("No selection!\n");
  80.     return;
  81.   }
  82.  
  83.   dump("Selection contains:\n");
  84.   // 3rd param = column to wrap
  85.   dump(selection.QueryInterface(Components.interfaces.nsISelectionPrivate)
  86.        .toStringWithFormat("text/plain",
  87.                            3,  // OutputFormatted & gOutputSelectionOnly
  88.                            0) + "\n");
  89.  
  90.   var output, i;
  91.  
  92.   dump("====== Selection as node and offsets==========\n");
  93.   dump("rangeCount = " + selection.rangeCount + "\n");
  94.   for (i = 0; i < selection.rangeCount; i++)
  95.   {
  96.     var range = selection.getRangeAt(i);
  97.     if (range)
  98.     {
  99.       dump("Range "+i+": StartParent="+range.startContainer.nodeName+", offset="+range.startOffset+"\n");
  100.       dump("Range "+i+":   EndParent="+range.endContainer.nodeName+", offset="+range.endOffset+"\n\n");
  101.     }
  102.   }
  103.  
  104.   var editor = GetCurrentEditor();
  105.  
  106.   dump("====== Selection as unformatted text ==========\n");
  107.   output = editor.outputToString("text/plain", 1);
  108.   dump(output + "\n\n");
  109.  
  110.   dump("====== Selection as formatted text ============\n");
  111.   output = editor.outputToString("text/plain", 3);
  112.   dump(output + "\n\n");
  113.  
  114.   dump("====== Selection as HTML ======================\n");
  115.   output = editor.outputToString("text/html", 1);
  116.   dump(output + "\n\n");  
  117.  
  118.   dump("====== Selection as prettyprinted HTML ========\n");
  119.   output = editor.outputToString("text/html", 3);
  120.   dump(output + "\n\n");  
  121.  
  122.   dump("====== Length and status =====================\n");
  123.   output = "Document is ";
  124.   if (editor.documentIsEmpty)
  125.     output += "empty\n";
  126.   else
  127.     output += "not empty\n";
  128.   output += "Text length is " + editor.textLength + " characters";
  129.   dump(output + "\n\n");
  130. }
  131.  
  132. function EditorTestTableLayout()
  133. {
  134.   dump("\n\n\n************ Dump Selection Ranges ************\n");
  135.   var selection = GetCurrentEditor().selection;
  136.   var i;
  137.   for (i = 0; i < selection.rangeCount; i++)
  138.   {
  139.     var range = selection.getRangeAt(i);
  140.     if (range)
  141.     {
  142.       dump("Range "+i+": StartParent="+range.startParent+", offset="+range.startOffset+"\n");
  143.     }
  144.   }
  145.   dump("\n\n");
  146.  
  147.   var editor = GetCurrentEditor();
  148.   var table = editor.getElementOrParentByTagName("table", null);
  149.   if (!table) {
  150.     dump("Enclosing Table not found: Place caret in a table cell to do this test\n\n");
  151.     return;
  152.   }
  153.     
  154.   var cell;
  155.   var startRowIndexObj = { value: null };
  156.   var startColIndexObj = { value: null };
  157.   var rowSpanObj = { value: null };
  158.   var colSpanObj = { value: null };
  159.   var actualRowSpanObj = { value: null };
  160.   var actualColSpanObj = { value: null };
  161.   var isSelectedObj = { value: false };
  162.   var startRowIndex = 0;
  163.   var startColIndex = 0;
  164.   var rowSpan;
  165.   var colSpan;
  166.   var actualRowSpan;
  167.   var actualColSpan;
  168.   var isSelected;
  169.   var col = 0;
  170.   var row = 0;
  171.   var rowCount = 0;
  172.   var maxColCount = 0;
  173.   var doneWithRow = false;
  174.   var doneWithCol = false;
  175.  
  176.   dump("\n\n\n************ Starting Table Layout test ************\n");
  177.  
  178.   // Note: We could also get the number of rows, cols and use for loops,
  179.   //   but this tests using out-of-bounds offsets to detect end of row or column
  180.  
  181.   while (!doneWithRow)  // Iterate through rows
  182.   {  
  183.     dump("* Data for ROW="+row+":\n");
  184.     while(!doneWithCol)  // Iterate through cells in the row
  185.     {
  186.       try {
  187.         cell = editor.getCellDataAt(table, row, col,
  188.                                     startRowIndexObj, startColIndexObj,
  189.                                     rowSpanObj, colSpanObj,
  190.                                     actualRowSpanObj, actualColSpanObj,
  191.                                     isSelectedObj);
  192.  
  193.         if (cell)
  194.         {
  195.           rowSpan = rowSpanObj.value;
  196.           colSpan = colSpanObj.value;
  197.           actualRowSpan = actualRowSpanObj.value;
  198.           actualColSpan = actualColSpanObj.value;
  199.           isSelected = isSelectedObj.value;
  200.           
  201.           dump(" Row="+row+", Col="+col+"  StartRow="+startRowIndexObj.value+", StartCol="+startColIndexObj.value+"\n");
  202.           dump("  RowSpan="+rowSpan+", ColSpan="+colSpan+"  ActualRowSpan="+actualRowSpan+", ActualColSpan="+actualColSpan);
  203.           if (isSelected)
  204.             dump("  Cell is selected\n");
  205.           else
  206.             dump("  Cell is NOT selected\n");
  207.  
  208.           // Save the indexes of a cell that will span across the cellmap grid
  209.           if (rowSpan > 1)
  210.             startRowIndex = startRowIndexObj.value;
  211.           if (colSpan > 1)
  212.             startColIndex = startColIndexObj.value;
  213.  
  214.           // Initialize these for efficient spanned-cell search
  215.           startRowIndexObj.value = startRowIndex;
  216.           startColIndexObj.value = startColIndex;
  217.  
  218.           col++;
  219.         } else {
  220.           doneWithCol = true;
  221.           // Get maximum number of cells in any row
  222.           if (col > maxColCount)
  223.             maxColCount = col;
  224.           dump("  End of row found\n\n");
  225.         }
  226.       }
  227.       catch (e) {
  228.         dump("  *** GetCellDataAt failed at Row="+row+", Col="+col+" ***\n\n");
  229.         return;
  230.       }
  231.     }
  232.     if (col == 0) {
  233.       // Didn't find a cell in the first col of a row,
  234.       // thus no more rows in table
  235.       doneWithRow = true;
  236.       rowCount = row;
  237.       dump("No more rows in table\n\n");
  238.     } else {
  239.       // Setup for next row
  240.       col = 0;
  241.       row++;
  242.       doneWithCol = false;
  243.     }      
  244.   }
  245.   dump("Counted during scan: Number of rows="+rowCount+" Number of Columns="+maxColCount+"\n");
  246.   rowCount = editor.getTableRowCount(table);
  247.   maxColCount = editor.getTableColumnCount(table);
  248.   dump("From nsITableLayout: Number of rows="+rowCount+" Number of Columns="+maxColCount+"\n****** End of Table Layout Test *****\n\n");
  249. }
  250.  
  251. function EditorShowEmbeddedObjects()
  252. {
  253.   dump("\nEmbedded Objects:\n");
  254.   try {
  255.     var objectArray = GetCurrentEditor().getEmbeddedObjects();
  256.     dump(objectArray.Count() + " embedded objects\n");
  257.     for (var i=0; i < objectArray.Count(); ++i)
  258.       dump(objectArray.GetElementAt(i) + "\n");
  259.   } catch(e) {}
  260. }
  261.  
  262. function EditorUnitTests()
  263. {
  264.   dump("Running Unit Tests\n");
  265.   var numTests       = { value:0 };
  266.   var numTestsFailed = { value:0 };
  267.   GetCurrentEditor().debugUnitTests(numTests, numTestsFailed);
  268. }
  269.  
  270. function EditorTestDocument()
  271. {
  272.   dump("Getting document\n");
  273.   var theDoc = GetCurrentEditor().document;
  274.   if (theDoc)
  275.   {
  276.     dump("Got the doc\n");
  277.     dump("Document name:" + theDoc.nodeName + "\n");
  278.     dump("Document type:" + theDoc.doctype + "\n");
  279.   }
  280.   else
  281.   {
  282.     dump("Failed to get the doc\n");
  283.   }
  284. }
  285.  
  286. // --------------------------- Logging stuff ---------------------------
  287.  
  288. function EditorExecuteScript(theFile)
  289. {
  290.   var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance();
  291.   inputStream = inputStream.QueryInterface(Components.interfaces.nsIFileInputStream);
  292.  
  293.   inputStream.init(theFile, 1, 0, false);    // open read only
  294.   
  295.   var scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance();
  296.   scriptableInputStream = scriptableInputStream.QueryInterface(Components.interfaces.nsIScriptableInputStream);
  297.   
  298.   scriptableInputStream.init(inputStream);    // open read only
  299.   
  300.   var buf         = { value:null };
  301.   var tmpBuf      = { value:null };
  302.   var didTruncate = { value:false };
  303.   var lineNum     = 0;
  304.   var ex;
  305.  
  306. /*
  307.   // Log files can be quite huge, so read in a line
  308.   // at a time and execute it:
  309.  
  310.   while (!inputStream.eof())
  311.   {
  312.     buf.value         = "";
  313.     didTruncate.value = true;
  314.  
  315.     // Keep looping until we get a complete line of
  316.     // text, or we hit the end of file:
  317.  
  318.     while (didTruncate.value && !inputStream.eof())
  319.     {
  320.       didTruncate.value = false;
  321.       fileSpec.readLine(tmpBuf, 1024, didTruncate);
  322.       buf.value += tmpBuf.value;
  323.  
  324.       // XXX Need to null out tmpBuf.value to avoid crashing
  325.       // XXX in some JavaScript string allocation method.
  326.       // XXX This is probably leaking the buffer allocated
  327.       // XXX by the readLine() implementation.
  328.  
  329.       tmpBuf.value = null;
  330.     }
  331.  
  332.     ++lineNum;
  333. */
  334.   {
  335.     // suck in the entire file
  336.     var fileSize = scriptableInputStream.available();
  337.     var fileContents = scriptableInputStream.read(fileSize);
  338.     
  339.     dump(fileContents);
  340.     
  341.     try       { eval(fileContents); }
  342.     catch(ex) { dump("Playback ERROR: Line " + lineNum + "  " + ex + "\n"); return; }
  343.   }
  344.  
  345.   buf.value = null;
  346. }
  347.  
  348. function EditorGetScriptFileSpec()
  349. {
  350.   var dirServ = Components.classes['@mozilla.org/file/directory_service;1'].createInstance();
  351.   dirServ = dirServ.QueryInterface(Components.interfaces.nsIProperties);
  352.   var processDir = dirServ.get("Home", Components.interfaces.nsIFile);
  353.   processDir.append("journal.js");
  354.   return processDir;
  355. }
  356.  
  357. function EditorStartLog()
  358. {
  359.   try {
  360.     var edlog = GetCurrentEditor().QueryInterface(Components.interfaces.nsIEditorLogging);
  361.     var fs = EditorGetScriptFileSpec();
  362.     edlog.startLogging(fs);
  363.     window._content.focus();
  364.  
  365.     fs = null;
  366.   }
  367.   catch(ex) { dump("Can't start logging!:\n" + ex + "\n"); }
  368. }
  369.  
  370. function EditorStopLog()
  371. {
  372.   try {
  373.     var edlog = GetCurrentEditor().QueryInterface(Components.interfaces.nsIEditorLogging);
  374.     edlog.stopLogging();
  375.     window._content.focus();
  376.   }
  377.   catch(ex) { dump("Can't stop logging!:\n" + ex + "\n"); }
  378. }
  379.  
  380. function EditorRunLog()
  381. {
  382.   var fs;
  383.   fs = EditorGetScriptFileSpec();
  384.   EditorExecuteScript(fs);
  385.   window._content.focus();
  386. }
  387.  
  388. // --------------------------- TransactionManager ---------------------------
  389.  
  390.  
  391. function DumpUndoStack()
  392. {
  393.   try {
  394.     var txmgr = GetCurrentEditor().transactionManager;
  395.  
  396.     if (!txmgr)
  397.     {
  398.       dump("**** Editor has no TransactionManager!\n");
  399.       return;
  400.     }
  401.  
  402.     dump("---------------------- BEGIN UNDO STACK DUMP\n");
  403.     dump("<!-- Bottom of Stack -->\n");
  404.     PrintTxnList(txmgr.getUndoList(), "");
  405.     dump("<!--  Top of Stack  -->\n");
  406.     dump("Num Undo Items: " + txmgr.numberOfUndoItems + "\n");
  407.     dump("---------------------- END   UNDO STACK DUMP\n");
  408.   } catch (e) {
  409.     dump("ERROR: DumpUndoStack() failed: " + e);
  410.   }
  411. }
  412.  
  413. function DumpRedoStack()
  414. {
  415.   try {
  416.     var txmgr = GetCurrentEditor().transactionManager;
  417.  
  418.     if (!txmgr)
  419.     {
  420.       dump("**** Editor has no TransactionManager!\n");
  421.       return;
  422.     }
  423.  
  424.     dump("---------------------- BEGIN REDO STACK DUMP\n");
  425.     dump("<!-- Bottom of Stack -->\n");
  426.     PrintTxnList(txmgr.getRedoList(), "");
  427.     dump("<!--  Top of Stack  -->\n");
  428.     dump("Num Redo Items: " + txmgr.numberOfRedoItems + "\n");
  429.     dump("---------------------- END   REDO STACK DUMP\n");
  430.   } catch (e) {
  431.     dump("ERROR: DumpUndoStack() failed: " + e);
  432.   }
  433. }
  434.  
  435. function PrintTxnList(txnList, prefixStr)
  436. {
  437.   var i;
  438.  
  439.   for (i=0 ; i < txnList.numItems; i++)
  440.   {
  441.     var txn = txnList.getItem(i);
  442.     var desc = "TXMgr Batch";
  443.  
  444.     if (txn)
  445.     {
  446.       txn = txn.QueryInterface(Components.interfaces.nsPIEditorTransaction);
  447.       desc = txn.txnDescription;
  448.     }
  449.     dump(prefixStr + "+ " + desc + "\n");
  450.     PrintTxnList(txnList.getChildListForItem(i), prefixStr + "|    ");
  451.   }
  452. }
  453.  
  454.