--- a/editor/libeditor/HTMLTableEditor.cpp
+++ b/editor/libeditor/HTMLTableEditor.cpp
@@ -23,17 +23,16 @@
#include "nsIContent.h"
#include "nsIDOMElement.h"
#include "nsIDOMNode.h"
#include "nsIFrame.h"
#include "nsINode.h"
#include "nsIPresShell.h"
#include "nsISupportsUtils.h"
#include "nsITableCellLayout.h" // For efficient access to table cell
-#include "nsITableEditor.h"
#include "nsLiteralString.h"
#include "nsQueryFrame.h"
#include "nsRange.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsTableCellFrame.h"
#include "nsTableWrapperFrame.h"
#include "nscore.h"
@@ -44,49 +43,49 @@ namespace mozilla {
using namespace dom;
/**
* Stack based helper class for restoring selection after table edit.
*/
class MOZ_STACK_CLASS AutoSelectionSetterAfterTableEdit final
{
private:
- nsCOMPtr<nsITableEditor> mTableEditor;
+ RefPtr<HTMLEditor> mHTMLEditor;
nsCOMPtr<nsIDOMElement> mTable;
int32_t mCol, mRow, mDirection, mSelected;
public:
- AutoSelectionSetterAfterTableEdit(nsITableEditor* aTableEditor,
+ AutoSelectionSetterAfterTableEdit(HTMLEditor& aHTMLEditor,
nsIDOMElement* aTable,
int32_t aRow,
int32_t aCol,
int32_t aDirection,
bool aSelected)
- : mTableEditor(aTableEditor)
+ : mHTMLEditor(&aHTMLEditor)
, mTable(aTable)
, mCol(aCol)
, mRow(aRow)
, mDirection(aDirection)
, mSelected(aSelected)
{
}
~AutoSelectionSetterAfterTableEdit()
{
- if (mTableEditor) {
- mTableEditor->SetSelectionAfterTableEdit(mTable, mRow, mCol, mDirection,
- mSelected);
+ if (mHTMLEditor) {
+ mHTMLEditor->SetSelectionAfterTableEdit(mTable, mRow, mCol, mDirection,
+ mSelected);
}
}
// This is needed to abort the caret reset in the destructor
// when one method yields control to another
void CancelSetCaret()
{
- mTableEditor = nullptr;
+ mHTMLEditor = nullptr;
mTable = nullptr;
}
};
NS_IMETHODIMP
HTMLEditor::InsertCell(nsIDOMElement* aCell,
int32_t aRowSpan,
int32_t aColSpan,
@@ -188,17 +187,17 @@ HTMLEditor::InsertTableCell(int32_t aNum
rv = GetCellDataAt(table, startRowIndex, startColIndex,
getter_AddRefs(curCell),
&curStartRowIndex, &curStartColIndex, &rowSpan, &colSpan,
&actualRowSpan, &actualColSpan, &isSelected);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(curCell, NS_ERROR_FAILURE);
int32_t newCellIndex = aAfter ? (startColIndex+colSpan) : startColIndex;
//We control selection resetting after the insert...
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
newCellIndex, ePreviousColumn,
false);
//...so suppress Rules System selection munging
AutoTransactionsConserveSelection dontChangeSelection(this);
for (int32_t i = 0; i < aNumber; i++) {
nsCOMPtr<nsIDOMElement> newCell;
rv = CreateElementWithDefaults(NS_LITERAL_STRING("td"),
@@ -432,17 +431,17 @@ HTMLEditor::InsertTableColumn(int32_t aN
}
}
int32_t rowCount, colCount, rowIndex;
rv = GetTableSize(table, &rowCount, &colCount);
NS_ENSURE_SUCCESS(rv, rv);
//We reset caret in destructor...
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
startColIndex, ePreviousRow,
false);
//.. so suppress Rules System selection munging
AutoTransactionsConserveSelection dontChangeSelection(this);
// If we are inserting after all existing columns
// Make sure table is "well formed"
// before appending new column
@@ -564,17 +563,17 @@ HTMLEditor::InsertTableRow(int32_t aNumb
// really add a new row. Thus we set the
// rowspan to its true value
if (!rowSpan) {
SetRowSpan(curCell, actualRowSpan);
}
}
//We control selection resetting after the insert...
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
startColIndex, ePreviousColumn,
false);
//...so suppress Rules System selection munging
AutoTransactionsConserveSelection dontChangeSelection(this);
nsCOMPtr<nsIDOMElement> cellForRowParent;
int32_t cellsInRow = 0;
if (startRowIndex < rowCount) {
@@ -779,17 +778,17 @@ HTMLEditor::DeleteTableCell(int32_t aNum
NS_ENSURE_SUCCESS(rv, rv);
// Get indexes -- may be different than original cell
rv = GetCellIndexes(cell, &startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(rv, rv);
// The setCaret object will call AutoSelectionSetterAfterTableEdit in its
// destructor
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
startColIndex, ePreviousColumn,
false);
AutoTransactionsConserveSelection dontChangeSelection(this);
bool checkToDeleteRow = true;
bool checkToDeleteColumn = true;
while (cell) {
bool deleteRow = false;
@@ -909,17 +908,17 @@ HTMLEditor::DeleteTableCell(int32_t aNum
// We need to call DeleteTableRow to handle cells with rowspan
rv = DeleteTableRow(1);
NS_ENSURE_SUCCESS(rv, rv);
} else {
// More than 1 cell in the row
// The setCaret object will call AutoSelectionSetterAfterTableEdit in its
// destructor
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
startColIndex, ePreviousColumn,
false);
AutoTransactionsConserveSelection dontChangeSelection(this);
rv = DeleteNode(cell);
// If we fail, don't try to delete any more cells???
NS_ENSURE_SUCCESS(rv, rv);
}
@@ -959,17 +958,17 @@ HTMLEditor::DeleteTableCellContents()
if (firstCell) {
cell = firstCell;
rv = GetCellIndexes(cell, &startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(rv, rv);
}
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
startColIndex, ePreviousColumn,
false);
while (cell) {
DeleteCellContents(cell);
if (firstCell) {
// We doing a selected cells, so do all of them
rv = GetNextSelectedCell(nullptr, getter_AddRefs(cell));
@@ -1044,17 +1043,17 @@ HTMLEditor::DeleteTableColumn(int32_t aN
NS_ENSURE_SUCCESS(rv, rv);
if (firstCell && rangeCount > 1) {
// Fetch indexes again - may be different for selected cells
rv = GetCellIndexes(firstCell, &startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(rv, rv);
}
//We control selection resetting after the insert...
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
startColIndex, ePreviousRow,
false);
if (firstCell && rangeCount > 1) {
// Use selected cells to determine what rows to delete
cell = firstCell;
while (cell) {
@@ -1210,17 +1209,17 @@ HTMLEditor::DeleteTableRow(int32_t aNumb
if (firstCell && rangeCount > 1) {
// Fetch indexes again - may be different for selected cells
rv = GetCellIndexes(firstCell, &startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(rv, rv);
}
//We control selection resetting after the insert...
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
startColIndex, ePreviousRow,
false);
// Don't change selection during deletions
AutoTransactionsConserveSelection dontChangeSelection(this);
if (firstCell && rangeCount > 1) {
// Use selected cells to determine what rows to delete
cell = firstCell;
@@ -1735,17 +1734,17 @@ HTMLEditor::SplitTableCell()
return NS_OK;
}
AutoEditBatch beginBatching(this);
// Prevent auto insertion of BR in new cell until we're done
AutoRules beginRulesSniffing(this, EditAction::insertNode, nsIEditor::eNext);
// We reset selection
- AutoSelectionSetterAfterTableEdit setCaret(this, table, startRowIndex,
+ AutoSelectionSetterAfterTableEdit setCaret(*this, table, startRowIndex,
startColIndex, ePreviousColumn,
false);
//...so suppress Rules System selection munging
AutoTransactionsConserveSelection dontChangeSelection(this);
nsCOMPtr<nsIDOMElement> newCell;
int32_t rowIndex = startRowIndex;
int32_t rowSpanBelow, colSpanAfter;
@@ -3092,102 +3091,101 @@ HTMLEditor::GetFirstSelectedCellInTable(
if (aColIndex) {
*aColIndex = startColIndex;
}
}
return NS_OK;
}
-NS_IMETHODIMP
+void
HTMLEditor::SetSelectionAfterTableEdit(nsIDOMElement* aTable,
int32_t aRow,
int32_t aCol,
int32_t aDirection,
bool aSelected)
{
- NS_ENSURE_TRUE(aTable, NS_ERROR_NOT_INITIALIZED);
-
- if (Destroyed()) {
- return NS_ERROR_FAILURE;
+ if (NS_WARN_IF(!aTable) || Destroyed()) {
+ return;
}
RefPtr<Selection> selection = GetSelection();
-
if (!selection) {
- return NS_ERROR_FAILURE;
+ return;
}
nsCOMPtr<nsIDOMElement> cell;
bool done = false;
do {
nsresult rv = GetCellAt(aTable, aRow, aCol, getter_AddRefs(cell));
if (NS_FAILED(rv)) {
break;
}
if (cell) {
if (aSelected) {
// Reselect the cell
- return SelectElement(cell);
- } else {
- // Set the caret to deepest first child
- // but don't go into nested tables
- // TODO: Should we really be placing the caret at the END
- // of the cell content?
- nsCOMPtr<nsINode> cellNode = do_QueryInterface(cell);
- if (cellNode) {
- CollapseSelectionToDeepestNonTableFirstChild(selection, cellNode);
- }
- return NS_OK;
+ SelectElement(cell);
+ return;
+ }
+
+ // Set the caret to deepest first child
+ // but don't go into nested tables
+ // TODO: Should we really be placing the caret at the END
+ // of the cell content?
+ nsCOMPtr<nsINode> cellNode = do_QueryInterface(cell);
+ if (cellNode) {
+ CollapseSelectionToDeepestNonTableFirstChild(selection, cellNode);
}
- } else {
- // Setup index to find another cell in the
- // direction requested, but move in
- // other direction if already at beginning of row or column
- switch (aDirection) {
- case ePreviousColumn:
- if (!aCol) {
- if (aRow > 0) {
- aRow--;
- } else {
- done = true;
- }
+ return;
+ }
+
+ // Setup index to find another cell in the
+ // direction requested, but move in other direction if already at
+ // beginning of row or column
+ switch (aDirection) {
+ case ePreviousColumn:
+ if (!aCol) {
+ if (aRow > 0) {
+ aRow--;
} else {
- aCol--;
+ done = true;
}
- break;
- case ePreviousRow:
- if (!aRow) {
- if (aCol > 0) {
- aCol--;
- } else {
- done = true;
- }
+ } else {
+ aCol--;
+ }
+ break;
+ case ePreviousRow:
+ if (!aRow) {
+ if (aCol > 0) {
+ aCol--;
} else {
- aRow--;
+ done = true;
}
- break;
- default:
- done = true;
- }
+ } else {
+ aRow--;
+ }
+ break;
+ default:
+ done = true;
}
} while (!done);
// We didn't find a cell
// Set selection to just before the table
nsCOMPtr<nsIDOMNode> tableParent;
nsresult rv = aTable->GetParentNode(getter_AddRefs(tableParent));
if (NS_SUCCEEDED(rv) && tableParent) {
int32_t tableOffset = GetChildOffset(aTable, tableParent);
- return selection->Collapse(tableParent, tableOffset);
+ selection->Collapse(tableParent, tableOffset);
+ return;
}
// Last resort: Set selection to start of doc
// (it's very bad to not have a valid selection!)
- return SetSelectionAtDocumentStart(selection);
+ SetSelectionAtDocumentStart(selection);
}
NS_IMETHODIMP
HTMLEditor::GetSelectedOrParentTableElement(nsAString& aTagName,
int32_t* aSelectedCount,
nsIDOMElement** aTableElement)
{
NS_ENSURE_ARG_POINTER(aTableElement);