From d0797846a4cbafd5f8af58c87b3e5588d1a04fec Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Thu, 22 Aug 2019 08:29:16 +0000 Subject: [PATCH] Bug 1574852 - part 12: Move `HTMLEditRules::SplitMailCites()` to `HTMLEditor` r=m_kato Differential Revision: https://phabricator.services.mozilla.com/D42783 --HG-- extra : moz-landing-system : lando --- editor/libeditor/HTMLEditRules.cpp | 74 ++++++++++++++---------------- editor/libeditor/HTMLEditRules.h | 9 ---- editor/libeditor/HTMLEditor.h | 11 +++++ 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/editor/libeditor/HTMLEditRules.cpp b/editor/libeditor/HTMLEditRules.cpp index 720f11eed7db..79b81ceaba76 100644 --- a/editor/libeditor/HTMLEditRules.cpp +++ b/editor/libeditor/HTMLEditRules.cpp @@ -1673,7 +1673,13 @@ EditActionResult HTMLEditRules::WillInsertParagraphSeparator() { // Split any mailcites in the way. Should we abort this if we encounter // table cell boundaries? if (IsMailEditor()) { - EditActionResult result = SplitMailCites(); + EditorDOMPoint pointToSplit(EditorBase::GetStartPoint(*SelectionRefPtr())); + if (NS_WARN_IF(!pointToSplit.IsSet())) { + return EditActionIgnored(NS_ERROR_FAILURE); + } + + EditActionResult result = + MOZ_KnownLive(HTMLEditorRef()).SplitMailCiteElements(pointToSplit); if (NS_WARN_IF(result.Failed())) { return result; } @@ -2039,20 +2045,19 @@ nsresult HTMLEditRules::InsertBRElement(const EditorDOMPoint& aPointToBreak) { return NS_OK; } -EditActionResult HTMLEditRules::SplitMailCites() { - MOZ_ASSERT(IsEditorDataAvailable()); +EditActionResult HTMLEditor::SplitMailCiteElements( + const EditorDOMPoint& aPointToSplit) { + MOZ_ASSERT(IsEditActionDataAvailable()); + MOZ_ASSERT(aPointToSplit.IsSet()); - EditorDOMPoint pointToSplit(EditorBase::GetStartPoint(*SelectionRefPtr())); - if (NS_WARN_IF(!pointToSplit.IsSet())) { - return EditActionIgnored(NS_ERROR_FAILURE); - } - - RefPtr citeNode = HTMLEditorRef().GetMostAncestorMailCiteElement( - *pointToSplit.GetContainer()); + RefPtr citeNode = + GetMostAncestorMailCiteElement(*aPointToSplit.GetContainer()); if (!citeNode) { return EditActionIgnored(); } + EditorDOMPoint pointToSplit(aPointToSplit); + // If our selection is just before a break, nudge it to be just after it. // This does two things for us. It saves us the trouble of having to add // a break here ourselves to preserve the "blockness" of the inline span @@ -2062,7 +2067,7 @@ EditActionResult HTMLEditRules::SplitMailCites() { // The latter can confuse a user if they click there and start typing, // because being in the mailquote may affect wrapping behavior, or font // color, etc. - WSRunObject wsObj(&HTMLEditorRef(), pointToSplit); + WSRunObject wsObj(this, pointToSplit); nsCOMPtr visNode; WSType wsType; wsObj.NextVisibleNode(pointToSplit, address_of(visNode), nullptr, &wsType); @@ -2080,12 +2085,9 @@ EditActionResult HTMLEditRules::SplitMailCites() { return EditActionIgnored(NS_ERROR_FAILURE); } - SplitNodeResult splitCiteNodeResult = - MOZ_KnownLive(HTMLEditorRef()) - .SplitNodeDeepWithTransaction( - *citeNode, pointToSplit, - SplitAtEdges::eDoNotCreateEmptyContainer); - if (NS_WARN_IF(!CanHandleEditAction())) { + SplitNodeResult splitCiteNodeResult = SplitNodeDeepWithTransaction( + *citeNode, pointToSplit, SplitAtEdges::eDoNotCreateEmptyContainer); + if (NS_WARN_IF(Destroyed())) { return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); } if (NS_WARN_IF(splitCiteNodeResult.Failed())) { @@ -2113,9 +2115,8 @@ EditActionResult HTMLEditRules::SplitMailCites() { EditorDOMPoint endOfPreviousNodeOfSplitPoint; endOfPreviousNodeOfSplitPoint.SetToEndOf(previousNodeOfSplitPoint); RefPtr invisibleBrElement = - MOZ_KnownLive(HTMLEditorRef()) - .InsertBRElementWithTransaction(endOfPreviousNodeOfSplitPoint); - if (NS_WARN_IF(!CanHandleEditAction())) { + InsertBRElementWithTransaction(endOfPreviousNodeOfSplitPoint); + if (NS_WARN_IF(Destroyed())) { return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); } NS_WARNING_ASSERTION(invisibleBrElement, @@ -2128,9 +2129,8 @@ EditActionResult HTMLEditRules::SplitMailCites() { // cite node,
should be inserted before the current cite. EditorDOMPoint pointToInsertBrNode(splitCiteNodeResult.SplitPoint()); RefPtr brElement = - MOZ_KnownLive(HTMLEditorRef()) - .InsertBRElementWithTransaction(pointToInsertBrNode); - if (NS_WARN_IF(!CanHandleEditAction())) { + InsertBRElementWithTransaction(pointToInsertBrNode); + if (NS_WARN_IF(Destroyed())) { return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); } if (NS_WARN_IF(!brElement)) { @@ -2147,7 +2147,7 @@ EditActionResult HTMLEditRules::SplitMailCites() { NS_WARNING_ASSERTION(!error.Failed(), "Failed to set interline position"); error = NS_OK; SelectionRefPtr()->Collapse(atBrNode, error); - if (NS_WARN_IF(!CanHandleEditAction())) { + if (NS_WARN_IF(Destroyed())) { error.SuppressException(); return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); } @@ -2164,7 +2164,7 @@ EditActionResult HTMLEditRules::SplitMailCites() { EditorDOMPoint pointToCreateNewBrNode(atBrNode.GetContainer(), atBrNode.Offset()); - WSRunObject wsObj(&HTMLEditorRef(), pointToCreateNewBrNode); + WSRunObject wsObj(this, pointToCreateNewBrNode); WSType wsType; wsObj.PriorVisibleNode(pointToCreateNewBrNode, nullptr, nullptr, &wsType); if (wsType == WSType::normalWS || wsType == WSType::text || @@ -2173,15 +2173,14 @@ EditActionResult HTMLEditRules::SplitMailCites() { DebugOnly advanced = pointAfterNewBrNode.AdvanceOffset(); NS_WARNING_ASSERTION(advanced, "Failed to advance offset after the
node"); - WSRunObject wsObjAfterBR(&HTMLEditorRef(), pointAfterNewBrNode); + WSRunObject wsObjAfterBR(this, pointAfterNewBrNode); wsObjAfterBR.NextVisibleNode(pointAfterNewBrNode, &wsType); if (wsType == WSType::normalWS || wsType == WSType::text || wsType == WSType::special || // In case we're at the very end. wsType == WSType::thisBlock) { - brElement = MOZ_KnownLive(HTMLEditorRef()) - .InsertBRElementWithTransaction(pointToCreateNewBrNode); - if (NS_WARN_IF(!CanHandleEditAction())) { + brElement = InsertBRElementWithTransaction(pointToCreateNewBrNode); + if (NS_WARN_IF(Destroyed())) { return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); } if (NS_WARN_IF(!brElement)) { @@ -2197,16 +2196,14 @@ EditActionResult HTMLEditRules::SplitMailCites() { // delete any empty cites bool bEmptyCite = false; if (previousNodeOfSplitPoint) { - nsresult rv = HTMLEditorRef().IsEmptyNode(previousNodeOfSplitPoint, - &bEmptyCite, true, false); + nsresult rv = + IsEmptyNode(previousNodeOfSplitPoint, &bEmptyCite, true, false); if (NS_WARN_IF(NS_FAILED(rv))) { return EditActionIgnored(rv); } if (bEmptyCite) { - rv = MOZ_KnownLive(HTMLEditorRef()) - .DeleteNodeWithTransaction( - MOZ_KnownLive(*previousNodeOfSplitPoint)); - if (NS_WARN_IF(!CanHandleEditAction())) { + rv = DeleteNodeWithTransaction(MOZ_KnownLive(*previousNodeOfSplitPoint)); + if (NS_WARN_IF(Destroyed())) { return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); } if (NS_WARN_IF(NS_FAILED(rv))) { @@ -2216,14 +2213,13 @@ EditActionResult HTMLEditRules::SplitMailCites() { } if (citeNode) { - nsresult rv = - HTMLEditorRef().IsEmptyNode(citeNode, &bEmptyCite, true, false); + nsresult rv = IsEmptyNode(citeNode, &bEmptyCite, true, false); if (NS_WARN_IF(NS_FAILED(rv))) { return EditActionIgnored(rv); } if (bEmptyCite) { - rv = MOZ_KnownLive(HTMLEditorRef()).DeleteNodeWithTransaction(*citeNode); - if (NS_WARN_IF(!CanHandleEditAction())) { + rv = DeleteNodeWithTransaction(*citeNode); + if (NS_WARN_IF(Destroyed())) { return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED); } if (NS_WARN_IF(NS_FAILED(rv))) { diff --git a/editor/libeditor/HTMLEditRules.h b/editor/libeditor/HTMLEditRules.h index 0f54b392eeb6..3c3f9515358d 100644 --- a/editor/libeditor/HTMLEditRules.h +++ b/editor/libeditor/HTMLEditRules.h @@ -148,15 +148,6 @@ class HTMLEditRules : public TextEditRules { MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult InsertBRElement(const EditorDOMPoint& aInsertToBreak); - /** - * SplitMailCites() splits mail-cite elements at start of Selection if - * Selection starts from inside a mail-cite element. Of course, if it's - * necessary, this inserts
node to new left nodes or existing right - * nodes. - */ - MOZ_CAN_RUN_SCRIPT - MOZ_MUST_USE EditActionResult SplitMailCites(); - /** * Called before deleting selected contents. This method actually removes * selected contents. diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index 05e24fb5904f..7a308c25c18e 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -42,6 +42,7 @@ class nsRange; namespace mozilla { class AutoSelectionSetterAfterTableEdit; class AutoSetTemporaryAncestorLimiter; +class EditActionResult; class EmptyEditableFunctor; class ResizerSelectionListener; enum class EditSubAction : int32_t; @@ -1176,6 +1177,16 @@ class HTMLEditor final : public TextEditor, */ Element* GetMostAncestorMailCiteElement(nsINode& aNode) const; + /** + * SplitMailCiteElements() splits mail-cite elements at start of Selection if + * Selection starts from inside a mail-cite element. Of course, if it's + * necessary, this inserts
node to new left nodes or existing right + * nodes. + * XXX This modifies Selection, but should return SplitNodeResult() instead. + */ + MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult + SplitMailCiteElements(const EditorDOMPoint& aPointToSplit); + protected: // Called by helper classes. virtual void OnStartToHandleTopLevelEditSubAction( EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;