From 7b542e3e3493c093eff70fbb8ce4082b5956be77 Mon Sep 17 00:00:00 2001 From: "brade%netscape.com" Date: Mon, 28 Jul 2003 13:13:50 +0000 Subject: [PATCH] cleanup of editor code, bug=209548, r=jfrancis, sr=blizzard git-svn-id: svn://10.0.0.236/trunk@145270 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/idl/nsIPlaintextEditor.idl | 12 - .../base/ChangeCSSInlineStyleTxn.cpp | 8 +- .../libeditor/html/nsHTMLDataTransfer.cpp | 43 +- .../editor/libeditor/html/nsHTMLEditor.cpp | 6 - mozilla/editor/libeditor/html/nsHTMLEditor.h | 13 +- .../editor/libeditor/html/nsTableEditor.cpp | 188 +++---- .../text/nsPlaintextDataTransfer.cpp | 251 +++++---- .../libeditor/text/nsPlaintextEditor.cpp | 480 ++++++------------ .../editor/libeditor/text/nsPlaintextEditor.h | 21 +- 9 files changed, 370 insertions(+), 652 deletions(-) diff --git a/mozilla/editor/idl/nsIPlaintextEditor.idl b/mozilla/editor/idl/nsIPlaintextEditor.idl index b52faa3fbd2..0df8f77004f 100644 --- a/mozilla/editor/idl/nsIPlaintextEditor.idl +++ b/mozilla/editor/idl/nsIPlaintextEditor.idl @@ -106,11 +106,6 @@ interface nsIPlaintextEditor : nsISupports * @param aString the string to be inserted */ void insertText(in DOMString aStringToInsert); - - /** - * True if the document is modifiable. - */ - readonly attribute boolean canModify; /** * Insert a line break into the content model. @@ -119,11 +114,4 @@ interface nsIPlaintextEditor : nsISupports * This may be more efficient than calling InsertText with a newline. */ void insertLineBreak(); - - /** - * Set selection to start of text. - * This will be moved to a different API eventually! - * Should probably be done through the selection controller. - */ - void collapseSelectionToStart(); }; diff --git a/mozilla/editor/libeditor/base/ChangeCSSInlineStyleTxn.cpp b/mozilla/editor/libeditor/base/ChangeCSSInlineStyleTxn.cpp index 986deb724aa..9ade39a8f4a 100644 --- a/mozilla/editor/libeditor/base/ChangeCSSInlineStyleTxn.cpp +++ b/mozilla/editor/libeditor/base/ChangeCSSInlineStyleTxn.cpp @@ -317,12 +317,12 @@ NS_IMETHODIMP ChangeCSSInlineStyleTxn::Merge(nsITransaction *aTransaction, PRBoo NS_IMETHODIMP ChangeCSSInlineStyleTxn::GetTxnDescription(nsAString& aString) { - aString.Assign(NS_LITERAL_STRING("ChangeCSSInlineStyleTxn: ")); + aString.Assign(NS_LITERAL_STRING("ChangeCSSInlineStyleTxn: [mRemoveProperty == ")); - if (PR_FALSE==mRemoveProperty) - aString += NS_LITERAL_STRING("[mRemoveProperty == false] "); + if (!mRemoveProperty) + aString += NS_LITERAL_STRING("false] "); else - aString += NS_LITERAL_STRING("[mRemoveProperty == true] "); + aString += NS_LITERAL_STRING("true] "); nsAutoString tempString; mProperty->ToString(tempString); aString += tempString; diff --git a/mozilla/editor/libeditor/html/nsHTMLDataTransfer.cpp b/mozilla/editor/libeditor/html/nsHTMLDataTransfer.cpp index 7b09a5757d2..4b52b9a0491 100644 --- a/mozilla/editor/libeditor/html/nsHTMLDataTransfer.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLDataTransfer.cpp @@ -774,7 +774,7 @@ nsHTMLEditor::DoContentFilterCallback(const nsAString &aFlavor, PRInt32 *aFragStartOffset, nsIDOMNode **aFragEndNode, PRInt32 *aFragEndOffset, - nsIDOMNode **aTargetNode, + nsIDOMNode **aTargetNode, PRInt32 *aTargetOffset, PRBool *aDoContinue) { @@ -1001,13 +1001,12 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable if ( NS_SUCCEEDED(transferable->GetAnyTransferData(&bestFlavor, getter_AddRefs(genericDataObj), &len)) ) { nsAutoTxnsConserveSelection dontSpazMySelection(this); - nsAutoString flavor, stuffToPaste; - flavor.AssignWithConversion(bestFlavor); // just so we can use flavor.Equals() + nsAutoString flavor(bestFlavor); + nsAutoString stuffToPaste; #ifdef DEBUG_clipboard printf("Got flavor [%s]\n", bestFlavor); #endif - - if (flavor.Equals(NS_LITERAL_STRING(kNativeHTMLMime))) + if (0 == nsCRT::strcmp(bestFlavor, kNativeHTMLMime)) { // note cf_html uses utf8, hence use length = len, not len/2 as in flavors below nsCOMPtr textDataObj(do_QueryInterface(genericDataObj)); @@ -1030,7 +1029,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable } } } - else if (flavor.Equals(NS_LITERAL_STRING(kHTMLMime))) + else if (0 == nsCRT::strcmp(bestFlavor, kHTMLMime)) { nsCOMPtr textDataObj(do_QueryInterface(genericDataObj)); if (textDataObj && len > 0) @@ -1047,7 +1046,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable aDoDeleteSelection); } } - else if (flavor.Equals(NS_LITERAL_STRING(kUnicodeMime))) + else if (0 == nsCRT::strcmp(bestFlavor, kUnicodeMime)) { nsCOMPtr textDataObj(do_QueryInterface(genericDataObj)); if (textDataObj && len > 0) @@ -1061,7 +1060,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable rv = InsertTextAt(stuffToPaste, aDestinationNode, aDestOffset, aDoDeleteSelection); } } - else if (flavor.Equals(NS_LITERAL_STRING(kFileMime))) + else if (0 == nsCRT::strcmp(bestFlavor, kFileMime)) { nsCOMPtr fileObj(do_QueryInterface(genericDataObj)); if (fileObj && len > 0) @@ -1117,7 +1116,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable } } } - else if (flavor.Equals(NS_LITERAL_STRING(kJPEGImageMime))) + else if (0 == nsCRT::strcmp(bestFlavor, kJPEGImageMime)) { // need to provide a hook from here // Insert Image code here @@ -1776,20 +1775,18 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsPlaintextQuotation(PRInt32 aSelectionType) #endif return rv; } + + if (flav && 0 == nsCRT::strcmp((flav), kUnicodeMime)) + { #ifdef DEBUG_clipboard printf("Got flavor [%s]\n", flav); #endif - nsAutoString flavor; flavor.AssignWithConversion(flav); - nsAutoString stuffToPaste; - if (flavor.Equals(NS_LITERAL_STRING(kUnicodeMime))) - { nsCOMPtr textDataObj(do_QueryInterface(genericDataObj)); if (textDataObj && len > 0) { - nsAutoString text; - textDataObj->GetData(text); - NS_ASSERTION(text.Length() <= (len/2), "Invalid length!"); - stuffToPaste.Assign(text.get(), len / 2); + nsAutoString stuffToPaste; + textDataObj->GetData(stuffToPaste); + NS_ASSERTION(stuffToPaste.Length() <= (len/2), "Invalid length!"); nsAutoEditBatch beginBatching(this); rv = InsertAsPlaintextQuotation(stuffToPaste, PR_TRUE, 0); } @@ -2010,6 +2007,18 @@ nsHTMLEditor::InsertAsPlaintextQuotation(const nsAString & aQuotedText, return rv; } +NS_IMETHODIMP +nsHTMLEditor::StripCites() +{ + return nsPlaintextEditor::StripCites(); +} + +NS_IMETHODIMP +nsHTMLEditor::Rewrap(PRBool aRespectNewlines) +{ + return nsPlaintextEditor::Rewrap(aRespectNewlines); +} + NS_IMETHODIMP nsHTMLEditor::InsertAsCitedQuotation(const nsAString & aQuotedText, const nsAString & aCitation, diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp index e735a62c9ff..509a9f36a62 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp @@ -1664,12 +1664,6 @@ nsHTMLEditor::GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver) return result; } -NS_IMETHODIMP -nsHTMLEditor::CollapseSelectionToStart() -{ - return BeginningOfDocument(); -} - nsresult nsHTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(nsISelection *aSelection, nsIDOMNode *aNode) { diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.h b/mozilla/editor/libeditor/html/nsHTMLEditor.h index f0351e722ed..bc09f5ff55a 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.h +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.h @@ -145,7 +145,6 @@ public: /* ------------ nsPlaintextEditor overrides -------------- */ NS_IMETHODIMP HandleKeyPress(nsIDOMKeyEvent* aKeyEvent); - NS_IMETHODIMP CollapseSelectionToStart(); NS_IMETHOD GetIsDocumentEditable(PRBool *aIsDocumentEditable); NS_IMETHODIMP BeginningOfDocument(); @@ -199,17 +198,7 @@ public: /* ------------ nsIEditorMailSupport methods -------------- */ - NS_IMETHOD PasteAsQuotation(PRInt32 aSelectionType); - NS_IMETHOD InsertTextWithQuotations(const nsAString & aQuotedText); - NS_IMETHOD InsertAsQuotation(const nsAString & aQuotedText, - nsIDOMNode **aNodeInserted); - NS_IMETHOD PasteAsCitedQuotation(const nsAString & aCitation, - PRInt32 aSelectionType); - NS_IMETHOD InsertAsCitedQuotation(const nsAString & aQuotedText, - const nsAString & aCitation, - PRBool aInsertHTML, - nsIDOMNode **aNodeInserted); - NS_IMETHOD GetEmbeddedObjects(nsISupportsArray** aNodeList); + NS_DECL_NSIEDITORMAILSUPPORT /* ------------ nsITableEditor methods -------------- */ diff --git a/mozilla/editor/libeditor/html/nsTableEditor.cpp b/mozilla/editor/libeditor/html/nsTableEditor.cpp index 170e9789efa..fbd90ac4164 100644 --- a/mozilla/editor/libeditor/html/nsTableEditor.cpp +++ b/mozilla/editor/libeditor/html/nsTableEditor.cpp @@ -174,20 +174,6 @@ nsHTMLEditor::InsertCell(nsIDOMElement *aCell, PRInt32 aRowSpan, PRInt32 aColSpa return InsertNode(newCell, cellParent, cellOffset); } -static -PRBool IsRowNode(nsIDOMNode *aNode) -{ - nsCOMPtr atom; - nsCOMPtr content = do_QueryInterface(aNode); - if (content) - { - content->GetTag(getter_AddRefs(atom)); - if (atom && atom.get() == nsEditProperty::tr) - return PR_TRUE; - } - return PR_FALSE; -} - NS_IMETHODIMP nsHTMLEditor::SetColSpan(nsIDOMElement *aCell, PRInt32 aColSpan) { if (!aCell) return NS_ERROR_NULL_POINTER; @@ -269,11 +255,8 @@ nsHTMLEditor::GetFirstRow(nsIDOMElement* aTableElement, nsIDOMNode** aRowNode) if (NS_FAILED(res)) return res; if (!tableElement) return NS_ERROR_NULL_POINTER; - nsCOMPtrtableNode = do_QueryInterface(tableElement); - if (!tableNode) return NS_ERROR_NULL_POINTER; - nsCOMPtr tableChild; - res = tableNode->GetFirstChild(getter_AddRefs(tableChild)); + res = tableElement->GetFirstChild(getter_AddRefs(tableChild)); if (NS_FAILED(res)) return res; while (tableChild) @@ -283,7 +266,7 @@ nsHTMLEditor::GetFirstRow(nsIDOMElement* aTableElement, nsIDOMNode** aRowNode) { nsCOMPtr atom; content->GetTag(getter_AddRefs(atom)); - if (atom.get() == nsEditProperty::tr) + if (atom == nsEditProperty::tr) { // Found a row directly under *aRowNode = tableChild.get(); @@ -291,16 +274,16 @@ nsHTMLEditor::GetFirstRow(nsIDOMElement* aTableElement, nsIDOMNode** aRowNode) return NS_OK; } // Look for row in one of the row container elements - if (atom.get() == nsEditProperty::tbody || - atom.get() == nsEditProperty::thead || - atom.get() == nsEditProperty::tfoot ) + if (atom == nsEditProperty::tbody || + atom == nsEditProperty::thead || + atom == nsEditProperty::tfoot ) { nsCOMPtr rowNode; res = tableChild->GetFirstChild(getter_AddRefs(rowNode)); if (NS_FAILED(res)) return res; // We can encounter "__moz_text" nodes here -- must find a row - while (rowNode && !IsRowNode(rowNode)) + while (rowNode && !nsHTMLEditUtils::IsTableRow(rowNode)) { nsCOMPtr nextNode; res = rowNode->GetNextSibling(getter_AddRefs(nextNode)); @@ -339,17 +322,17 @@ nsHTMLEditor::GetNextRow(nsIDOMNode* aCurrentRowNode, nsIDOMNode **aRowNode) if (!aCurrentRowNode) return NS_ERROR_NULL_POINTER; - if (!IsRowNode(aCurrentRowNode)) + if (!nsHTMLEditUtils::IsTableRow(aCurrentRowNode)) return NS_ERROR_FAILURE; nsCOMPtr nextRow; - nsCOMPtr nextNode; - nsresult res = aCurrentRowNode->GetNextSibling(getter_AddRefs(nextRow)); if (NS_FAILED(res)) return res; + nsCOMPtr nextNode; + // Skip over any "__moz_text" nodes here - while (nextRow && !IsRowNode(nextRow)) + while (nextRow && !nsHTMLEditUtils::IsTableRow(nextRow)) { res = nextRow->GetNextSibling(getter_AddRefs(nextNode)); if (NS_FAILED(res)) return res; @@ -365,11 +348,11 @@ nsHTMLEditor::GetNextRow(nsIDOMNode* aCurrentRowNode, nsIDOMNode **aRowNode) // No row found, search for rows in other table sections nsCOMPtr rowParent; - nsCOMPtr parentSibling; res = aCurrentRowNode->GetParentNode(getter_AddRefs(rowParent)); if (NS_FAILED(res)) return res; if (!rowParent) return NS_ERROR_NULL_POINTER; + nsCOMPtr parentSibling; res = rowParent->GetNextSibling(getter_AddRefs(parentSibling)); if (NS_FAILED(res)) return res; @@ -379,7 +362,7 @@ nsHTMLEditor::GetNextRow(nsIDOMNode* aCurrentRowNode, nsIDOMNode **aRowNode) if (NS_FAILED(res)) return res; // We can encounter "__moz_text" nodes here -- must find a row - while (nextRow && !IsRowNode(nextRow)) + while (nextRow && !nsHTMLEditUtils::IsTableRow(nextRow)) { res = nextRow->GetNextSibling(getter_AddRefs(nextNode)); if (NS_FAILED(res)) return res; @@ -614,8 +597,6 @@ nsHTMLEditor::InsertTableColumn(PRInt32 aNumber, PRBool aAfter) if (rowNode) { nsCOMPtr lastCell; - if (!rowNode) return NS_ERROR_FAILURE; - res = GetLastCellInRow(rowNode, getter_AddRefs(lastCell)); if (NS_FAILED(res)) return res; if (!lastCell) return NS_ERROR_FAILURE; @@ -643,7 +624,6 @@ nsHTMLEditor::InsertTableRow(PRInt32 aNumber, PRBool aAfter) nsCOMPtr selection; nsCOMPtr table; nsCOMPtr curCell; - nsCOMPtr cellForRowParent; PRInt32 startRowIndex, startColIndex; nsresult res = GetCellContext(nsnull, @@ -692,6 +672,7 @@ nsHTMLEditor::InsertTableRow(PRInt32 aNumber, PRBool aAfter) //...so suppress Rules System selection munging nsAutoTxnsConserveSelection dontChangeSelection(this); + nsCOMPtr cellForRowParent; PRInt32 cellsInRow = 0; if (startRowIndex < rowCount) { @@ -788,7 +769,6 @@ nsHTMLEditor::InsertTableRow(PRInt32 aNumber, PRBool aAfter) else return NS_ERROR_FAILURE; - NS_NAMED_LITERAL_STRING(tdStr, "td"); for (PRInt32 row = 0; row < aNumber; row++) { // Create a new row @@ -801,7 +781,7 @@ nsHTMLEditor::InsertTableRow(PRInt32 aNumber, PRBool aAfter) for (PRInt32 i = 0; i < cellsInRow; i++) { nsCOMPtr newCell; - res = CreateElementWithDefaults(tdStr, getter_AddRefs(newCell)); + res = CreateElementWithDefaults(NS_LITERAL_STRING("td"), getter_AddRefs(newCell)); if (NS_FAILED(res)) return res; if (!newCell) return NS_ERROR_FAILURE; @@ -839,9 +819,7 @@ nsHTMLEditor::DeleteTable2(nsIDOMElement *aTable, nsISelection *aSelection) if (NS_FAILED(res)) return res; // Place selection just before the table - aSelection->Collapse(tableParent, tableOffset); - - return NS_OK; + return aSelection->Collapse(tableParent, tableOffset); } NS_IMETHODIMP @@ -856,10 +834,7 @@ nsHTMLEditor::DeleteTable() if (NS_FAILED(res)) return res; nsAutoEditBatch beginBatching(this); - res = DeleteTable2(table, selection); - if (NS_FAILED(res)) return res; - - return NS_OK; + return DeleteTable2(table, selection); } NS_IMETHODIMP @@ -1063,8 +1038,7 @@ nsHTMLEditor::DeleteTableCellContents() nsCOMPtr table; nsCOMPtr cell; PRInt32 startRowIndex, startColIndex; - nsresult res = NS_OK; - + nsresult res; res = GetCellContext(getter_AddRefs(selection), getter_AddRefs(table), getter_AddRefs(cell), @@ -1528,13 +1502,10 @@ nsHTMLEditor::SelectTable() // Don't fail if we didn't find a table if (!table) return NS_OK; - nsCOMPtr tableNode = do_QueryInterface(table); - if (tableNode) - { - res = ClearSelection(); - if (NS_SUCCEEDED(res)) - res = AppendNodeToSelectionAsRange(table); - } + res = ClearSelection(); + if (NS_SUCCEEDED(res)) + res = AppendNodeToSelectionAsRange(table); + return res; } @@ -1544,16 +1515,12 @@ nsHTMLEditor::SelectTableCell() nsCOMPtr cell; nsresult res = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nsnull, getter_AddRefs(cell)); if (NS_FAILED(res)) return res; - // Don't fail if we didn't find a table if (!cell) return NS_EDITOR_ELEMENT_NOT_FOUND; - nsCOMPtr cellNode = do_QueryInterface(cell); - if (cellNode) - { - res = ClearSelection(); - if (NS_SUCCEEDED(res)) - res = AppendNodeToSelectionAsRange(cellNode); - } + res = ClearSelection(); + if (NS_SUCCEEDED(res)) + res = AppendNodeToSelectionAsRange(cell); + return res; } @@ -1639,8 +1606,7 @@ nsHTMLEditor::SelectBlockOfCells(nsIDOMElement *aStartCell, nsIDOMElement *aEndC // Skip cells that already selected or are spanned from previous locations if (!isSelected && cell && row == currentRowIndex && col == currentColIndex) { - nsCOMPtr cellNode = do_QueryInterface(cell); - res = AppendNodeToSelectionAsRange(cellNode); + res = AppendNodeToSelectionAsRange(cell); if (NS_FAILED(res)) break; } } @@ -1658,8 +1624,6 @@ nsHTMLEditor::SelectAllTableCells() // Don't fail if we didn't find a cell if (!cell) return NS_EDITOR_ELEMENT_NOT_FOUND; - nsCOMPtr cellNode = do_QueryInterface(cell); - if (!cellNode) return NS_ERROR_FAILURE; nsCOMPtr startCell = cell; // Get parent table @@ -1701,8 +1665,7 @@ nsHTMLEditor::SelectAllTableCells() // Skip cells that are spanned from previous rows or columns if (cell && row == currentRowIndex && col == currentColIndex) { - cellNode = do_QueryInterface(cell); - res = AppendNodeToSelectionAsRange(cellNode); + res = AppendNodeToSelectionAsRange(cell); if (NS_FAILED(res)) break; cellSelected = PR_TRUE; } @@ -1711,8 +1674,7 @@ nsHTMLEditor::SelectAllTableCells() // Safety code to select starting cell if nothing else was selected if (!cellSelected) { - cellNode = do_QueryInterface(startCell); - return AppendNodeToSelectionAsRange(cellNode); + return AppendNodeToSelectionAsRange(startCell); } return res; } @@ -1728,9 +1690,6 @@ nsHTMLEditor::SelectTableRow() if (!cell) return NS_EDITOR_ELEMENT_NOT_FOUND; nsCOMPtr startCell = cell; - nsCOMPtr cellNode = do_QueryInterface(cell); - if (!cellNode) return NS_ERROR_FAILURE; - // Get table and location of cell: nsCOMPtr selection; nsCOMPtr table; @@ -1773,8 +1732,7 @@ nsHTMLEditor::SelectTableRow() // Skip cells that are spanned from previous rows or columns if (cell && currentRowIndex == startRowIndex && currentColIndex == col) { - cellNode = do_QueryInterface(cell); - res = AppendNodeToSelectionAsRange(cellNode); + res = AppendNodeToSelectionAsRange(cell); if (NS_FAILED(res)) break; cellSelected = PR_TRUE; } @@ -1782,8 +1740,7 @@ nsHTMLEditor::SelectTableRow() // Safety code to select starting cell if nothing else was selected if (!cellSelected) { - cellNode = do_QueryInterface(startCell); - return AppendNodeToSelectionAsRange(cellNode); + return AppendNodeToSelectionAsRange(startCell); } return res; } @@ -1798,8 +1755,6 @@ nsHTMLEditor::SelectTableColumn() // Don't fail if we didn't find a cell if (!cell) return NS_EDITOR_ELEMENT_NOT_FOUND; - nsCOMPtr cellNode = do_QueryInterface(cell); - if (!cellNode) return NS_ERROR_FAILURE; nsCOMPtr startCell = cell; // Get location of cell: @@ -1840,8 +1795,7 @@ nsHTMLEditor::SelectTableColumn() // Skip cells that are spanned from previous rows or columns if (cell && currentRowIndex == row && currentColIndex == startColIndex) { - cellNode = do_QueryInterface(cell); - res = AppendNodeToSelectionAsRange(cellNode); + res = AppendNodeToSelectionAsRange(cell); if (NS_FAILED(res)) break; cellSelected = PR_TRUE; } @@ -1849,8 +1803,7 @@ nsHTMLEditor::SelectTableColumn() // Safety code to select starting cell if nothing else was selected if (!cellSelected) { - cellNode = do_QueryInterface(startCell); - return AppendNodeToSelectionAsRange(cellNode); + return AppendNodeToSelectionAsRange(startCell); } return res; } @@ -2100,16 +2053,8 @@ nsHTMLEditor::SwitchTableCellHeaderType(nsIDOMElement *aSourceCell, nsIDOMElemen // Prevent auto insertion of BR in new cell created by ReplaceContainer nsAutoRules beginRulesSniffing(this, kOpInsertNode, nsIEditor::eNext); - nsCOMPtr sourceNode = do_QueryInterface(aSourceCell); nsCOMPtr newNode; - // Set to the opposite of current type - nsAutoString tagName; - GetTagString(aSourceCell, tagName); - NS_NAMED_LITERAL_STRING(tdType, "td"); - NS_NAMED_LITERAL_STRING(thType, "th"); - nsString newCellType( (tagName == tdType) ? thType : tdType ); - // Save current selection to restore when done // This is needed so ReplaceContainer can monitor selection // when replacing nodes @@ -2119,9 +2064,13 @@ nsHTMLEditor::SwitchTableCellHeaderType(nsIDOMElement *aSourceCell, nsIDOMElemen if (!selection) return NS_ERROR_FAILURE; nsAutoSelectionReset selectionResetter(selection, this); + // Set to the opposite of current type + nsCOMPtr atom = nsEditor::GetTag(aSourceCell); + nsString newCellType( (atom == nsEditProperty::th) ? NS_LITERAL_STRING("th") : NS_LITERAL_STRING("td")); + // This creates new node, moves children, copies attributes (PR_TRUE) // and manages the selection! - res = ReplaceContainer(sourceNode, address_of(newNode), newCellType, nsnull, nsnull, PR_TRUE); + res = ReplaceContainer(aSourceCell, address_of(newNode), newCellType, nsnull, nsnull, PR_TRUE); if (NS_FAILED(res)) return res; if (!newNode) return NS_ERROR_FAILURE; @@ -2488,9 +2437,7 @@ nsHTMLEditor::MergeCells(nsCOMPtr aTargetCell, nsCOMPtr aCellToMerge, PRBool aDeleteCellToMerge) { - nsCOMPtr targetCell = do_QueryInterface(aTargetCell); - nsCOMPtr cellToMerge = do_QueryInterface(aCellToMerge); - if(!targetCell || !cellToMerge) return NS_ERROR_NULL_POINTER; + if (!aTargetCell || !aCellToMerge) return NS_ERROR_NULL_POINTER; nsresult res = NS_OK; @@ -2503,7 +2450,7 @@ nsHTMLEditor::MergeCells(nsCOMPtr aTargetCell, // Get index of last child in target cell nsCOMPtr childNodes; nsCOMPtr cellChild; - res = targetCell->GetChildNodes(getter_AddRefs(childNodes)); + res = aTargetCell->GetChildNodes(getter_AddRefs(childNodes)); // If we fail or don't have children, // we insert at index 0 PRInt32 insertIndex = 0; @@ -2516,8 +2463,6 @@ nsHTMLEditor::MergeCells(nsCOMPtr aTargetCell, if (NS_FAILED(res)) return res; if (len == 1 && IsEmptyCell(aTargetCell)) { - if (IsEmptyCell(aTargetCell)) - { // Delete the empty node nsCOMPtr tempNode; res = childNodes->Item(0, getter_AddRefs(cellChild)); @@ -2525,7 +2470,6 @@ nsHTMLEditor::MergeCells(nsCOMPtr aTargetCell, res = DeleteNode(cellChild); if (NS_FAILED(res)) return res; insertIndex = 0; - } } else insertIndex = (PRInt32)len; @@ -2533,23 +2477,23 @@ nsHTMLEditor::MergeCells(nsCOMPtr aTargetCell, // Move the contents PRBool hasChild; - cellToMerge->HasChildNodes(&hasChild); + aCellToMerge->HasChildNodes(&hasChild); while (hasChild) { - cellToMerge->GetLastChild(getter_AddRefs(cellChild)); + aCellToMerge->GetLastChild(getter_AddRefs(cellChild)); res = DeleteNode(cellChild); if (NS_FAILED(res)) return res; - res = InsertNode(cellChild, targetCell, insertIndex); + res = InsertNode(cellChild, aTargetCell, insertIndex); if (NS_FAILED(res)) return res; - cellToMerge->HasChildNodes(&hasChild); + aCellToMerge->HasChildNodes(&hasChild); } } // Delete cells whose contents were moved if (aDeleteCellToMerge) - res = DeleteNode(cellToMerge); + res = DeleteNode(aCellToMerge); return res; } @@ -2848,7 +2792,7 @@ nsHTMLEditor::GetTableSize(nsIDOMElement *aTable, { NS_ENSURE_ARG_POINTER(aRowCount); NS_ENSURE_ARG_POINTER(aColCount); - nsresult res = NS_ERROR_FAILURE; + nsresult res; *aRowCount = 0; *aColCount = 0; nsCOMPtr table; @@ -2882,6 +2826,8 @@ nsHTMLEditor::GetCellDataAt(nsIDOMElement* aTable, PRInt32 aRowIndex, NS_ENSURE_ARG_POINTER(aActualRowSpan); NS_ENSURE_ARG_POINTER(aActualColSpan); NS_ENSURE_ARG_POINTER(aIsSelected); + if (!aCell) return NS_ERROR_NULL_POINTER; + nsresult res=NS_ERROR_FAILURE; *aStartRowIndex = 0; *aStartColIndex = 0; @@ -2891,7 +2837,6 @@ nsHTMLEditor::GetCellDataAt(nsIDOMElement* aTable, PRInt32 aRowIndex, *aActualColSpan = 0; *aIsSelected = PR_FALSE; - if (!aCell) return NS_ERROR_NULL_POINTER; *aCell = nsnull; if (!aTable) @@ -3000,7 +2945,7 @@ nsHTMLEditor::GetCellContext(nsISelection **aSelection, res = GetSelectedOrParentTableElement(tagName, &selectedCount, getter_AddRefs(cellOrTableElement)); if (NS_FAILED(res)) return res; - if (tagName == NS_LITERAL_STRING("table")) + if (tagName.Equals(NS_LITERAL_STRING("table"))) { // We have a selected table, not a cell if (aTable) @@ -3010,8 +2955,7 @@ nsHTMLEditor::GetCellContext(nsISelection **aSelection, } return NS_OK; } - // Don't fail if we are not in a cell - if (tagName != NS_LITERAL_STRING("td")) + if (!tagName.Equals(NS_LITERAL_STRING("td"))) return NS_EDITOR_ELEMENT_NOT_FOUND; // We found a cell @@ -3130,8 +3074,6 @@ nsHTMLEditor::GetFirstSelectedCell(nsIDOMRange **aRange, nsIDOMElement **aCell) mSelectedCellIndex = 0; - nsCOMPtr cellNode; - res = GetCellFromRange(range, aCell); // Failure here probably means selection is in a text node, // so there's no selected cell @@ -3246,11 +3188,10 @@ NS_IMETHODIMP nsHTMLEditor::SetSelectionAfterTableEdit(nsIDOMElement* aTable, PRInt32 aRow, PRInt32 aCol, PRInt32 aDirection, PRBool aSelected) { - nsresult res = NS_ERROR_NOT_INITIALIZED; - if (!aTable) return res; + if (!aTable) return NS_ERROR_NOT_INITIALIZED; nsCOMPtrselection; - res = GetSelection(getter_AddRefs(selection)); + nsresult res = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(res)) return res; if (!selection) @@ -3265,7 +3206,6 @@ nsHTMLEditor::SetSelectionAfterTableEdit(nsIDOMElement* aTable, PRInt32 aRow, PR PRBool done = PR_FALSE; do { res = GetCellAt(aTable, aRow, aCol, getter_AddRefs(cell)); - nsCOMPtr cellNode = do_QueryInterface(cell); if (NS_SUCCEEDED(res)) { if (cell) @@ -3281,7 +3221,7 @@ nsHTMLEditor::SetSelectionAfterTableEdit(nsIDOMElement* aTable, PRInt32 aRow, PR // but don't go into nested tables // TODO: Should we really be placing the caret at the END // of the cell content? - return CollapseSelectionToDeepestNonTableFirstChild(selection, cellNode); + return CollapseSelectionToDeepestNonTableFirstChild(selection, cell); } } else { // Setup index to find another cell in the @@ -3351,13 +3291,13 @@ nsHTMLEditor::GetSelectedOrParentTableElement(nsAString& aTagName, if (NS_FAILED(res)) return res; if (!selection) return NS_ERROR_FAILURE; - NS_NAMED_LITERAL_STRING(tdName, "td"); - // Try to get the first selected cell nsCOMPtr tableOrCellElement; res = GetFirstSelectedCell(nsnull, getter_AddRefs(tableOrCellElement)); if (NS_FAILED(res)) return res; + NS_NAMED_LITERAL_STRING(tdName, "td"); + if (tableOrCellElement) { // Each cell is in its own selection range, @@ -3390,14 +3330,12 @@ nsHTMLEditor::GetSelectedOrParentTableElement(nsAString& aTagName, selectedNode = anchorNode; // If anchor doesn't have a child, we can't be selecting a table element, // so don't do the following: - } else { - nsAutoString tag; - GetTagString(selectedNode,tag); + } + else + { + nsCOMPtr atom = nsEditor::GetTag(selectedNode); - NS_NAMED_LITERAL_STRING(tableName, "table"); - NS_NAMED_LITERAL_STRING(trName, "tr"); - - if (tag == tdName) + if (atom == nsEditProperty::td) { tableOrCellElement = do_QueryInterface(selectedNode); aTagName = tdName; @@ -3406,16 +3344,16 @@ nsHTMLEditor::GetSelectedOrParentTableElement(nsAString& aTagName, res = selection->GetRangeCount(aSelectedCount); if (NS_FAILED(res)) return res; } - else if(tag == tableName) + else if (atom == nsEditProperty::table) { tableOrCellElement = do_QueryInterface(selectedNode); - aTagName = tableName; + aTagName = NS_LITERAL_STRING("table"); *aSelectedCount = 1; } - else if(tag == trName) + else if (atom == nsEditProperty::tr) { tableOrCellElement = do_QueryInterface(selectedNode); - aTagName = trName; + aTagName = NS_LITERAL_STRING("tr"); *aSelectedCount = 1; } } diff --git a/mozilla/editor/libeditor/text/nsPlaintextDataTransfer.cpp b/mozilla/editor/libeditor/text/nsPlaintextDataTransfer.cpp index 32de4c4a213..f57998ad598 100644 --- a/mozilla/editor/libeditor/text/nsPlaintextDataTransfer.cpp +++ b/mozilla/editor/libeditor/text/nsPlaintextDataTransfer.cpp @@ -38,7 +38,6 @@ #include "nsPlaintextEditor.h" -#include "nsTextEditUtils.h" #include "nsIDOMDocument.h" #include "nsIDocument.h" @@ -64,14 +63,11 @@ // Misc #include "nsEditorUtils.h" -const PRUnichar nbsp = 160; NS_IMETHODIMP nsPlaintextEditor::PrepareTransferable(nsITransferable **transferable) { // Create generic Transferable for getting the data - nsresult rv = nsComponentManager::CreateInstance("@mozilla.org/widget/transferable;1", nsnull, - NS_GET_IID(nsITransferable), - (void**)transferable); + nsresult rv = CallCreateInstance("@mozilla.org/widget/transferable;1", transferable); if (NS_FAILED(rv)) return rv; @@ -120,23 +116,18 @@ NS_IMETHODIMP nsPlaintextEditor::InsertTextFromTransferable(nsITransferable *aTr char* bestFlavor = nsnull; nsCOMPtr genericDataObj; PRUint32 len = 0; - if ( NS_SUCCEEDED(aTransferable->GetAnyTransferData(&bestFlavor, getter_AddRefs(genericDataObj), &len)) ) + if (NS_SUCCEEDED(aTransferable->GetAnyTransferData(&bestFlavor, getter_AddRefs(genericDataObj), &len)) + && bestFlavor && 0 == nsCRT::strcmp(bestFlavor, kUnicodeMime)) { nsAutoTxnsConserveSelection dontSpazMySelection(this); - nsAutoString flavor, stuffToPaste; - flavor.AssignWithConversion( bestFlavor ); // just so we can use flavor.Equals() - if (flavor.Equals(NS_LITERAL_STRING(kUnicodeMime))) + nsCOMPtr textDataObj ( do_QueryInterface(genericDataObj) ); + if (textDataObj && len > 0) { - nsCOMPtr textDataObj ( do_QueryInterface(genericDataObj) ); - if (textDataObj && len > 0) - { - nsAutoString text; - textDataObj->GetData ( text ); - NS_ASSERTION(text.Length() <= (len/2), "Invalid length!"); - stuffToPaste.Assign ( text.get(), len / 2 ); - nsAutoEditBatch beginBatching(this); - rv = InsertTextAt(stuffToPaste, aDestinationNode, aDestOffset, aDoDeleteSelection); - } + nsAutoString stuffToPaste; + textDataObj->GetData(stuffToPaste); + NS_ASSERTION(stuffToPaste.Length() <= (len/2), "Invalid length!"); + nsAutoEditBatch beginBatching(this); + rv = InsertTextAt(stuffToPaste, aDestinationNode, aDestOffset, aDoDeleteSelection); } } nsCRT::free(bestFlavor); @@ -161,10 +152,13 @@ NS_IMETHODIMP nsPlaintextEditor::InsertFromDrop(nsIDOMEvent* aDropEvent) dragService->GetCurrentSession(getter_AddRefs(dragSession)); if (!dragSession) return NS_OK; + // Current doc is destination + nsCOMPtr destdomdoc; + rv = GetDocument(getter_AddRefs(destdomdoc)); + if (NS_FAILED(rv)) return rv; + // transferable hooks - nsCOMPtr domdoc; - GetDocument(getter_AddRefs(domdoc)); - if (!nsEditorHookUtils::DoAllowDropHook(domdoc, aDropEvent, dragSession)) + if (!nsEditorHookUtils::DoAllowDropHook(destdomdoc, aDropEvent, dragSession)) return NS_OK; // Get the nsITransferable interface for getting the data from the drop @@ -176,126 +170,115 @@ NS_IMETHODIMP nsPlaintextEditor::InsertFromDrop(nsIDOMEvent* aDropEvent) PRUint32 numItems = 0; rv = dragSession->GetNumDropItems(&numItems); if (NS_FAILED(rv)) return rv; + if (numItems < 1) return NS_ERROR_FAILURE; // nothing to drop? // Combine any deletion and drop insertion into one transaction nsAutoEditBatch beginBatching(this); PRBool deleteSelection = PR_FALSE; + + // We have to figure out whether to delete and relocate caret only once + // Parent and offset are under the mouse cursor + nsCOMPtr nsuiEvent (do_QueryInterface(aDropEvent)); + if (!nsuiEvent) return NS_ERROR_FAILURE; + nsCOMPtr newSelectionParent; - PRInt32 newSelectionOffset = 0; + rv = nsuiEvent->GetRangeParent(getter_AddRefs(newSelectionParent)); + if (NS_FAILED(rv)) return rv; + if (!newSelectionParent) return NS_ERROR_FAILURE; + + PRInt32 newSelectionOffset; + rv = nsuiEvent->GetRangeOffset(&newSelectionOffset); + if (NS_FAILED(rv)) return rv; + + nsCOMPtr selection; + rv = GetSelection(getter_AddRefs(selection)); + if (NS_FAILED(rv)) return rv; + if (!selection) return NS_ERROR_FAILURE; + + PRBool isCollapsed; + rv = selection->GetIsCollapsed(&isCollapsed); + if (NS_FAILED(rv)) return rv; + + // Check if mouse is in the selection + // if so, jump through some hoops to determine if mouse is over selection (bail) + // and whether user wants to copy selection or delete it + if (!isCollapsed) + { + // We never have to delete if selection is already collapsed + PRBool cursorIsInSelection = PR_FALSE; + + PRInt32 rangeCount; + rv = selection->GetRangeCount(&rangeCount); + if (NS_FAILED(rv)) return rv; + + for (PRInt32 j = 0; j < rangeCount; j++) + { + nsCOMPtr range; + rv = selection->GetRangeAt(j, getter_AddRefs(range)); + nsCOMPtr nsrange(do_QueryInterface(range)); + if (NS_FAILED(rv) || !nsrange) + continue; // don't bail yet, iterate through them all + + rv = nsrange->IsPointInRange(newSelectionParent, newSelectionOffset, &cursorIsInSelection); + if (cursorIsInSelection) + break; + } + + // Source doc is null if source is *not* the current editor document + // Current doc is destination (set earlier) + nsCOMPtr srcdomdoc; + rv = dragSession->GetSourceDocument(getter_AddRefs(srcdomdoc)); + if (NS_FAILED(rv)) return rv; + + if (cursorIsInSelection) + { + // Dragging within same doc can't drop on itself -- leave! + if (srcdomdoc == destdomdoc) + return NS_OK; + + // Dragging from another window onto a selection + // XXX Decision made to NOT do this, + // note that 4.x does replace if dropped on + //deleteSelection = PR_TRUE; + } + else + { + // We are NOT over the selection + if (srcdomdoc == destdomdoc) + { + // Within the same doc: delete if user doesn't want to copy + + // check if the user pressed the key to force a copy rather than a move + // if we run into problems here, we'll just assume the user doesn't want a copy + PRBool userWantsCopy = PR_FALSE; + + nsCOMPtr mouseEvent ( do_QueryInterface(aDropEvent) ); + if (mouseEvent) +#if defined(XP_MAC) || defined(XP_MACOSX) + mouseEvent->GetAltKey(&userWantsCopy); +#else + mouseEvent->GetCtrlKey(&userWantsCopy); +#endif + + deleteSelection = !userWantsCopy; + } + else + { + // Different source doc: Don't delete + deleteSelection = PR_FALSE; + } + } + } PRUint32 i; - PRBool doPlaceCaret = PR_TRUE; for (i = 0; i < numItems; ++i) { rv = dragSession->GetData(trans, i); if (NS_FAILED(rv)) return rv; if (!trans) return NS_OK; // NS_ERROR_FAILURE; Should we fail? - if ( doPlaceCaret ) - { - // check if the user pressed the key to force a copy rather than a move - // if we run into problems here, we'll just assume the user doesn't want a copy - PRBool userWantsCopy = PR_FALSE; - - nsCOMPtr nsuiEvent (do_QueryInterface(aDropEvent)); - if (!nsuiEvent) return NS_ERROR_FAILURE; - - nsCOMPtr mouseEvent ( do_QueryInterface(aDropEvent) ); - if (mouseEvent) - -#if defined(XP_MAC) || defined(XP_MACOSX) - mouseEvent->GetAltKey(&userWantsCopy); -#else - mouseEvent->GetCtrlKey(&userWantsCopy); -#endif - // Source doc is null if source is *not* the current editor document - nsCOMPtr srcdomdoc; - rv = dragSession->GetSourceDocument(getter_AddRefs(srcdomdoc)); - if (NS_FAILED(rv)) return rv; - - // Current doc is destination - nsCOMPtrdestdomdoc; - rv = GetDocument(getter_AddRefs(destdomdoc)); - if (NS_FAILED(rv)) return rv; - - nsCOMPtr selection; - rv = GetSelection(getter_AddRefs(selection)); - if (NS_FAILED(rv)) return rv; - if (!selection) return NS_ERROR_FAILURE; - - PRBool isCollapsed; - rv = selection->GetIsCollapsed(&isCollapsed); - if (NS_FAILED(rv)) return rv; - - // Parent and offset under the mouse cursor - rv = nsuiEvent->GetRangeParent(getter_AddRefs(newSelectionParent)); - if (NS_FAILED(rv)) return rv; - if (!newSelectionParent) return NS_ERROR_FAILURE; - - rv = nsuiEvent->GetRangeOffset(&newSelectionOffset); - if (NS_FAILED(rv)) return rv; - - // We never have to delete if selection is already collapsed - PRBool cursorIsInSelection = PR_FALSE; - - // Check if mouse is in the selection - if (!isCollapsed) - { - PRInt32 rangeCount; - rv = selection->GetRangeCount(&rangeCount); - if (NS_FAILED(rv)) - return rv; - - for (PRInt32 j = 0; j < rangeCount; j++) - { - nsCOMPtr range; - - rv = selection->GetRangeAt(j, getter_AddRefs(range)); - if (NS_FAILED(rv) || !range) - continue;//dont bail yet, iterate through them all - - nsCOMPtr nsrange(do_QueryInterface(range)); - if (NS_FAILED(rv) || !nsrange) - continue;//dont bail yet, iterate through them all - - rv = nsrange->IsPointInRange(newSelectionParent, newSelectionOffset, &cursorIsInSelection); - if(cursorIsInSelection) - break; - } - if (cursorIsInSelection) - { - // Dragging within same doc can't drop on itself -- leave! - // (We shouldn't get here - drag event shouldn't have started if over selection) - if (srcdomdoc == destdomdoc) - return NS_OK; - - // Dragging from another window onto a selection - // XXX Decision made to NOT do this, - // note that 4.x does replace if dropped on - //deleteSelection = PR_TRUE; - } - else - { - // We are NOT over the selection - if (srcdomdoc == destdomdoc) - { - // Within the same doc: delete if user doesn't want to copy - deleteSelection = !userWantsCopy; - } - else - { - // Different source doc: Don't delete - deleteSelection = PR_FALSE; - } - } - } - - // We have to figure out whether to delete and relocate caret only once - doPlaceCaret = PR_FALSE; - } - - if (!nsEditorHookUtils::DoInsertionHook(domdoc, aDropEvent, trans)) + if (!nsEditorHookUtils::DoInsertionHook(destdomdoc, aDropEvent, trans)) return NS_OK; rv = InsertTextFromTransferable(trans, newSelectionParent, newSelectionOffset, deleteSelection); @@ -337,7 +320,6 @@ NS_IMETHODIMP nsPlaintextEditor::CanDrag(nsIDOMEvent *aDragEvent, PRBool *aCanDr nsCOMPtr eventTarget; nsCOMPtr nsevent(do_QueryInterface(aDragEvent)); - if (nsevent) { res = nsevent->GetTmpRealOriginalTarget(getter_AddRefs(eventTarget)); if (NS_FAILED(res)) return res; @@ -382,7 +364,7 @@ NS_IMETHODIMP nsPlaintextEditor::DoDrag(nsIDOMEvent *aDragEvent) /* create an array of transferables */ nsCOMPtr transferableArray; NS_NewISupportsArray(getter_AddRefs(transferableArray)); - if (transferableArray == nsnull) + if (!transferableArray) return NS_ERROR_OUT_OF_MEMORY; /* add the transferable to the array */ @@ -424,7 +406,7 @@ NS_IMETHODIMP nsPlaintextEditor::Paste(PRInt32 aSelectionType) nsCOMPtr clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv)); if ( NS_FAILED(rv) ) return rv; - + // Get the nsITransferable interface for getting the data from the clipboard nsCOMPtr trans; rv = PrepareTransferable(getter_AddRefs(trans)); @@ -464,11 +446,8 @@ NS_IMETHODIMP nsPlaintextEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPa // the flavors that we can deal with const char* const textEditorFlavors[] = { kUnicodeMime, nsnull }; - nsCOMPtr flavorsList; - rv = nsComponentManager::CreateInstance(NS_SUPPORTSARRAY_CONTRACTID, nsnull, - NS_GET_IID(nsISupportsArray), getter_AddRefs(flavorsList)); - if (NS_FAILED(rv)) return rv; - + nsCOMPtr flavorsList = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID); + PRUint32 editorFlags; GetFlags(&editorFlags); diff --git a/mozilla/editor/libeditor/text/nsPlaintextEditor.cpp b/mozilla/editor/libeditor/text/nsPlaintextEditor.cpp index 35d344301ae..32ccaee8fdb 100644 --- a/mozilla/editor/libeditor/text/nsPlaintextEditor.cpp +++ b/mozilla/editor/libeditor/text/nsPlaintextEditor.cpp @@ -44,53 +44,34 @@ #include "nsTextEditRules.h" #include "nsEditorEventListeners.h" #include "nsIEditActionListener.h" -#include "nsIDOMText.h" #include "nsIDOMNodeList.h" #include "nsIDOMDocument.h" -#include "nsIDOMAttr.h" #include "nsIDocument.h" #include "nsIDOMEventReceiver.h" #include "nsIDOM3EventTarget.h" #include "nsIDOMKeyEvent.h" -#include "nsIDOMKeyListener.h" #include "nsIDOMMouseListener.h" -#include "nsIDOMMouseEvent.h" #include "nsISelection.h" #include "nsISelectionPrivate.h" -#include "nsIDOMHTMLAnchorElement.h" -#include "nsIDOMHTMLImageElement.h" #include "nsISelectionController.h" #include "nsGUIEvent.h" #include "nsIDOMEventGroup.h" #include "nsCRT.h" -#include "nsIDocumentObserver.h" -#include "nsIDocumentStateListener.h" - #include "nsIEnumerator.h" #include "nsIContent.h" #include "nsIContentIterator.h" #include "nsIDOMRange.h" -#include "nsIDOMNSRange.h" #include "nsISupportsArray.h" -#include "nsVoidArray.h" -#include "nsIURL.h" #include "nsIComponentManager.h" #include "nsIServiceManager.h" #include "nsIDocumentEncoder.h" -#include "nsIDOMDocumentFragment.h" #include "nsIPresShell.h" -#include "nsIPresContext.h" -#include "nsIImage.h" -#include "nsXPCOM.h" #include "nsISupportsPrimitives.h" - -// netwerk -#include "nsIURI.h" -#include "nsNetUtil.h" +#include "nsReadableUtils.h" // Misc -#include "nsEditorUtils.h" +#include "nsEditorUtils.h" // nsAutoEditBatch, nsAutoRules #include "nsIPrefBranch.h" #include "nsIPrefService.h" #include "nsUnicharUtils.h" @@ -102,8 +83,6 @@ #include "nsIClipboard.h" #include "nsITransferable.h" -const PRUnichar nbsp = 160; - // prototype for rules creation shortcut nsresult NS_NewTextEditRules(nsIEditRules** aInstancePtrResult); @@ -220,7 +199,6 @@ nsPlaintextEditor::SetDocumentCharacterSet(const nsACString & characterSet) result = GetDocument(getter_AddRefs(domdoc)); if (NS_SUCCEEDED(result) && domdoc) { nsCOMPtrmetaList; - nsCOMPtrmetaNode; nsCOMPtrmetaElement; PRBool newMetaCharset = PR_TRUE; @@ -230,6 +208,7 @@ nsPlaintextEditor::SetDocumentCharacterSet(const nsACString & characterSet) PRUint32 listLength = 0; (void) metaList->GetLength(&listLength); + nsCOMPtrmetaNode; for (PRUint32 i = 0; i < listLength; i++) { metaList->Item(i, getter_AddRefs(metaNode)); if (!metaNode) continue; @@ -305,8 +284,7 @@ nsPlaintextEditor::PostCreate() nsresult result = InstallEventListeners(); if (NS_FAILED(result)) return result; - result = nsEditor::PostCreate(); - return result; + return nsEditor::PostCreate(); } NS_IMETHODIMP @@ -434,13 +412,10 @@ nsPlaintextEditor::SetFlags(PRUint32 aFlags) NS_IMETHODIMP nsPlaintextEditor::InitRules() { // instantiate the rules for this text editor - nsresult res = NS_ERROR_FAILURE; - res = NS_NewTextEditRules(getter_AddRefs(mRules)); + nsresult res = NS_NewTextEditRules(getter_AddRefs(mRules)); if (NS_FAILED(res)) return res; if (!mRules) return NS_ERROR_UNEXPECTED; - res = mRules->Init(this, mFlags); - - return res; + return mRules->Init(this, mFlags); } @@ -461,8 +436,8 @@ PRBool nsPlaintextEditor::IsModifiable() PRUint32 flags; if (NS_SUCCEEDED(GetFlags(&flags))) return ((flags & eEditorReadonlyMask) == 0); - else - return PR_FALSE; + + return PR_FALSE; } @@ -586,14 +561,15 @@ NS_IMETHODIMP nsPlaintextEditor::CreateBRImpl(nsCOMPtr *aInOutParent *outBRNode = brNode; if (*outBRNode && (aSelect != eNone)) { - nsCOMPtr selection; nsCOMPtr parent; PRInt32 offset; + res = GetNodeLocation(*outBRNode, address_of(parent), &offset); + if (NS_FAILED(res)) return res; + + nsCOMPtr selection; res = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(res)) return res; nsCOMPtr selPriv(do_QueryInterface(selection)); - res = GetNodeLocation(*outBRNode, address_of(parent), &offset); - if (NS_FAILED(res)) return res; if (aSelect == eNext) { // position selection after br @@ -620,18 +596,16 @@ NS_IMETHODIMP nsPlaintextEditor::CreateBR(nsIDOMNode *aNode, PRInt32 aOffset, ns NS_IMETHODIMP nsPlaintextEditor::InsertBR(nsCOMPtr *outBRNode) { - PRBool bCollapsed; - nsCOMPtr selection; - if (!outBRNode) return NS_ERROR_NULL_POINTER; *outBRNode = nsnull; // calling it text insertion to trigger moz br treatment by rules nsAutoRules beginRulesSniffing(this, kOpInsertText, nsIEditor::eNext); + nsCOMPtr selection; nsresult res = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(res)) return res; - nsCOMPtr selPriv(do_QueryInterface(selection)); + PRBool bCollapsed; res = selection->GetIsCollapsed(&bCollapsed); if (NS_FAILED(res)) return res; if (!bCollapsed) @@ -650,17 +624,16 @@ NS_IMETHODIMP nsPlaintextEditor::InsertBR(nsCOMPtr *outBRNode) // position selection after br res = GetNodeLocation(*outBRNode, address_of(selNode), &selOffset); if (NS_FAILED(res)) return res; + nsCOMPtr selPriv(do_QueryInterface(selection)); selPriv->SetInterlinePosition(PR_TRUE); - res = selection->Collapse(selNode, selOffset+1); - - return res; + return selection->Collapse(selNode, selOffset+1); } + nsresult nsPlaintextEditor::GetTextSelectionOffsets(nsISelection *aSelection, PRInt32 &aOutStartOffset, PRInt32 &aOutEndOffset) { if(!aSelection) { return NS_ERROR_NULL_POINTER; } - nsresult result; // initialize out params aOutStartOffset = 0; // default to first char in selection aOutEndOffset = -1; // default to total length of text in selection @@ -673,9 +646,8 @@ nsresult nsPlaintextEditor::GetTextSelectionOffsets(nsISelection *aSelection, aSelection->GetFocusOffset(&endOffset); nsCOMPtr enumerator; - nsCOMPtr selection(aSelection); - nsCOMPtr selPriv(do_QueryInterface(selection)); - result = selPriv->GetEnumerator(getter_AddRefs(enumerator)); + nsCOMPtr selPriv(do_QueryInterface(aSelection)); + nsresult result = selPriv->GetEnumerator(getter_AddRefs(enumerator)); if (NS_FAILED(result)) return result; if (!enumerator) return NS_ERROR_NULL_POINTER; @@ -712,11 +684,11 @@ nsPlaintextEditor::GetAbsoluteOffsetsForPoints(nsIDOMNode *aInStartNode, if(!aInStartNode || !aInEndNode || !aInCommonParentNode) return NS_ERROR_NULL_POINTER; - nsresult result; // initialize out params aOutStartOffset = 0; // default to first char in selection aOutEndOffset = -1; // default to total length of text in selection + nsresult result; nsCOMPtr iter = do_CreateInstance( "@mozilla.org/content/post-content-iterator;1", &result); if (NS_FAILED(result)) return result; @@ -762,8 +734,7 @@ nsPlaintextEditor::GetAbsoluteOffsetsForPoints(nsIDOMNode *aInStartNode, // guarantee that aOutStartOffset <= aOutEndOffset if (aOutEndOffset rootElement; - nsresult result = GetRootElement(getter_AddRefs(rootElement)); - if (NS_FAILED(result)) return result; @@ -793,7 +762,6 @@ nsPlaintextEditor::GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver) // If we are grab the parent of root element for our observer. nsCOMPtr content = do_QueryInterface(rootElement); - if (content) { nsCOMPtr parent; @@ -819,7 +787,6 @@ nsPlaintextEditor::GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver) // if it exists. nsCOMPtr domdoc = do_QueryReferent(mDocWeak); - if (!domdoc) return NS_ERROR_FAILURE; @@ -828,27 +795,11 @@ nsPlaintextEditor::GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver) return result; } - -NS_IMETHODIMP -nsPlaintextEditor::CollapseSelectionToStart() -{ - nsCOMPtr bodyElement; - nsresult res = nsEditor::GetRootElement(getter_AddRefs(bodyElement)); - if (NS_FAILED(res)) return res; - if (!bodyElement) return NS_ERROR_NULL_POINTER; - nsCOMPtr bodyNode = do_QueryInterface(bodyElement); - nsCOMPtr selection; - res = GetSelection(getter_AddRefs(selection)); - if (NS_FAILED(res)) return res; - return selection->Collapse(bodyNode,0); -} NS_IMETHODIMP nsPlaintextEditor::DeleteSelection(nsIEditor::EDirection aAction) { if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } - nsCOMPtr selection; - PRBool cancel, handled; nsresult result; // delete placeholder txns merge. @@ -862,7 +813,6 @@ NS_IMETHODIMP nsPlaintextEditor::DeleteSelection(nsIEditor::EDirection aAction) if (aAction == eNextWord || aAction == ePreviousWord || aAction == eToBeginningOfLine || aAction == eToEndOfLine) { - if (!mSelConWeak) return NS_ERROR_NOT_INITIALIZED; nsCOMPtr selCont (do_QueryReferent(mSelConWeak)); if (!selCont) return NS_ERROR_NO_INTERFACE; @@ -902,12 +852,14 @@ NS_IMETHODIMP nsPlaintextEditor::DeleteSelection(nsIEditor::EDirection aAction) } // pre-process + nsCOMPtr selection; result = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(result)) return result; if (!selection) return NS_ERROR_NULL_POINTER; nsTextRulesInfo ruleInfo(nsTextEditRules::kDeleteSelection); ruleInfo.collapsedAction = aAction; + PRBool cancel, handled; result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); if (NS_FAILED(result)) return result; if (!cancel && !handled) @@ -927,8 +879,6 @@ NS_IMETHODIMP nsPlaintextEditor::InsertText(const nsAString &aStringToInsert) { if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } - nsCOMPtr selection; - PRBool cancel, handled; PRInt32 theAction = nsTextEditRules::kInsertText; PRInt32 opID = kOpInsertText; if (mInIMEMode) @@ -940,6 +890,7 @@ NS_IMETHODIMP nsPlaintextEditor::InsertText(const nsAString &aStringToInsert) nsAutoRules beginRulesSniffing(this, opID, nsIEditor::eNext); // pre-process + nsCOMPtr selection; nsresult result = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(result)) return result; if (!selection) return NS_ERROR_NULL_POINTER; @@ -952,6 +903,7 @@ NS_IMETHODIMP nsPlaintextEditor::InsertText(const nsAString &aStringToInsert) ruleInfo.outString = &resultString; ruleInfo.maxLength = mMaxTextLength; + PRBool cancel, handled; result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); if (NS_FAILED(result)) return result; if (!cancel && !handled) @@ -966,38 +918,29 @@ NS_IMETHODIMP nsPlaintextEditor::InsertText(const nsAString &aStringToInsert) return result; } -NS_IMETHODIMP nsPlaintextEditor::GetCanModify(PRBool *aCanModify) -{ - NS_ENSURE_ARG_POINTER(aCanModify); - - *aCanModify = IsModifiable(); - return NS_OK; -} - - NS_IMETHODIMP nsPlaintextEditor::InsertLineBreak() { - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } nsAutoEditBatch beginBatching(this); nsAutoRules beginRulesSniffing(this, kOpInsertBreak, nsIEditor::eNext); - nsCOMPtr selection; - PRBool cancel, handled; // pre-process + nsCOMPtr selection; + nsresult res; res = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(res)) return res; if (!selection) return NS_ERROR_NULL_POINTER; nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertBreak); + PRBool cancel, handled; res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); if (NS_FAILED(res)) return res; if (!cancel && !handled) { // create the new BR node nsCOMPtr newNode; - res = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("BR"), getter_AddRefs(newNode)); + res = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("br"), getter_AddRefs(newNode)); if (!newNode) res = NS_ERROR_NULL_POINTER; // don't return here, so DidDoAction is called if (NS_SUCCEEDED(res)) { @@ -1012,41 +955,36 @@ NS_IMETHODIMP nsPlaintextEditor::InsertLineBreak() newNode->GetNextSibling(getter_AddRefs(nextNode)); if (nextNode) { - nsCOMPtrnextTextNode; - nextTextNode = do_QueryInterface(nextNode); + nsCOMPtrnextTextNode = do_QueryInterface(nextNode); if (!nextTextNode) { - nextNode = do_QueryInterface(newNode); + nextNode = do_QueryInterface(newNode); // is this QI needed? } else { offsetInParent=0; } } else { - nextNode = do_QueryInterface(newNode); + nextNode = do_QueryInterface(newNode); // is this QI needed? } - res = GetSelection(getter_AddRefs(selection)); - if (!selection) res = NS_ERROR_NULL_POINTER; // don't return here, so DidDoAction is called - if (NS_SUCCEEDED(res)) + + if (-1==offsetInParent) { - nsCOMPtr selPriv(do_QueryInterface(selection)); - if (-1==offsetInParent) - { - nextNode->GetParentNode(getter_AddRefs(parent)); - res = GetChildOffset(nextNode, parent, offsetInParent); - if (NS_SUCCEEDED(res)) { - // SetInterlinePosition(PR_TRUE) means we want the caret to stick to the content on the "right". - // We want the caret to stick to whatever is past the break. This is - // because the break is on the same line we were on, but the next content - // will be on the following line. - selPriv->SetInterlinePosition(PR_TRUE); - res = selection->Collapse(parent, offsetInParent+1); // +1 to insert just after the break - } - } - else - { - res = selection->Collapse(nextNode, offsetInParent); + nextNode->GetParentNode(getter_AddRefs(parent)); + res = GetChildOffset(nextNode, parent, offsetInParent); + if (NS_SUCCEEDED(res)) { + // SetInterlinePosition(PR_TRUE) means we want the caret to stick to the content on the "right". + // We want the caret to stick to whatever is past the break. This is + // because the break is on the same line we were on, but the next content + // will be on the following line. + nsCOMPtr selPriv(do_QueryInterface(selection)); + selPriv->SetInterlinePosition(PR_TRUE); + res = selection->Collapse(parent, offsetInParent+1); // +1 to insert just after the break } } + else + { + res = selection->Collapse(nextNode, offsetInParent); + } } } } @@ -1092,42 +1030,36 @@ NS_IMETHODIMP nsPlaintextEditor::GetTextLength(PRInt32 *aCount) { if (!aCount) { return NS_ERROR_NULL_POINTER; } - nsresult result; // initialize out params *aCount = 0; // special-case for empty document, to account for the bogus text node PRBool docEmpty; - result = GetDocumentIsEmpty(&docEmpty); + nsresult result = GetDocumentIsEmpty(&docEmpty); if (NS_FAILED(result)) return result; if (docEmpty) - { - *aCount = 0; return NS_OK; - } // get the body node nsCOMPtr bodyElement; - result = nsEditor::GetRootElement(getter_AddRefs(bodyElement)); + result = GetRootElement(getter_AddRefs(bodyElement)); if (NS_FAILED(result)) { return result; } if (!bodyElement) { return NS_ERROR_NULL_POINTER; } // get the offsets of the first and last children of the body node - nsCOMPtrbodyNode = do_QueryInterface(bodyElement); - if (!bodyNode) { return NS_ERROR_NULL_POINTER; } - PRInt32 numBodyChildren=0; nsCOMPtrlastChild; - result = bodyNode->GetLastChild(getter_AddRefs(lastChild)); + result = bodyElement->GetLastChild(getter_AddRefs(lastChild)); if (NS_FAILED(result)) { return result; } if (!lastChild) { return NS_ERROR_NULL_POINTER; } - result = GetChildOffset(lastChild, bodyNode, numBodyChildren); + PRInt32 numBodyChildren = 0; + result = GetChildOffset(lastChild, bodyElement, numBodyChildren); if (NS_FAILED(result)) { return result; } // count PRInt32 start, end; - result = GetAbsoluteOffsetsForPoints(bodyNode, 0, - bodyNode, numBodyChildren, - bodyNode, start, end); + result = GetAbsoluteOffsetsForPoints(bodyElement, 0, + bodyElement, numBodyChildren, + bodyElement, start, end); if (NS_SUCCEEDED(result)) { NS_ASSERTION(0==start, "GetAbsoluteOffsetsForPoints failed to set start correctly."); @@ -1192,8 +1124,6 @@ static void CutStyle(const char* stylename, nsString& styleValue) NS_IMETHODIMP nsPlaintextEditor::SetWrapWidth(PRInt32 aWrapColumn) { - nsresult res; - mWrapColumn = aWrapColumn; // Make sure we're a plaintext editor, otherwise we shouldn't @@ -1206,7 +1136,7 @@ nsPlaintextEditor::SetWrapWidth(PRInt32 aWrapColumn) // Ought to set a style sheet here ... // Probably should keep around an mPlaintextStyleSheet for this purpose. nsCOMPtr bodyElement; - res = GetRootElement(getter_AddRefs(bodyElement)); + nsresult res = GetRootElement(getter_AddRefs(bodyElement)); if (NS_FAILED(res)) return res; if (!bodyElement) return NS_ERROR_NULL_POINTER; @@ -1261,8 +1191,7 @@ nsPlaintextEditor::SetWrapWidth(PRInt32 aWrapColumn) else styleValue.Append(NS_LITERAL_STRING("white-space: pre;")); - res = bodyElement->SetAttribute(styleName, styleValue); - return res; + return bodyElement->SetAttribute(styleName, styleValue); } @@ -1279,7 +1208,6 @@ nsPlaintextEditor::Undo(PRUint32 aCount) nsAutoUpdateViewBatch beginViewBatching(this); ForceCompositionEnd(); - nsresult result = NS_OK; nsAutoRules beginRulesSniffing(this, kOpUndo, nsIEditor::eNone); @@ -1287,7 +1215,7 @@ nsPlaintextEditor::Undo(PRUint32 aCount) nsCOMPtr selection; GetSelection(getter_AddRefs(selection)); PRBool cancel, handled; - result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); + nsresult result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); if (!cancel && NS_SUCCEEDED(result)) { @@ -1298,20 +1226,20 @@ nsPlaintextEditor::Undo(PRUint32 aCount) return result; } - NS_IMETHODIMP nsPlaintextEditor::Redo(PRUint32 aCount) { - nsresult result = NS_OK; - nsAutoUpdateViewBatch beginViewBatching(this); + + ForceCompositionEnd(); + nsAutoRules beginRulesSniffing(this, kOpRedo, nsIEditor::eNone); nsTextRulesInfo ruleInfo(nsTextEditRules::kRedo); nsCOMPtr selection; GetSelection(getter_AddRefs(selection)); PRBool cancel, handled; - result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); + nsresult result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); if (!cancel && NS_SUCCEEDED(result)) { @@ -1341,19 +1269,10 @@ NS_IMETHODIMP nsPlaintextEditor::Cut() NS_IMETHODIMP nsPlaintextEditor::CanCut(PRBool *aCanCut) { - if (!aCanCut) - return NS_ERROR_NULL_POINTER; - *aCanCut = PR_FALSE; - - nsCOMPtr selection; - nsresult res = GetSelection(getter_AddRefs(selection)); + nsresult res = CanCopy(aCanCut); if (NS_FAILED(res)) return res; - PRBool isCollapsed; - res = selection->GetIsCollapsed(&isCollapsed); - if (NS_FAILED(res)) return res; - - *aCanCut = !isCollapsed && IsModifiable(); + *aCanCut = *aCanCut && IsModifiable(); return NS_OK; } @@ -1478,13 +1397,13 @@ nsPlaintextEditor::OutputToString(const nsAString& aFormatType, PRUint32 aFlags, nsAString& aOutputString) { - PRBool cancel, handled; nsString resultString; nsTextRulesInfo ruleInfo(nsTextEditRules::kOutputText); ruleInfo.outString = &resultString; // XXX Struct should store a nsAReadable* nsAutoString str(aFormatType); ruleInfo.outputFormat = &str; + PRBool cancel, handled; nsresult rv = mRules->WillDoAction(nsnull, &ruleInfo, &cancel, &handled); if (cancel || NS_FAILED(rv)) { return rv; } if (handled) @@ -1500,11 +1419,9 @@ nsPlaintextEditor::OutputToString(const nsAString& aFormatType, nsCOMPtr encoder; rv = GetAndInitDocEncoder(aFormatType, aFlags, charsetStr, getter_AddRefs(encoder)); - if (NS_FAILED(rv)) return rv; - rv = encoder->EncodeToString(aOutputString); - return rv; + return encoder->EncodeToString(aOutputString); } NS_IMETHODIMP @@ -1573,11 +1490,11 @@ nsPlaintextEditor::PasteAsQuotation(PRInt32 aSelectionType) // it still owns the data, we just have a pointer to it. // If it can't support a "text" output of the data the call will fail nsCOMPtr genericDataObj; - PRUint32 len = 0; - char* flav = 0; + PRUint32 len; + char* flav = nsnull; rv = trans->GetAnyTransferData(&flav, getter_AddRefs(genericDataObj), &len); - if (NS_FAILED(rv)) + if (NS_FAILED(rv) || !flav) { #ifdef DEBUG_akkana printf("PasteAsPlaintextQuotation: GetAnyTransferData failed, %d\n", rv); @@ -1587,20 +1504,15 @@ nsPlaintextEditor::PasteAsQuotation(PRInt32 aSelectionType) #ifdef DEBUG_clipboard printf("Got flavor [%s]\n", flav); #endif - nsAutoString flavor; flavor.AssignWithConversion(flav); - nsAutoString stuffToPaste; - if (flavor.Equals(NS_LITERAL_STRING(kUnicodeMime))) + if (0 == nsCRT::strcmp(flav, kUnicodeMime)) { nsCOMPtr textDataObj ( do_QueryInterface(genericDataObj) ); if (textDataObj && len > 0) { - PRUnichar* text = nsnull; - textDataObj->ToString ( &text ); - stuffToPaste.Assign ( text, len / 2 ); + nsAutoString stuffToPaste; + textDataObj->GetData ( stuffToPaste ); nsAutoEditBatch beginBatching(this); rv = InsertAsQuotation(stuffToPaste, 0); - if (text) - nsMemory::Free(text); } } nsCRT::free(flav); @@ -1622,17 +1534,14 @@ static nsICiter* MakeACiter() char *citationType = 0; rv = prefBranch->GetCharPref("mail.compose.citationType", &citationType); - if (NS_SUCCEEDED(rv) && citationType[0]) - { - if (!strncmp(citationType, "aol", 3)) - citer = new nsAOLCiter; - else - citer = new nsInternetCiter; - PL_strfree(citationType); - } + if (NS_SUCCEEDED(rv) && citationType[0] && !strncmp(citationType, "aol", 3)) + citer = new nsAOLCiter; else citer = new nsInternetCiter; + if (citationType) + PL_strfree(citationType); + if (citer) NS_ADDREF(citer); return citer; @@ -1701,6 +1610,24 @@ nsPlaintextEditor::InsertAsCitedQuotation(const nsAString& aQuotedText, return InsertAsQuotation(aQuotedText, aNodeInserted); } +nsresult +nsPlaintextEditor::SharedOutputString(PRUint32 aFlags, PRBool* aIsCollapsed, nsAString& aResult) +{ + nsCOMPtr selection; + nsresult rv = GetSelection(getter_AddRefs(selection)); + if (NS_FAILED(rv)) return rv; + if (!selection) + return NS_ERROR_NOT_INITIALIZED; + + rv = selection->GetIsCollapsed(aIsCollapsed); + if (NS_FAILED(rv)) return rv; + + if (*aIsCollapsed) // rewrap the whole document + aFlags |= nsIDocumentEncoder::OutputSelectionOnly; + + return OutputToString(NS_LITERAL_STRING("text/plain"), aFlags, aResult); +} + NS_IMETHODIMP nsPlaintextEditor::Rewrap(PRBool aRespectNewlines) { @@ -1717,62 +1644,27 @@ nsPlaintextEditor::Rewrap(PRBool aRespectNewlines) printf("nsPlaintextEditor::Rewrap to %ld columns\n", (long)wrapCol); #endif - nsCOMPtr selection; - rv = GetSelection(getter_AddRefs(selection)); - if (NS_FAILED(rv)) return rv; - - if (!selection) - return NS_ERROR_NOT_INITIALIZED; - PRBool isCollapsed; - rv = selection->GetIsCollapsed(&isCollapsed); - if (NS_FAILED(rv)) return rv; - - // Variables we'll need either way - NS_NAMED_LITERAL_STRING(format, "text/plain"); nsAutoString current; + PRBool isCollapsed; + rv = SharedOutputString(nsIDocumentEncoder::OutputFormatted + | nsIDocumentEncoder::OutputLFLineBreak, + &isCollapsed, current); + if (NS_FAILED(rv)) return rv; + + nsCOMPtr citer = dont_AddRef(MakeACiter()); + if (NS_FAILED(rv)) return rv; + if (!citer) return NS_ERROR_UNEXPECTED; + nsString wrapped; + PRUint32 firstLineOffset = 0; // XXX need to reset this if there is a selection + rv = citer->Rewrap(current, wrapCol, firstLineOffset, aRespectNewlines, + wrapped); + if (NS_FAILED(rv)) return rv; if (isCollapsed) // rewrap the whole document - { - rv = OutputToString(format, - nsIDocumentEncoder::OutputFormatted - | nsIDocumentEncoder::OutputLFLineBreak, - current); - if (NS_FAILED(rv)) return rv; + SelectAll(); - nsCOMPtr citer = dont_AddRef(MakeACiter()); - if (NS_FAILED(rv)) return rv; - if (!citer) return NS_ERROR_UNEXPECTED; - - rv = citer->Rewrap(current, wrapCol, 0, aRespectNewlines, wrapped); - if (NS_FAILED(rv)) return rv; - - rv = SelectAll(); - if (NS_FAILED(rv)) return rv; - - return InsertTextWithQuotations(wrapped); - } - else // rewrap only the selection - { - rv = OutputToString(format, - nsIDocumentEncoder::OutputFormatted - | nsIDocumentEncoder::OutputLFLineBreak - | nsIDocumentEncoder::OutputSelectionOnly, - current); - if (NS_FAILED(rv)) return rv; - - nsCOMPtr citer = dont_AddRef(MakeACiter()); - if (NS_FAILED(rv)) return rv; - if (!citer) return NS_ERROR_UNEXPECTED; - - PRUint32 firstLineOffset = 0; // XXX need to get this - rv = citer->Rewrap(current, wrapCol, firstLineOffset, aRespectNewlines, - wrapped); - if (NS_FAILED(rv)) return rv; - - return InsertTextWithQuotations(wrapped); - } - return NS_OK; + return InsertTextWithQuotations(wrapped); } NS_IMETHODIMP @@ -1782,56 +1674,27 @@ nsPlaintextEditor::StripCites() printf("nsPlaintextEditor::StripCites()\n"); #endif - nsCOMPtr selection; - nsresult rv = GetSelection(getter_AddRefs(selection)); - if (NS_FAILED(rv)) return rv; - - if (!selection) - return NS_ERROR_NOT_INITIALIZED; - PRBool isCollapsed; - rv = selection->GetIsCollapsed(&isCollapsed); - if (NS_FAILED(rv)) return rv; - - // Variables we'll need either way - NS_NAMED_LITERAL_STRING(format, "text/plain"); nsAutoString current; + PRBool isCollapsed; + nsresult rv = SharedOutputString(nsIDocumentEncoder::OutputFormatted, + &isCollapsed, current); + if (NS_FAILED(rv)) return rv; + + nsCOMPtr citer = dont_AddRef(MakeACiter()); + if (NS_FAILED(rv)) return rv; + if (!citer) return NS_ERROR_UNEXPECTED; + nsString stripped; + rv = citer->StripCites(current, stripped); + if (NS_FAILED(rv)) return rv; if (isCollapsed) // rewrap the whole document { - rv = OutputToString(format, nsIDocumentEncoder::OutputFormatted, current); - if (NS_FAILED(rv)) return rv; - - nsCOMPtr citer = dont_AddRef(MakeACiter()); - if (NS_FAILED(rv)) return rv; - if (!citer) return NS_ERROR_UNEXPECTED; - - rv = citer->StripCites(current, stripped); - if (NS_FAILED(rv)) return rv; - rv = SelectAll(); if (NS_FAILED(rv)) return rv; - - return InsertText(stripped); } - else // rewrap only the selection - { - rv = OutputToString(format, - nsIDocumentEncoder::OutputFormatted - | nsIDocumentEncoder::OutputSelectionOnly, - current); - if (NS_FAILED(rv)) return rv; - nsCOMPtr citer = dont_AddRef(MakeACiter()); - if (NS_FAILED(rv)) return rv; - if (!citer) return NS_ERROR_UNEXPECTED; - - rv = citer->StripCites(current, stripped); - if (NS_FAILED(rv)) return rv; - - return InsertText(stripped); - } - return NS_OK; + return InsertText(stripped); } NS_IMETHODIMP @@ -1852,29 +1715,20 @@ NS_IMETHODIMP nsPlaintextEditor::SetCompositionString(const nsAString& aCompositionString, nsIPrivateTextRangeList* aTextRangeList,nsTextEventReply* aReply) { NS_ASSERTION(aTextRangeList, "null ptr"); - if (nsnull == aTextRangeList) + if (!aTextRangeList) return NS_ERROR_NULL_POINTER; - nsCOMPtr caretP; - // workaround for windows ime bug 23558: we get every ime event twice. // for escape keypress, this causes an empty string to be passed - // twice, which freaks out the editor. This is to detect and aviod that + // twice, which freaks out the editor. This is to detect and avoid that // situation: if (aCompositionString.IsEmpty() && !mIMETextNode) { return NS_OK; } - nsCOMPtr selection; - nsresult result = GetSelection(getter_AddRefs(selection)); - if (NS_FAILED(result)) return result; - mIMETextRangeList = aTextRangeList; - if (!mPresShellWeak) - return NS_ERROR_NOT_INITIALIZED; - nsCOMPtr ps = do_QueryReferent(mPresShellWeak); if (!ps) return NS_ERROR_NOT_INITIALIZED; @@ -1902,6 +1756,12 @@ nsPlaintextEditor::SetCompositionString(const nsAString& aCompositionString, nsI // XXX_kin: END HACK! HACK! HACK! + nsCOMPtr selection; + nsresult result = GetSelection(getter_AddRefs(selection)); + if (NS_FAILED(result)) return result; + + nsCOMPtr caretP; + // we need the nsAutoPlaceHolderBatch destructor called before hitting // GetCaretCoordinates so the states in Frame system sync with content // therefore, we put the nsAutoPlaceHolderBatch into a inner block @@ -1934,9 +1794,12 @@ nsPlaintextEditor::SetCompositionString(const nsAString& aCompositionString, nsI // XXX_kin: END HACK! HACK! HACK! - result = caretP->GetCaretCoordinates(nsICaret::eIMECoordinates, selection, - &(aReply->mCursorPosition), &(aReply->mCursorIsCollapsed), nsnull); - NS_ASSERTION(NS_SUCCEEDED(result), "cannot get caret position"); + if (caretP) + { + result = caretP->GetCaretCoordinates(nsICaret::eIMECoordinates, selection, + &(aReply->mCursorPosition), &(aReply->mCursorIsCollapsed), nsnull); + NS_ASSERTION(NS_SUCCEEDED(result), "cannot get caret position"); + } return result; } @@ -1944,12 +1807,10 @@ nsPlaintextEditor::SetCompositionString(const nsAString& aCompositionString, nsI NS_IMETHODIMP nsPlaintextEditor::GetReconversionString(nsReconversionEventReply* aReply) { - nsresult res; - nsCOMPtr selection; - res = GetSelection(getter_AddRefs(selection)); - if (NS_FAILED(res) || !selection) - return (res == NS_OK) ? NS_ERROR_FAILURE : res; + nsresult res = GetSelection(getter_AddRefs(selection)); + if (NS_FAILED(res)) return res; + if (!selection) return NS_ERROR_FAILURE; // get the first range in the selection. Since it is // unclear what to do if reconversion happens with a @@ -1957,8 +1818,8 @@ nsPlaintextEditor::GetReconversionString(nsReconversionEventReply* aReply) nsCOMPtr range; res = selection->GetRangeAt(0, getter_AddRefs(range)); - if (NS_FAILED(res) || !range) - return (res == NS_OK) ? NS_ERROR_FAILURE : res; + if (NS_FAILED(res)) return res; + if (!range) return NS_ERROR_FAILURE; nsAutoString textValue; res = range->ToString(textValue); @@ -1971,9 +1832,7 @@ nsPlaintextEditor::GetReconversionString(nsReconversionEventReply* aReply) return NS_ERROR_OUT_OF_MEMORY; // delete the selection - res = DeleteSelection(eNone); - - return res; + return DeleteSelection(eNone); } #ifdef XP_MAC @@ -2010,31 +1869,23 @@ nsPlaintextEditor::EndOperation() NS_IMETHODIMP nsPlaintextEditor::SelectEntireDocument(nsISelection *aSelection) { - nsresult res; if (!aSelection || !mRules) { return NS_ERROR_NULL_POINTER; } - // get body node - nsCOMPtrbodyElement; - res = GetRootElement(getter_AddRefs(bodyElement)); - if (NS_FAILED(res)) return res; - nsCOMPtrbodyNode = do_QueryInterface(bodyElement); - if (!bodyNode) return NS_ERROR_FAILURE; - // is doc empty? PRBool bDocIsEmpty; - res = mRules->DocumentIsEmpty(&bDocIsEmpty); - if (NS_FAILED(res)) return res; - - if (bDocIsEmpty) + if (NS_SUCCEEDED(mRules->DocumentIsEmpty(&bDocIsEmpty)) && bDocIsEmpty) { - // if its empty dont select entire doc - that would select the bogus node - return aSelection->Collapse(bodyNode, 0); + // get body node + nsCOMPtrbodyElement; + nsresult res = GetRootElement(getter_AddRefs(bodyElement)); + if (NS_FAILED(res)) return res; + if (!bodyElement) return NS_ERROR_FAILURE; + + // if it's empty don't select entire doc - that would select the bogus node + return aSelection->Collapse(bodyElement, 0); } - else - { - return nsEditor::SelectEntireDocument(aSelection); - } - return res; + + return nsEditor::SelectEntireDocument(aSelection); } @@ -2048,12 +1899,11 @@ nsPlaintextEditor::SelectEntireDocument(nsISelection *aSelection) NS_IMETHODIMP nsPlaintextEditor::GetLayoutObject(nsIDOMNode *aNode, nsISupports **aLayoutObject) { - nsresult result = NS_ERROR_FAILURE; // we return an error unless we get the index - if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; nsCOMPtr ps = do_QueryReferent(mPresShellWeak); if (!ps) return NS_ERROR_NOT_INITIALIZED; - if ((nsnull!=aNode)) + nsresult result; + if (aNode) { // get the content interface nsCOMPtr nodeAsContent( do_QueryInterface(aNode) ); if (nodeAsContent) @@ -2070,22 +1920,6 @@ NS_IMETHODIMP nsPlaintextEditor::GetLayoutObject(nsIDOMNode *aNode, nsISupports return result; } - -#ifdef XP_MAC -#pragma mark - -#endif - -NS_IMETHODIMP -nsPlaintextEditor::IsRootTag(nsString &aTag, PRBool &aIsTag) -{ - aIsTag = (aTag.EqualsIgnoreCase("body") || - aTag.EqualsIgnoreCase("td") || - aTag.EqualsIgnoreCase("th") || - aTag.EqualsIgnoreCase("caption") ); - return NS_OK; -} - - #ifdef XP_MAC #pragma mark - #endif diff --git a/mozilla/editor/libeditor/text/nsPlaintextEditor.h b/mozilla/editor/libeditor/text/nsPlaintextEditor.h index 40821d70c40..5b8a2115cb1 100644 --- a/mozilla/editor/libeditor/text/nsPlaintextEditor.h +++ b/mozilla/editor/libeditor/text/nsPlaintextEditor.h @@ -51,7 +51,6 @@ #include "nsEditRules.h" -class nsIDOMKeyEvent; class nsITransferable; class nsIDOMEventReceiver; class nsIDocumentEncoder; @@ -86,19 +85,7 @@ public: NS_DECL_NSIPLAINTEXTEDITOR /* ------------ nsIEditorMailSupport overrides -------------- */ - NS_IMETHOD InsertTextWithQuotations(const nsAString &aStringToInsert); - NS_IMETHOD PasteAsQuotation(PRInt32 aSelectionType); - NS_IMETHOD InsertAsQuotation(const nsAString& aQuotedText, - nsIDOMNode** aNodeInserted); - NS_IMETHOD PasteAsCitedQuotation(const nsAString& aCitation, - PRInt32 aSelectionType); - NS_IMETHOD InsertAsCitedQuotation(const nsAString& aQuotedText, - const nsAString& aCitation, - PRBool aInsertHTML, - nsIDOMNode** aNodeInserted); - NS_IMETHOD Rewrap(PRBool aRespectNewlines); - NS_IMETHOD StripCites(); - NS_IMETHOD GetEmbeddedObjects(nsISupportsArray** aNodeList); + NS_DECL_NSIEDITORMAILSUPPORT /* ------------ nsIEditorIMESupport overrides -------------- */ @@ -224,9 +211,6 @@ protected: EDirection aSelect); NS_IMETHOD InsertBR(nsCOMPtr *outBRNode); - NS_IMETHOD IsRootTag(nsString &aTag, PRBool &aIsTag); - - // factored methods for handling insertion of data from transferables (drag&drop or clipboard) NS_IMETHOD PrepareTransferable(nsITransferable **transferable); NS_IMETHOD InsertTextFromTransferable(nsITransferable *transferable, @@ -236,6 +220,9 @@ protected: virtual nsresult SetupDocEncoder(nsIDocumentEncoder **aDocEncoder); virtual nsresult PutDragDataInTransferable(nsITransferable **aTransferable); + /** shared outputstring; returns whether selection is collapsed and resulting string */ + nsresult SharedOutputString(PRUint32 aFlags, PRBool* aIsCollapsed, nsAString& aResult); + /** simple utility to handle any error with event listener allocation or registration */ void HandleEventListenerError();