From 1a08039730aad2552effa68045a9272f8325dfcd Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Mon, 17 Aug 2020 09:22:19 +0000 Subject: [PATCH] Bug 1658472 - part 1: Move `HTMLEditor::TryToJoinBlocksWithTransaction()` into new nested class of `AutoBlockElementsJoiner` r=m_kato Now all users of the method is methods of `AutoBlockElementsJoiner`. Therefore, we can move it into the nested class. And for splitting the method in the following patch, it should be a nested class of `AutoBlockElementJoiner`. Depends on D86791 Differential Revision: https://phabricator.services.mozilla.com/D86881 --- editor/libeditor/HTMLEditSubActionHandler.cpp | 47 +++++++++++-------- editor/libeditor/HTMLEditor.h | 45 +++++++++--------- 2 files changed, 51 insertions(+), 41 deletions(-) diff --git a/editor/libeditor/HTMLEditSubActionHandler.cpp b/editor/libeditor/HTMLEditSubActionHandler.cpp index 9e3f73a28a32..bf3013a3025f 100644 --- a/editor/libeditor/HTMLEditSubActionHandler.cpp +++ b/editor/libeditor/HTMLEditSubActionHandler.cpp @@ -2448,8 +2448,9 @@ EditActionResult HTMLEditor::HandleDeleteSelectionInternal( // blocks in HandleDeleteNonCollapsedRanges(). We don't really care about // collapsed because it will be modified by // AutoRangeArray::ExtendAnchorFocusRangeFor() later. - // TryToJoinBlocksWithTransaction() should happen if the original selection is - // collapsed and the cursor is at the end of a block element, in which case + // AutoBlockElementsJoiner::AutoInclusiveAncestorBlockElementsJoiner should + // happen if the original selection is collapsed and the cursor is at the end + // of a block element, in which case // AutoRangeArray::ExtendAnchorFocusRangeFor() would always make the selection // not collapsed. SelectionWasCollapsed selectionWasCollapsed = aRangesToDelete.IsCollapsed() @@ -3215,15 +3216,16 @@ EditActionResult HTMLEditor::AutoBlockElementsJoiner:: EditorDOMPoint pointToPutCaret(aCaretPoint); { AutoTrackDOMPoint tracker(aHTMLEditor.RangeUpdaterRef(), &pointToPutCaret); - result |= aHTMLEditor.TryToJoinBlocksWithTransaction( - MOZ_KnownLive(*mLeftContent), MOZ_KnownLive(*mRightContent)); + AutoInclusiveAncestorBlockElementsJoiner joiner; + result |= joiner.Run(aHTMLEditor, MOZ_KnownLive(*mLeftContent), + MOZ_KnownLive(*mRightContent)); if (result.Failed()) { - NS_WARNING("HTMLEditor::TryToJoinBlocksWithTransaction() failed"); + NS_WARNING("AutoInclusiveAncestorBlockElementsJoiner::Run() failed"); return result; } } - // If TryToJoinBlocksWithTransaction() didn't handle it and it's not + // If AutoInclusiveAncestorBlockElementsJoiner didn't handle it and it's not // canceled, user may want to modify the start leaf node or the last leaf // node of the block. if (!result.Handled() && !result.Canceled() && @@ -3287,14 +3289,15 @@ EditActionResult HTMLEditor::AutoBlockElementsJoiner:: EditorDOMPoint pointToPutCaret(aCaretPoint); { AutoTrackDOMPoint tracker(aHTMLEditor.RangeUpdaterRef(), &pointToPutCaret); - result |= aHTMLEditor.TryToJoinBlocksWithTransaction( - MOZ_KnownLive(*mLeftContent), MOZ_KnownLive(*mRightContent)); + AutoInclusiveAncestorBlockElementsJoiner joiner; + result |= joiner.Run(aHTMLEditor, MOZ_KnownLive(*mLeftContent), + MOZ_KnownLive(*mRightContent)); // This should claim that trying to join the block means that // this handles the action because the caller shouldn't do anything // anymore in this case. result.MarkAsHandled(); if (result.Failed()) { - NS_WARNING("HTMLEditor::TryToJoinBlocksWithTransaction() failed"); + NS_WARNING("AutoInclusiveAncestorBlockElementsJoiner::Run() failed"); return result; } } @@ -3696,10 +3699,11 @@ HTMLEditor::AutoBlockElementsJoiner::HandleDeleteNonCollapsedRanges( } if (joinInclusiveAncestorBlockElements) { - result |= aHTMLEditor.TryToJoinBlocksWithTransaction( - MOZ_KnownLive(*mLeftContent), MOZ_KnownLive(*mRightContent)); + AutoInclusiveAncestorBlockElementsJoiner joiner; + result |= joiner.Run(aHTMLEditor, MOZ_KnownLive(*mLeftContent), + MOZ_KnownLive(*mRightContent)); if (result.Failed()) { - NS_WARNING("HTMLEditor::TryToJoinBlocksWithTransaction() failed"); + NS_WARNING("AutoInclusiveAncestorBlockElementsJoiner::Run() failed"); return result; } @@ -4595,9 +4599,11 @@ HTMLEditor::AutoBlockElementsJoiner::JoinNodesDeepWithTransaction( return ret; } -EditActionResult HTMLEditor::TryToJoinBlocksWithTransaction( - nsIContent& aLeftContentInBlock, nsIContent& aRightContentInBlock) { - MOZ_ASSERT(IsEditActionDataAvailable()); +EditActionResult HTMLEditor::AutoBlockElementsJoiner:: + AutoInclusiveAncestorBlockElementsJoiner::Run( + HTMLEditor& aHTMLEditor, nsIContent& aLeftContentInBlock, + nsIContent& aRightContentInBlock) { + MOZ_ASSERT(aHTMLEditor.IsEditActionDataAvailable()); RefPtr leftBlockElement = HTMLEditUtils::GetInclusiveAncestorBlockElement(aLeftContentInBlock); @@ -4682,8 +4688,8 @@ EditActionResult HTMLEditor::TryToJoinBlocksWithTransaction( &atRightBlockChild)) { EditActionResult result = WhiteSpaceVisibilityKeeper:: MergeFirstLineOfRightBlockElementIntoDescendantLeftBlockElement( - *this, *leftBlockElement, *rightBlockElement, atRightBlockChild, - newListElementTagNameOfRightListElement); + aHTMLEditor, *leftBlockElement, *rightBlockElement, + atRightBlockChild, newListElementTagNameOfRightListElement); NS_WARNING_ASSERTION(result.Succeeded(), "WhiteSpaceVisibilityKeeper::" "MergeFirstLineOfRightBlockElementIntoDescendantLeftBl" @@ -4704,8 +4710,9 @@ EditActionResult HTMLEditor::TryToJoinBlocksWithTransaction( &atLeftBlockChild)) { EditActionResult result = WhiteSpaceVisibilityKeeper:: MergeFirstLineOfRightBlockElementIntoAncestorLeftBlockElement( - *this, *leftBlockElement, *rightBlockElement, atLeftBlockChild, - aLeftContentInBlock, newListElementTagNameOfRightListElement); + aHTMLEditor, *leftBlockElement, *rightBlockElement, + atLeftBlockChild, aLeftContentInBlock, + newListElementTagNameOfRightListElement); NS_WARNING_ASSERTION(result.Succeeded(), "WhiteSpaceVisibilityKeeper::" "MergeFirstLineOfRightBlockElementIntoAncestorLeftBloc" @@ -4719,7 +4726,7 @@ EditActionResult HTMLEditor::TryToJoinBlocksWithTransaction( // if you backspace from li into p. EditActionResult result = WhiteSpaceVisibilityKeeper:: MergeFirstLineOfRightBlockElementIntoLeftBlockElement( - *this, *leftBlockElement, *rightBlockElement, + aHTMLEditor, *leftBlockElement, *rightBlockElement, newListElementTagNameOfRightListElement); NS_WARNING_ASSERTION( result.Succeeded(), diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index 27f9ff414f71..ba86e4a79dcc 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -2363,27 +2363,6 @@ class HTMLEditor final : public TextEditor, const EditorDOMPoint& aDeepestStartOfRightNode, SplitAtEdges aSplitAtEdges); - /** - * TryToJoinBlocksWithTransaction() tries to join two block elements. The - * right element is always joined to the left element. If the elements are - * the same type and not nested within each other, - * JoinEditableNodesWithTransaction() is called (example, joining two list - * items together into one). If the elements are not the same type, or one - * is a descendant of the other, we instead destroy the right block placing - * its children into leftblock. - * - * @return Sets canceled to true if the operation should do - * nothing anymore even if this doesn't join the blocks. - * Sets handled to true if this actually handles the - * request. Note that this may set it to true even if this - * does not join the block. E.g., if the blocks shouldn't - * be joined or it's impossible to join them but it's not - * unexpected case, this returns true with this. - */ - [[nodiscard]] MOZ_CAN_RUN_SCRIPT EditActionResult - TryToJoinBlocksWithTransaction(nsIContent& aLeftContentInBlock, - nsIContent& aRightContentInBlock); - /** * GetGoodCaretPointFor() returns a good point to collapse `Selection` * after handling edit action with aDirectionAndAmount. @@ -2957,6 +2936,30 @@ class HTMLEditor final : public TextEditor, [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult DeleteTextAtStartAndEndOfRange(HTMLEditor& aHTMLEditor, nsRange& aRange); + class MOZ_STACK_CLASS AutoInclusiveAncestorBlockElementsJoiner final { + public: + /** + * Tries to join two block elements. The right element is always joined + * to the left element. If the elements are the same type and not nested + * within each other, JoinEditableNodesWithTransaction() is called + * (example, joining two list items together into one). If the elements + * are not the same type, or one is a descendant of the other, we instead + * destroy the right block placing its children into leftblock. + * + * @return Sets canceled to true if the operation should do + * nothing anymore even if this doesn't join the blocks. + * Sets handled to true if this actually handles the + * request. Note that this may set it to true even if + * this does not join the block. E.g., if the blocks + * shouldn't be joined or it's impossible to join them + * but it's not unexpected case, this return true with + * this. + */ + [[nodiscard]] MOZ_CAN_RUN_SCRIPT EditActionResult + Run(HTMLEditor& aHTMLEditor, nsIContent& aLeftContentInBlock, + nsIContent& aRightContentInBlock); + }; + enum class Mode { NotInitialized, JoinCurrentBlock,