From f8da7ad6121b40fdf33e0a66a4f3f66afc38ca7a Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Fri, 20 May 2022 07:39:52 +0000 Subject: [PATCH] Bug 1730442 - part 4: Rewrite `HTMLEditor::GetFirstTableRowElement()` as returing `Result, nsresult>` r=m_kato This change is tested by `test_nsITableEditor_getFirstRow.html` and `test_nsITableEditor_insertTableColumn.html`. Differential Revision: https://phabricator.services.mozilla.com/D146362 --- editor/libeditor/HTMLEditor.cpp | 2 +- editor/libeditor/HTMLEditor.h | 17 +++++------- editor/libeditor/HTMLTableEditor.cpp | 40 +++++++++++++++------------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index 6f70651dc50f..cc05edd12668 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -2717,7 +2717,7 @@ Element* HTMLEditor::GetInclusiveAncestorByTagNameAtSelection( } Element* HTMLEditor::GetInclusiveAncestorByTagNameInternal( - const nsStaticAtom& aTagName, nsIContent& aContent) const { + const nsStaticAtom& aTagName, const nsIContent& aContent) const { MOZ_ASSERT(&aTagName != nsGkAtoms::_empty); Element* currentElement = aContent.GetAsElementOrParentElement(); diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index 0c32766a9771..fe7b9e8c6843 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -2891,8 +2891,8 @@ class HTMLEditor final : public EditorBase, * @return If an element which matches aTagName, returns * an Element. Otherwise, nullptr. */ - Element* GetInclusiveAncestorByTagNameInternal(const nsStaticAtom& aTagName, - nsIContent& aContent) const; + Element* GetInclusiveAncestorByTagNameInternal( + const nsStaticAtom& aTagName, const nsIContent& aContent) const; /** * GetSelectedElement() returns a "selected" element node. "selected" means: @@ -2928,21 +2928,18 @@ class HTMLEditor final : public EditorBase, /** * GetFirstTableRowElement() returns the first element in the most * nearest ancestor of aTableOrElementInTable or itself. + * When aTableOrElementInTable is neither nor in a
element, + * returns NS_ERROR_FAILURE. However, if
does not have element, + * returns nullptr. * * @param aTableOrElementInTable
element or another element. * If this is a
element, returns * first element in it. Otherwise, * returns first element in nearest * ancestor
element. - * @param aRv Returns an error code. When - * aTableOrElementInTable is neither - *
nor in a
element, - * returns NS_ERROR_FAILURE. - * However, if
does not have - * element, returns NS_OK. */ - Element* GetFirstTableRowElement(Element& aTableOrElementInTable, - ErrorResult& aRv) const; + Result, nsresult> GetFirstTableRowElement( + const Element& aTableOrElementInTable) const; /** * GetNextTableRowElement() returns next element of aTableRowElement. diff --git a/editor/libeditor/HTMLTableEditor.cpp b/editor/libeditor/HTMLTableEditor.cpp index 26b90f784daf..29e79cf16a83 100644 --- a/editor/libeditor/HTMLTableEditor.cpp +++ b/editor/libeditor/HTMLTableEditor.cpp @@ -487,18 +487,21 @@ NS_IMETHODIMP HTMLEditor::GetFirstRow(Element* aTableOrElementInTable, return EditorBase::ToGenericNSResult(rv); } - ErrorResult error; - RefPtr firstRowElement = - GetFirstTableRowElement(*aTableOrElementInTable, error); - NS_WARNING_ASSERTION(!error.Failed(), + Result, nsresult> firstRowElementOrError = + GetFirstTableRowElement(*aTableOrElementInTable); + NS_WARNING_ASSERTION(!firstRowElementOrError.isErr(), "HTMLEditor::GetFirstTableRowElement() failed"); - firstRowElement.forget(aFirstRowElement); - return EditorBase::ToGenericNSResult(error.StealNSResult()); + if (firstRowElementOrError.isErr()) { + NS_WARNING("HTMLEditor::GetFirstTableRowElement() failed"); + return EditorBase::ToGenericNSResult(firstRowElementOrError.unwrapErr()); + } + firstRowElementOrError.unwrap().forget(aFirstRowElement); + return NS_OK; } -Element* HTMLEditor::GetFirstTableRowElement(Element& aTableOrElementInTable, - ErrorResult& aRv) const { - MOZ_ASSERT(!aRv.Failed()); +Result, nsresult> HTMLEditor::GetFirstTableRowElement( + const Element& aTableOrElementInTable) const { + MOZ_ASSERT(IsEditActionDataAvailable()); Element* tableElement = GetInclusiveAncestorByTagNameInternal( *nsGkAtoms::table, aTableOrElementInTable); @@ -507,15 +510,14 @@ Element* HTMLEditor::GetFirstTableRowElement(Element& aTableOrElementInTable, NS_WARNING( "HTMLEditor::GetInclusiveAncestorByTagNameInternal(nsGkAtoms::table) " "failed"); - aRv.Throw(NS_ERROR_FAILURE); - return nullptr; + return Err(NS_ERROR_FAILURE); } for (nsIContent* tableChild = tableElement->GetFirstChild(); tableChild; tableChild = tableChild->GetNextSibling()) { if (tableChild->IsHTMLElement(nsGkAtoms::tr)) { // Found a row directly under
- return tableChild->AsElement(); + return RefPtr(tableChild->AsElement()); } //
can have table section elements like . elements // may be children of them. @@ -525,13 +527,13 @@ Element* HTMLEditor::GetFirstTableRowElement(Element& aTableOrElementInTable, tableSectionChild; tableSectionChild = tableSectionChild->GetNextSibling()) { if (tableSectionChild->IsHTMLElement(nsGkAtoms::tr)) { - return tableSectionChild->AsElement(); + return RefPtr(tableSectionChild->AsElement()); } } } } // Don't return error when there is no element in the
. - return nullptr; + return RefPtr(); } Element* HTMLEditor::GetNextTableRowElement(Element& aTableRowElement, @@ -762,15 +764,17 @@ nsresult HTMLEditor::InsertTableColumnsWithTransaction( // Get current row and append new cells after last cell in row if (!rowIndex) { - rowElement = GetFirstTableRowElement(*table, error); - if (error.Failed()) { + Result, nsresult> rowElementOrError = + GetFirstTableRowElement(*table); + if (rowElementOrError.isErr()) { NS_WARNING("HTMLEditor::GetFirstTableRowElement() failed"); - return error.StealNSResult(); + return rowElementOrError.unwrapErr(); } - if (!rowElement) { + if (!rowElementOrError.inspect()) { NS_WARNING("There was no table row"); continue; } + rowElement = rowElementOrError.unwrap(); } else { if (!rowElement) { NS_WARNING("Have not found table row yet");