Skip to content

Commit

Permalink
Bug 1455674 part 3. Stop using nsIDOMElement in nsITableEditor. r=mas…
Browse files Browse the repository at this point in the history
…ayuki

The patch is a bit large because all these methods except
SswitchTableCellHeaderType call each other; doing it piecemeal would have
required introducing, then removing, a bunch of QIs.
  • Loading branch information
bzbarsky committed Apr 27, 2018
1 parent aab9f97 commit 2348052
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 467 deletions.
9 changes: 4 additions & 5 deletions editor/libeditor/EditorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4491,9 +4491,9 @@ EditorBase::CreateTxnForDeleteRange(nsRange* aRangeToDelete,
}

nsresult
EditorBase::CreateRange(nsIDOMNode* aStartContainer,
EditorBase::CreateRange(nsINode* aStartContainer,
int32_t aStartOffset,
nsIDOMNode* aEndContainer,
nsINode* aEndContainer,
int32_t aEndOffset,
nsRange** aRange)
{
Expand All @@ -4502,14 +4502,13 @@ EditorBase::CreateRange(nsIDOMNode* aStartContainer,
}

nsresult
EditorBase::AppendNodeToSelectionAsRange(nsIDOMNode* aNode)
EditorBase::AppendNodeToSelectionAsRange(nsINode* aNode)
{
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);

nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
nsCOMPtr<nsIDOMNode> parentNode = do_QueryInterface(node->GetParentNode());
nsCOMPtr<nsINode> parentNode = aNode->GetParentNode();
NS_ENSURE_TRUE(parentNode, NS_ERROR_NULL_POINTER);

int32_t offset = GetChildOffset(aNode, parentNode);
Expand Down
6 changes: 3 additions & 3 deletions editor/libeditor/EditorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1451,15 +1451,15 @@ class EditorBase : public nsIEditor
* Helpers to add a node to the selection.
* Used by table cell selection methods.
*/
nsresult CreateRange(nsIDOMNode* aStartContainer, int32_t aStartOffset,
nsIDOMNode* aEndContainer, int32_t aEndOffset,
nsresult CreateRange(nsINode* aStartContainer, int32_t aStartOffset,
nsINode* aEndContainer, int32_t aEndOffset,
nsRange** aRange);

/**
* Creates a range with just the supplied node and appends that to the
* selection.
*/
nsresult AppendNodeToSelectionAsRange(nsIDOMNode *aNode);
nsresult AppendNodeToSelectionAsRange(nsINode* aNode);

/**
* When you are using AppendNodeToSelectionAsRange(), call this first to
Expand Down
2 changes: 1 addition & 1 deletion editor/libeditor/HTMLEditRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,7 @@ HTMLEditRules::WillDeleteSelection(Selection* aSelection,
}

// First check for table selection mode. If so, hand off to table editor.
nsCOMPtr<nsIDOMElement> cell;
RefPtr<Element> cell;
NS_ENSURE_STATE(mHTMLEditor);
nsresult rv =
mHTMLEditor->GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
Expand Down
33 changes: 11 additions & 22 deletions editor/libeditor/HTMLEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ HTMLEditor::TabInTable(bool inIsShift,
// Put selection in right place. Use table code to get selection and index
// to new row...
RefPtr<Selection> selection;
nsCOMPtr<nsIDOMElement> tblElement, cell;
RefPtr<Element> tblElement, cell;
int32_t row;
rv = GetCellContext(getter_AddRefs(selection),
getter_AddRefs(tblElement),
Expand Down Expand Up @@ -1878,16 +1878,14 @@ HTMLEditor::GetHTMLBackgroundColorState(bool* aMixed,
*aMixed = false;
aOutColor.Truncate();

nsCOMPtr<nsIDOMElement> domElement;
RefPtr<Element> element;
int32_t selectedCount;
nsAutoString tagName;
nsresult rv = GetSelectedOrParentTableElement(tagName,
&selectedCount,
getter_AddRefs(domElement));
getter_AddRefs(element));
NS_ENSURE_SUCCESS(rv, rv);

nsCOMPtr<dom::Element> element = do_QueryInterface(domElement);

while (element) {
// We are in a cell or selected table
element->GetAttr(kNameSpaceID_None, nsGkAtoms::bgcolor, aOutColor);
Expand Down Expand Up @@ -2768,44 +2766,35 @@ HTMLEditor::SetHTMLBackgroundColorWithTransaction(const nsAString& aColor)
MOZ_ASSERT(IsInitialized(), "The HTMLEditor hasn't been initialized yet");

// Find a selected or enclosing table element to set background on
nsCOMPtr<nsIDOMElement> domElement;
RefPtr<Element> element;
int32_t selectedCount;
nsAutoString tagName;
nsresult rv = GetSelectedOrParentTableElement(tagName, &selectedCount,
getter_AddRefs(domElement));
getter_AddRefs(element));
NS_ENSURE_SUCCESS(rv, rv);

bool setColor = !aColor.IsEmpty();

nsCOMPtr<Element> element = nullptr;
RefPtr<nsAtom> bgColorAtom = NS_Atomize("bgcolor");
if (domElement) {
if (element) {
if (selectedCount > 0) {
// Traverse all selected cells
nsCOMPtr<nsIDOMElement> domCell;
rv = GetFirstSelectedCell(nullptr, getter_AddRefs(domCell));
if (NS_SUCCEEDED(rv) && domCell) {
while (domCell) {
nsCOMPtr<Element> cell = do_QueryInterface(domCell);
if (NS_WARN_IF(!cell)) {
return NS_ERROR_FAILURE;
}
RefPtr<Element> cell;
rv = GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
if (NS_SUCCEEDED(rv) && cell) {
while (cell) {
rv = setColor ?
SetAttributeWithTransaction(*cell, *bgColorAtom, aColor) :
RemoveAttributeWithTransaction(*cell, *bgColorAtom);
if (NS_FAILED(rv)) {
return rv;
}
GetNextSelectedCell(nullptr, getter_AddRefs(domCell));
GetNextSelectedCell(nullptr, getter_AddRefs(cell));
}
return NS_OK;
}
}
// If we failed to find a cell, fall through to use originally-found element
element = do_QueryInterface(domElement);
if (NS_WARN_IF(!element)) {
return NS_ERROR_FAILURE;
}
} else {
// No table element -- set the background color on the body tag
element = GetRoot();
Expand Down
61 changes: 29 additions & 32 deletions editor/libeditor/HTMLEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ class HTMLEditor final : public TextEditor
// nsITableEditor methods
NS_DECL_NSITABLEEDITOR

nsresult GetLastCellInRow(nsIDOMNode* aRowNode,
nsIDOMNode** aCellNode);
nsresult GetLastCellInRow(nsINode* aRowNode,
nsINode** aCellNode);

nsresult GetCellFromRange(nsRange* aRange, nsIDOMElement** aCell);
nsresult GetCellFromRange(nsRange* aRange, Element** aCell);

// Miscellaneous

Expand Down Expand Up @@ -684,46 +684,46 @@ class HTMLEditor final : public TextEditor
* of course)
* This doesn't change or use the current selection.
*/
nsresult InsertCell(nsIDOMElement* aCell, int32_t aRowSpan,
nsresult InsertCell(Element* aCell, int32_t aRowSpan,
int32_t aColSpan, bool aAfter, bool aIsHeader,
nsIDOMElement** aNewCell);
Element** aNewCell);

/**
* Helpers that don't touch the selection or do batch transactions.
*/
nsresult DeleteRow(nsIDOMElement* aTable, int32_t aRowIndex);
nsresult DeleteColumn(nsIDOMElement* aTable, int32_t aColIndex);
nsresult DeleteCellContents(nsIDOMElement* aCell);
nsresult DeleteRow(Element* aTable, int32_t aRowIndex);
nsresult DeleteColumn(Element* aTable, int32_t aColIndex);
nsresult DeleteCellContents(Element* aCell);

/**
* Move all contents from aCellToMerge into aTargetCell (append at end).
*/
nsresult MergeCells(nsCOMPtr<nsIDOMElement> aTargetCell,
nsCOMPtr<nsIDOMElement> aCellToMerge,
nsresult MergeCells(RefPtr<Element> aTargetCell,
RefPtr<Element> aCellToMerge,
bool aDeleteCellToMerge);

nsresult DeleteTable2(nsIDOMElement* aTable, Selection* aSelection);
nsresult SetColSpan(nsIDOMElement* aCell, int32_t aColSpan);
nsresult SetRowSpan(nsIDOMElement* aCell, int32_t aRowSpan);
nsresult DeleteTable2(Element* aTable, Selection* aSelection);
nsresult SetColSpan(Element* aCell, int32_t aColSpan);
nsresult SetRowSpan(Element* aCell, int32_t aRowSpan);

/**
* Helper used to get nsTableWrapperFrame for a table.
*/
nsTableWrapperFrame* GetTableFrame(nsIDOMElement* aTable);
nsTableWrapperFrame* GetTableFrame(Element* aTable);

/**
* Needed to do appropriate deleting when last cell or row is about to be
* deleted. This doesn't count cells that don't start in the given row (are
* spanning from row above).
*/
int32_t GetNumberOfCellsInRow(nsIDOMElement* aTable, int32_t rowIndex);
int32_t GetNumberOfCellsInRow(Element* aTable, int32_t rowIndex);

/**
* Test if all cells in row or column at given index are selected.
*/
bool AllCellsInRowSelected(nsIDOMElement* aTable, int32_t aRowIndex,
bool AllCellsInRowSelected(Element* aTable, int32_t aRowIndex,
int32_t aNumberOfColumns);
bool AllCellsInColumnSelected(nsIDOMElement* aTable, int32_t aColIndex,
bool AllCellsInColumnSelected(Element* aTable, int32_t aColIndex,
int32_t aNumberOfRows);

bool IsEmptyCell(Element* aCell);
Expand All @@ -737,33 +737,33 @@ class HTMLEditor final : public TextEditor
* Returns NS_EDITOR_ELEMENT_NOT_FOUND if cell is not found even if aCell is
* null.
*/
nsresult GetCellContext(Selection** aSelection, nsIDOMElement** aTable,
nsIDOMElement** aCell, nsIDOMNode** aCellParent,
nsresult GetCellContext(Selection** aSelection, Element** aTable,
Element** aCell, nsINode** aCellParent,
int32_t* aCellOffset, int32_t* aRowIndex,
int32_t* aColIndex);

nsresult GetCellSpansAt(nsIDOMElement* aTable, int32_t aRowIndex,
nsresult GetCellSpansAt(Element* aTable, int32_t aRowIndex,
int32_t aColIndex, int32_t& aActualRowSpan,
int32_t& aActualColSpan);

nsresult SplitCellIntoColumns(nsIDOMElement* aTable, int32_t aRowIndex,
nsresult SplitCellIntoColumns(Element* aTable, int32_t aRowIndex,
int32_t aColIndex, int32_t aColSpanLeft,
int32_t aColSpanRight,
nsIDOMElement** aNewCell);
Element** aNewCell);

nsresult SplitCellIntoRows(nsIDOMElement* aTable, int32_t aRowIndex,
nsresult SplitCellIntoRows(Element* aTable, int32_t aRowIndex,
int32_t aColIndex, int32_t aRowSpanAbove,
int32_t aRowSpanBelow, nsIDOMElement** aNewCell);
int32_t aRowSpanBelow, Element** aNewCell);

nsresult CopyCellBackgroundColor(nsIDOMElement* destCell,
nsIDOMElement* sourceCell);
nsresult CopyCellBackgroundColor(Element* aDestCell,
Element* aSourceCell);

/**
* Reduce rowspan/colspan when cells span into nonexistent rows/columns.
*/
nsresult FixBadRowSpan(nsIDOMElement* aTable, int32_t aRowIndex,
nsresult FixBadRowSpan(Element* aTable, int32_t aRowIndex,
int32_t& aNewRowCount);
nsresult FixBadColSpan(nsIDOMElement* aTable, int32_t aColIndex,
nsresult FixBadColSpan(Element* aTable, int32_t aColIndex,
int32_t& aNewColCount);

/**
Expand All @@ -772,9 +772,6 @@ class HTMLEditor final : public TextEditor
*/
nsresult SetSelectionAtDocumentStart(Selection* aSelection);

nsresult GetTableSize(Element* aTable,
int32_t* aRowCount, int32_t* aColCount);

// End of Table Editing utilities

static Element* GetEnclosingTable(nsINode* aNode);
Expand Down Expand Up @@ -1224,7 +1221,7 @@ class HTMLEditor final : public TextEditor
* AutoSelectionSetterAfterTableEdit stack-based object to
* insure we reset the caret in a table-editing method.
*/
void SetSelectionAfterTableEdit(nsIDOMElement* aTable,
void SetSelectionAfterTableEdit(Element* aTable,
int32_t aRow, int32_t aCol,
int32_t aDirection, bool aSelected);

Expand Down
2 changes: 1 addition & 1 deletion editor/libeditor/HTMLEditorDataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ HTMLEditor::DoInsertHTMLWithContext(const nsAString& aInputString,
// Are there any table elements in the list?
// check for table cell selection mode
bool cellSelectionMode = false;
nsCOMPtr<nsIDOMElement> cell;
RefPtr<Element> cell;
rv = GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
if (NS_SUCCEEDED(rv) && cell) {
cellSelectionMode = true;
Expand Down
Loading

0 comments on commit 2348052

Please sign in to comment.