From e220fdf55ab292f437daca1ceb6bf585afbb7834 Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Tue, 17 May 2022 00:12:10 +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`. Depends on D146361 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 f92844681217..3b4520cff57e 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -2718,7 +2718,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 ff6a4ccd4d96..de9403e45577 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 e5fe218683fe..b486d163c3a3 100644 --- a/editor/libeditor/HTMLTableEditor.cpp +++ b/editor/libeditor/HTMLTableEditor.cpp @@ -475,18 +475,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); @@ -495,15 +498,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. @@ -513,13 +515,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, @@ -747,15 +749,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");