From 57b3cd700f8be1d2d43a770f49baa3c3467a956a Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Tue, 3 May 2022 01:03:05 +0000 Subject: [PATCH] Bug 1762115 - part 10: Make `HTMLEditor::InsertBRElement` stop touching `Selection` directly r=m_kato Differential Revision: https://phabricator.services.mozilla.com/D144653 --- editor/libeditor/EditorDOMPoint.h | 2 +- editor/libeditor/HTMLAbsPositionEditor.cpp | 16 +- editor/libeditor/HTMLEditSubActionHandler.cpp | 287 ++++++++++++------ editor/libeditor/HTMLEditor.cpp | 151 +++++---- editor/libeditor/HTMLEditor.h | 22 +- editor/libeditor/HTMLEditorDeleteHandler.cpp | 36 ++- editor/libeditor/WSRunObject.cpp | 48 ++- 7 files changed, 344 insertions(+), 218 deletions(-) diff --git a/editor/libeditor/EditorDOMPoint.h b/editor/libeditor/EditorDOMPoint.h index f76054850981..e1cd969f2f2c 100644 --- a/editor/libeditor/EditorDOMPoint.h +++ b/editor/libeditor/EditorDOMPoint.h @@ -637,7 +637,7 @@ class EditorDOMPointBase final { const StrongPtr& aContainer, InterlinePosition aInterlinePosition = InterlinePosition::Undefined) { MOZ_ASSERT(aContainer.get()); - return After(*aContainer.get()); + return After(*aContainer.get(), aInterlinePosition); } template MOZ_NEVER_INLINE_DEBUG static SelfType After( diff --git a/editor/libeditor/HTMLAbsPositionEditor.cpp b/editor/libeditor/HTMLAbsPositionEditor.cpp index 70fa153ee569..00e6fc72d843 100644 --- a/editor/libeditor/HTMLAbsPositionEditor.cpp +++ b/editor/libeditor/HTMLAbsPositionEditor.cpp @@ -692,14 +692,20 @@ nsresult HTMLEditor::SetPositionToAbsolute(Element& aElement) { if (parentNode->GetChildCount() != 1) { return NS_OK; } - Result, nsresult> resultOfInsertingBRElement = + CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, EditorDOMPoint(parentNode, 0u)); - if (resultOfInsertingBRElement.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); - return NS_OK; + // XXX Is this intentional selection change? + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + *this, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt}); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "CreateElementResult::SuggestCaretPointTo() failed"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); + return rv; } nsresult HTMLEditor::SetPositionToStatic(Element& aElement) { diff --git a/editor/libeditor/HTMLEditSubActionHandler.cpp b/editor/libeditor/HTMLEditSubActionHandler.cpp index e5efb2542efa..e0bc402304f6 100644 --- a/editor/libeditor/HTMLEditSubActionHandler.cpp +++ b/editor/libeditor/HTMLEditSubActionHandler.cpp @@ -1229,18 +1229,21 @@ EditActionResult HTMLEditor::HandleInsertText( // is it a return? if (subStr.Equals(newlineStr)) { - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, currentPoint, - nsIEditor::eNone); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = + InsertBRElement(WithTransaction::Yes, currentPoint); + if (insertBRElementResult.isErr()) { NS_WARNING( - "HTMLEditor::InsertBRElement(WithTransaction::Yes, eNone) " - "failed"); - return EditActionHandled(resultOfInsertingBRElement.unwrapErr()); + "HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); + return EditActionHandled(insertBRElementResult.unwrapErr()); } + // We don't want to update selection here because we've blocked + // InsertNodeTransaction updating selection with + // dontChangeMySelection. + insertBRElementResult.IgnoreCaretPointSuggestion(); + MOZ_ASSERT(!AllowsTransactionsToChangeSelection()); + pos++; - RefPtr brElement( - resultOfInsertingBRElement.unwrap().forget()); + RefPtr brElement = insertBRElementResult.UnwrapNewNode(); if (brElement->GetNextSibling()) { pointToInsert.Set(brElement->GetNextSibling()); } else { @@ -1447,16 +1450,17 @@ nsresult HTMLEditor::InsertLineBreakAsSubAction() { if (GetDefaultParagraphSeparator() == ParagraphSeparator::br || !HTMLEditUtils::ShouldInsertLinefeedCharacter(atStartOfSelection, *editingHost)) { - // InsertBRElement() will set selection after the new
element. - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, atStartOfSelection, - nsIEditor::eNext); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, atStartOfSelection, nsIEditor::eNext); + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); - return NS_OK; + nsresult rv = insertBRElementResult.SuggestCaretPointTo(*this, {}); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "CreateElementResult::SuggestCaretPointTo() failed"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); + return rv; } nsresult rv = EnsureNoPaddingBRElementForEmptyEditor(); @@ -1798,13 +1802,25 @@ EditActionResult HTMLEditor::InsertParagraphSeparatorAsSubAction() { AutoEditorDOMPointChildInvalidator lockOffset(atStartOfSelection); EditorDOMPoint endOfBlockParent; endOfBlockParent.SetToEndOf(editableBlockElement); - Result, nsresult> resultOfInsertingBRElement = + const CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, endOfBlockParent); - if (resultOfInsertingBRElement.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return EditActionIgnored(resultOfInsertingBRElement.unwrapErr()); + return EditActionIgnored(insertBRElementResult.unwrapErr()); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + // XXX Is this intentional selection change? + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + *this, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return EditActionHandled(rv); + } + NS_WARNING_ASSERTION( + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); } RefPtr maybeNonEditableListItem = @@ -1923,14 +1939,17 @@ CreateElementResult HTMLEditor::HandleInsertBRElement( // First, insert a
element. RefPtr brElement; if (IsInPlaintextMode()) { - Result, nsresult> resultOfInsertingBRElement = + CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, aPointToBreak); - if (MOZ_UNLIKELY(resultOfInsertingBRElement.isErr())) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return CreateElementResult(resultOfInsertingBRElement.unwrapErr()); + return CreateElementResult(insertBRElementResult.unwrapErr()); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); - brElement = resultOfInsertingBRElement.unwrap().forget(); + // We'll return with suggesting new caret position and nobody refers + // selection after here. So we don't need to update selection here. + insertBRElementResult.IgnoreCaretPointSuggestion(); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); + brElement = insertBRElementResult.UnwrapNewNode(); } else { EditorDOMPoint pointToBreak(aPointToBreak); WSRunScanner wsRunScanner(&aEditingHost, pointToBreak); @@ -2140,15 +2159,17 @@ nsresult HTMLEditor::HandleInsertLinefeed(const EditorDOMPoint& aPointToBreak, &pointToInsert); AutoTrackDOMPoint trackingNewCaretPosition(RangeUpdaterRef(), &newCaretPosition); - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, newCaretPosition, - nsIEditor::ePrevious); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = + InsertBRElement(WithTransaction::Yes, newCaretPosition); + if (insertBRElementResult.isErr()) { NS_WARNING( "HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + // We're tracking next caret position with newCaretPosition. Therefore, + // we don't need to update selection here. + insertBRElementResult.IgnoreCaretPointSuggestion(); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); } caretAfterInsert = newCaretPosition.To(); } @@ -2277,29 +2298,34 @@ HTMLEditor::HandleInsertParagraphInMailCiteElement( leftCiteElement->GetPrimaryFrame()->IsBlockFrameOrSubclass()) { nsIContent* lastChild = leftCiteElement->GetLastChild(); if (lastChild && !lastChild->IsHTMLElement(nsGkAtoms::br)) { - // We ignore the result here. - Result, nsresult> resultOfInsertingInvisibleBRElement = + const CreateElementResult insertInvisibleBRElementResult = InsertBRElement(WithTransaction::Yes, EditorDOMPoint::AtEndOf(*leftCiteElement)); - if (MOZ_UNLIKELY(resultOfInsertingInvisibleBRElement.isErr())) { + if (insertInvisibleBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return Err(resultOfInsertingInvisibleBRElement.unwrapErr()); + return Err(insertInvisibleBRElementResult.unwrapErr()); } - MOZ_ASSERT(resultOfInsertingInvisibleBRElement.inspect()); + // We don't need to update selection here because we'll do another + // InsertBRElement call soon. + insertInvisibleBRElementResult.IgnoreCaretPointSuggestion(); + MOZ_ASSERT(insertInvisibleBRElementResult.GetNewNode()); } } // In most cases,
should be inserted after current cite. However, if // left cite hasn't been created because the split point was start of the // cite node,
should be inserted before the current cite. - Result, nsresult> resultOfInsertingBRElement = + CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, splitCiteElementResult.AtSplitPoint()); - if (MOZ_UNLIKELY(resultOfInsertingBRElement.isErr())) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return Err(resultOfInsertingBRElement.unwrapErr()); + return Err(insertBRElementResult.unwrapErr()); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + // We'll return with suggesting caret position. Therefore, we don't need + // to update selection here. + insertBRElementResult.IgnoreCaretPointSuggestion(); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); // if aMailCiteElement wasn't a block, we might also want another break before // it. We need to examine the content both before the br we just added and @@ -2309,7 +2335,7 @@ HTMLEditor::HandleInsertParagraphInMailCiteElement( if (HTMLEditUtils::IsInlineElement(aMailCiteElement)) { nsresult rvOfInsertingBRElement = [&]() MOZ_CAN_RUN_SCRIPT { EditorDOMPoint pointToCreateNewBRElement( - resultOfInsertingBRElement.inspect()); + insertBRElementResult.GetNewNode()); // XXX Cannot we replace this complicated check with just a call of // HTMLEditUtils::IsVisibleBRElement with @@ -2347,17 +2373,18 @@ HTMLEditor::HandleInsertParagraphInMailCiteElement( .ReachedCurrentBlockBoundary()) { return NS_SUCCESS_DOM_NO_OPERATION; } - Result, nsresult> result = + CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, pointToCreateNewBRElement); - if (MOZ_UNLIKELY(result.isErr())) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return result.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(result.inspect()); + insertBRElementResult.IgnoreCaretPointSuggestion(); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); return NS_OK; }(); - if (MOZ_UNLIKELY(NS_FAILED(rvOfInsertingBRElement))) { + if (NS_FAILED(rvOfInsertingBRElement)) { NS_WARNING( "Failed to insert additional
element before the inline right " "mail-cite element"); @@ -2369,7 +2396,7 @@ HTMLEditor::HandleInsertParagraphInMailCiteElement( // MOZ_KnownLive(leftCiteElement) because it's grabbed by // splitCiteElementResult. nsresult rv = DeleteNodeWithTransaction(MOZ_KnownLive(*leftCiteElement)); - if (MOZ_UNLIKELY(NS_WARN_IF(Destroyed()))) { + if (NS_WARN_IF(Destroyed())) { return Err(NS_ERROR_EDITOR_DESTROYED); } if (NS_FAILED(rv)) { @@ -2382,7 +2409,7 @@ HTMLEditor::HandleInsertParagraphInMailCiteElement( // MOZ_KnownLive(rightCiteElement) because it's grabbed by // splitCiteElementResult. nsresult rv = DeleteNodeWithTransaction(MOZ_KnownLive(*rightCiteElement)); - if (MOZ_UNLIKELY(NS_WARN_IF(Destroyed()))) { + if (NS_WARN_IF(Destroyed())) { return Err(NS_ERROR_EDITOR_DESTROYED); } if (NS_FAILED(rv)) { @@ -2391,11 +2418,11 @@ HTMLEditor::HandleInsertParagraphInMailCiteElement( } } - if (MOZ_UNLIKELY(!resultOfInsertingBRElement.inspect()->GetParent())) { + if (MOZ_UNLIKELY(!insertBRElementResult.GetNewNode()->GetParent())) { NS_WARNING("Inserted
shouldn't become an orphan node"); return Err(NS_ERROR_EDITOR_UNEXPECTED_DOM_TREE); } - return EditorDOMPoint(resultOfInsertingBRElement.inspect()); + return EditorDOMPoint(insertBRElementResult.GetNewNode()); } HTMLEditor::CharPointData @@ -2906,16 +2933,17 @@ nsresult HTMLEditor::InsertBRElementIfHardLineIsEmptyAndEndsWithBlockBoundary( return NS_OK; } - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, aPointToInsert, - nsIEditor::ePrevious); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, aPointToInsert, nsIEditor::ePrevious); + if (insertBRElementResult.isErr()) { NS_WARNING( "HTMLEditor::InsertBRElement(WithTransaction::Yes, ePrevious) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); - return NS_OK; + nsresult rv = insertBRElementResult.SuggestCaretPointTo(*this, {}); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "CreateElementResult::SuggestCaretPointTo() failed"); + return rv; } EditActionResult HTMLEditor::MakeOrChangeListAndListItemAsSubAction( @@ -3756,20 +3784,19 @@ nsresult HTMLEditor::FormatBlockContainerWithTransaction(nsAtom& blockType) { return splitNodeResult.unwrapErr(); } // Put a
element at the split point - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, - splitNodeResult.AtSplitPoint()); - if (resultOfInsertingBRElement.isErr()) { + const CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, splitNodeResult.AtSplitPoint()); + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); // Don't restore the selection restoreSelectionLater.Abort(); // Put selection at the split point splitNodeResult.IgnoreCaretPointSuggestion(); nsresult rv = CollapseSelectionTo( - EditorRawDOMPoint(resultOfInsertingBRElement.inspect())); + EditorRawDOMPoint(insertBRElementResult.GetNewNode())); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EditorBase::CollapseSelectionTo() failed"); return rv; @@ -7284,17 +7311,19 @@ HTMLEditor::HandleInsertParagraphInHeadingElement( const auto withTransaction = aDivOrParagraphElement.IsInComposedDoc() ? WithTransaction::Yes : WithTransaction::No; - Result, nsresult> brElementOrError = + CreateElementResult insertBRElementResult = aHTMLEditor.InsertBRElement( withTransaction, EditorDOMPoint(&aDivOrParagraphElement, 0u)); - if (brElementOrError.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING( nsPrintfCString("HTMLEditor::InsertBRElement(%s) failed", ToString(withTransaction).c_str()) .get()); - return brElementOrError.unwrapErr(); + return insertBRElementResult.unwrapErr(); } + // We'll update selection after inserting the new paragraph. + insertBRElementResult.IgnoreCaretPointSuggestion(); return NS_OK; }); if (createNewParagraphElementResult.isErr()) { @@ -7517,20 +7546,31 @@ EditActionResult HTMLEditor::HandleInsertParagraphInParagraph( return EditActionResult(NS_OK); } - Result, nsresult> resultOfInsertingBRElement = + CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, pointToInsertBR); - if (resultOfInsertingBRElement.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return EditActionResult(resultOfInsertingBRElement.unwrapErr()); + return EditActionResult(insertBRElementResult.unwrapErr()); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + *this, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return EditActionResult(rv); + } + NS_WARNING_ASSERTION( + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); if (splitAfterNewBR) { // We split the parent after the br we've just inserted. - pointToSplitParentDivOrP.SetAfter(resultOfInsertingBRElement.inspect()); + pointToSplitParentDivOrP.SetAfter(insertBRElementResult.GetNewNode()); NS_WARNING_ASSERTION(pointToSplitParentDivOrP.IsSet(), "Failed to set after the new
"); } - brContent = resultOfInsertingBRElement.unwrap().forget(); + brContent = insertBRElementResult.UnwrapNewNode(); } EditActionResult result( SplitParagraph(aParentDivOrP, pointToSplitParentDivOrP, brContent)); @@ -7754,17 +7794,19 @@ HTMLEditor::HandleInsertParagraphInListItemElement( aDivOrParagraphElement.IsInComposedDoc() ? WithTransaction::Yes : WithTransaction::No; - Result, nsresult> brElementOrError = + CreateElementResult insertBRElementResult = aHTMLEditor.InsertBRElement( withTransaction, EditorDOMPoint(&aDivOrParagraphElement, 0u)); - if (brElementOrError.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING( nsPrintfCString("HTMLEditor::InsertBRElement(%s) failed", ToString(withTransaction).c_str()) .get()); - return brElementOrError.unwrapErr(); + return insertBRElementResult.unwrapErr(); } + // We'll update selection after inserting the paragraph. + insertBRElementResult.IgnoreCaretPointSuggestion(); return NS_OK; }); if (createNewParagraphElementResult.isErr()) { @@ -9337,6 +9379,7 @@ nsresult HTMLEditor::RemoveEmptyNodesIn(nsRange& aRange) { // Now delete the empty mailcites. This is a separate step because we want // to pull out any br's and preserve them. + EditorDOMPoint pointToPutCaret; for (OwningNonNull& emptyCite : arrayOfEmptyCites) { if (!HTMLEditUtils::IsEmptyNode( emptyCite, {EmptyCheckOption::TreatSingleBRElementAsVisible, @@ -9344,13 +9387,18 @@ nsresult HTMLEditor::RemoveEmptyNodesIn(nsRange& aRange) { EmptyCheckOption::TreatTableCellAsVisible})) { // We are deleting a cite that has just a `
`. We want to delete cite, // but preserve `
`. - Result, nsresult> resultOfInsertingBRElement = + CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, EditorDOMPoint(emptyCite)); - if (resultOfInsertingBRElement.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + // XXX Is this intentional selection change? + insertBRElementResult.MoveCaretPointTo( + pointToPutCaret, *this, + {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt}); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); } // MOZ_KnownLive because 'arrayOfEmptyCites' is guaranteed to keep it alive. rv = DeleteNodeWithTransaction(MOZ_KnownLive(emptyCite)); @@ -9362,6 +9410,18 @@ nsresult HTMLEditor::RemoveEmptyNodesIn(nsRange& aRange) { return rv; } } + // XXX Is this intentional selection change? + if (pointToPutCaret.IsSet()) { + nsresult rv = CollapseSelectionTo(pointToPutCaret); + if (MOZ_UNLIKELY(rv == NS_ERROR_EDITOR_DESTROYED)) { + NS_WARNING( + "EditorBase::CollapseSelectionTo() caused destroying the editor"); + return NS_ERROR_EDITOR_DESTROYED; + } + NS_WARNING_ASSERTION( + NS_SUCCEEDED(rv), + "EditorBase::CollapseSelectionTo() failed, but ignored"); + } return NS_OK; } @@ -9651,13 +9711,25 @@ nsresult HTMLEditor::InsertBRElementIfEmptyBlockElement(Element& aElement) { return NS_OK; } - Result, nsresult> resultOfInsertingBRElement = + CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, EditorDOMPoint(&aElement, 0u)); - if (resultOfInsertingBRElement.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + // XXX Is this intentional selection change? + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + *this, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return rv; + } + NS_WARNING_ASSERTION( + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); return NS_OK; } @@ -9821,14 +9893,25 @@ nsresult HTMLEditor::EnsureHardLineBeginsWithFirstChildOf( return NS_OK; } - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, - EditorDOMPoint(&aRemovingContainerElement, 0u)); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, EditorDOMPoint(&aRemovingContainerElement, 0u)); + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + // XXX Is this intentional selection change? + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + *this, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return rv; + } + NS_WARNING_ASSERTION( + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); return NS_OK; } @@ -9858,14 +9941,24 @@ nsresult HTMLEditor::EnsureHardLineEndsWithLastChildOf( return NS_OK; } - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, - EditorDOMPoint::AtEndOf(aRemovingContainerElement)); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, EditorDOMPoint::AtEndOf(aRemovingContainerElement)); + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + *this, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return rv; + } + NS_WARNING_ASSERTION( + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); return NS_OK; } diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index ad3fd942853b..c6cf69a61367 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -1894,16 +1894,18 @@ nsresult HTMLEditor::InsertElementAtSelectionAsAction( NS_WARNING_ASSERTION(advanced, "Failed to advance offset from inserted point"); // Collapse selection to the new `
` element node after creating it. - Result, nsresult> resultOfInsertingBRElement = + const CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, insertedPoint, ePrevious); - if (resultOfInsertingBRElement.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING( "HTMLEditor::InsertBRElement(WithTransaction::Yes, ePrevious) failed"); - return EditorBase::ToGenericNSResult( - resultOfInsertingBRElement.unwrapErr()); + return EditorBase::ToGenericNSResult(insertBRElementResult.unwrapErr()); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); - return NS_OK; + rv = insertBRElementResult.SuggestCaretPointTo(*this, {}); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "EditorBase::CollapseSelectionTo() failed"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); + return EditorBase::ToGenericNSResult(rv); } EditorDOMPoint HTMLEditor::InsertNodeIntoProperAncestorWithTransaction( @@ -3677,7 +3679,7 @@ Result HTMLEditor::PrepareToInsertBRElement( return atNextContent; } -Result, nsresult> HTMLEditor::InsertBRElement( +CreateElementResult HTMLEditor::InsertBRElement( WithTransaction aWithTransaction, const EditorDOMPoint& aPointToInsert, EDirection aSelect /* = eNone */) { MOZ_ASSERT(IsEditActionDataAvailable()); @@ -3689,7 +3691,7 @@ Result, nsresult> HTMLEditor::InsertBRElement( nsPrintfCString("HTMLEditor::PrepareToInsertBRElement(%s) failed", ToString(aWithTransaction).c_str()) .get()); - return Err(maybePointToInsert.unwrapErr()); + return CreateElementResult(maybePointToInsert.unwrapErr()); } MOZ_ASSERT(maybePointToInsert.inspect().IsSetAndValid()); @@ -3699,43 +3701,22 @@ Result, nsresult> HTMLEditor::InsertBRElement( NS_WARNING(nsPrintfCString("HTMLEditor::CreateAndInsertElement(%s) failed", ToString(aWithTransaction).c_str()) .get()); - return Err(createNewBRElementResult.unwrapErr()); + return CreateElementResult(createNewBRElementResult.unwrapErr()); } RefPtr newBRElement = createNewBRElementResult.UnwrapNewNode(); MOZ_ASSERT(newBRElement); + createNewBRElementResult.IgnoreCaretPointSuggestion(); switch (aSelect) { case eNext: { - createNewBRElementResult.IgnoreCaretPointSuggestion(); - // Collapse selection after the
node. - const auto afterBRElement = EditorRawDOMPoint::After( - *newBRElement, InterlinePosition::StartOfNextLine); - if (MOZ_UNLIKELY(!afterBRElement.IsSet())) { - NS_WARNING("Setting point to after
element failed"); - return Err(NS_ERROR_FAILURE); - } - nsresult rv = CollapseSelectionTo(afterBRElement); - if (NS_FAILED(rv)) { - NS_WARNING("EditorBase::CollapseSelectionTo() failed, but ignored"); - return Err(rv); - } - return newBRElement; + const auto pointToPutCaret = EditorDOMPoint::After( + *newBRElement, Selection::InterlinePosition::StartOfNextLine); + return CreateElementResult(std::move(newBRElement), pointToPutCaret); } case ePrevious: { - createNewBRElementResult.IgnoreCaretPointSuggestion(); - // Collapse selection at the
node. - EditorRawDOMPoint atBRElement(newBRElement, - InterlinePosition::StartOfNextLine); - if (MOZ_UNLIKELY(!atBRElement.IsSet())) { - NS_WARNING("Setting point to at
element failed"); - return Err(NS_ERROR_FAILURE); - } - nsresult rv = CollapseSelectionTo(atBRElement); - if (NS_FAILED(rv)) { - NS_WARNING("EditorBase::CollapseSelectionTo() failed, but ignored"); - return Err(rv); - } - return newBRElement; + const auto pointToPutCaret = EditorDOMPoint( + newBRElement, Selection::InterlinePosition::StartOfNextLine); + return CreateElementResult(std::move(newBRElement), pointToPutCaret); } default: NS_WARNING( @@ -3743,19 +3724,8 @@ Result, nsresult> HTMLEditor::InsertBRElement( "by itself"); [[fallthrough]]; case eNone: - // XXX Is this expecgted behavior? - nsresult rv = createNewBRElementResult.SuggestCaretPointTo( - *this, {SuggestCaret::OnlyIfHasSuggestion, - SuggestCaret::OnlyIfTransactionsAllowedToDoIt, - SuggestCaret::AndIgnoreTrivialError}); - if (NS_FAILED(rv)) { - NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); - return Err(rv); - } - NS_WARNING_ASSERTION( - rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, - "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); - return newBRElement; + return CreateElementResult(std::move(newBRElement), + createNewBRElementResult.UnwrapCaretPoint()); } } @@ -4281,6 +4251,7 @@ nsresult HTMLEditor::RemoveBlockContainerWithTransaction(Element& aElement) { // sibling and first child to determine if we need a leading br, and compare // following sibling and last child to determine if we need a trailing br. + EditorDOMPoint pointToPutCaret; if (nsCOMPtr child = HTMLEditUtils::GetFirstChild( aElement, {WalkTreeOption::IgnoreNonEditableNode})) { // The case of aNode not being empty. We need a br at start unless: @@ -4295,15 +4266,16 @@ nsresult HTMLEditor::RemoveBlockContainerWithTransaction(Element& aElement) { !previousSibling->IsHTMLElement(nsGkAtoms::br) && !HTMLEditUtils::IsBlockElement(*child)) { // Insert br node - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, - EditorDOMPoint(&aElement, 0u)); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, EditorDOMPoint(&aElement, 0u)); + if (insertBRElementResult.isErr()) { NS_WARNING( "HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + insertBRElementResult.MoveCaretPointTo( + pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion}); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); } } @@ -4320,15 +4292,16 @@ nsresult HTMLEditor::RemoveBlockContainerWithTransaction(Element& aElement) { aElement, {WalkTreeOption::IgnoreNonEditableNode})) { if (!HTMLEditUtils::IsBlockElement(*lastChild) && !lastChild->IsHTMLElement(nsGkAtoms::br)) { - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, - EditorDOMPoint::AtEndOf(aElement)); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, EditorDOMPoint::AtEndOf(aElement)); + if (insertBRElementResult.isErr()) { NS_WARNING( "HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + insertBRElementResult.MoveCaretPointTo( + pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion}); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); } } } @@ -4347,20 +4320,34 @@ nsresult HTMLEditor::RemoveBlockContainerWithTransaction(Element& aElement) { aElement, {WalkTreeOption::IgnoreNonEditableNode})) { if (!HTMLEditUtils::IsBlockElement(*nextSibling) && !nextSibling->IsHTMLElement(nsGkAtoms::br)) { - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, - EditorDOMPoint(&aElement, 0u)); - if (resultOfInsertingBRElement.isErr()) { + CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, EditorDOMPoint(&aElement, 0u)); + if (insertBRElementResult.isErr()) { NS_WARNING( "HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + insertBRElementResult.MoveCaretPointTo( + pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion}); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); } } } } + // XXX Is this intentional selection change? + if (pointToPutCaret.IsSet()) { + nsresult rv = CollapseSelectionTo(pointToPutCaret); + if (MOZ_UNLIKELY(rv == NS_ERROR_EDITOR_DESTROYED)) { + NS_WARNING( + "EditorBase::CollapseSelectionTo() caused destroying the editor"); + return NS_ERROR_EDITOR_DESTROYED; + } + NS_WARNING_ASSERTION( + NS_SUCCEEDED(rv), + "EditorBase::CollapseSelectionTo() failed, but ignored"); + } + // Now remove container nsresult rv = RemoveContainerWithTransaction(aElement); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), @@ -5859,19 +5846,27 @@ HTMLEditor::CopyLastEditableChildStylesWithTransaction(Element& aPreviousBlock, return RefPtr(); } - Result, nsresult> resultOfInsertingBRElement = - InsertBRElement(WithTransaction::Yes, - EditorDOMPoint(firstClonedElement, 0u)); - if (MOZ_UNLIKELY(NS_WARN_IF(Destroyed()))) { - return Err(NS_ERROR_EDITOR_DESTROYED); - } - if (MOZ_UNLIKELY(resultOfInsertingBRElement.isErr())) { + CreateElementResult insertBRElementResult = InsertBRElement( + WithTransaction::Yes, EditorDOMPoint(firstClonedElement, 0u)); + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return Err(resultOfInsertingBRElement.unwrapErr()); + return Err(insertBRElementResult.unwrapErr()); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + // XXX Is this intentional selection change? + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + *this, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return Err(rv); + } + NS_WARNING_ASSERTION( + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); return RefPtr( - HTMLBRElement::FromNode(resultOfInsertingBRElement.unwrap().get())); + HTMLBRElement::FromNode(insertBRElementResult.GetNewNode())); } nsresult HTMLEditor::GetElementOrigin(Element& aElement, int32_t& aX, diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index 25a450954bd1..374065c598af 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -729,9 +729,8 @@ class HTMLEditor final : public EditorBase, } /** - * InsertBRElement() creates a
element and inserts it - * before aPointToInsert. Then, tries to collapse selection at or after the - * new
node if aSelect is not eNone. + * InsertBRElement() creates a
element and inserts it before + * aPointToInsert. * * @param aWithTransaction Whether the inserting is new element is undoable * or not. WithTransaction::No is useful only when @@ -739,15 +738,16 @@ class HTMLEditor final : public EditorBase, * which has not been connected yet. * @param aPointToInsert The DOM point where should be
node inserted * before. - * @param aSelect If eNone, this won't change selection. - * If eNext, selection will be collapsed after - * the
element. - * If ePrevious, selection will be collapsed at - * the
element. - * @return The new
node. If failed to create new - *
node, returns error. + * @param aSelect If eNone, returns a point to put caret which is + * suggested by InsertNodeTransaction. + * If eNext, returns a point after the new
+ * element. + * If ePrevious, returns a point at the new
+ * element. + * @return The new
node and suggesting point to put + * caret which respects aSelect. */ - MOZ_CAN_RUN_SCRIPT Result, nsresult> InsertBRElement( + MOZ_CAN_RUN_SCRIPT CreateElementResult InsertBRElement( WithTransaction aWithTransaction, const EditorDOMPoint& aPointToInsert, EDirection aSelect = eNone); diff --git a/editor/libeditor/HTMLEditorDeleteHandler.cpp b/editor/libeditor/HTMLEditorDeleteHandler.cpp index f2f3241e21c2..3b71a9b25466 100644 --- a/editor/libeditor/HTMLEditorDeleteHandler.cpp +++ b/editor/libeditor/HTMLEditorDeleteHandler.cpp @@ -5140,15 +5140,16 @@ nsresult HTMLEditor::DeleteMostAncestorMailCiteElementIfEmpty( return NS_OK; } - Result, nsresult> resultOfInsertingBRElement = + CreateElementResult insertBRElementResult = InsertBRElement(WithTransaction::Yes, atEmptyMailCiteElement); - if (resultOfInsertingBRElement.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); + insertBRElementResult.IgnoreCaretPointSuggestion(); nsresult rv = CollapseSelectionTo( - EditorRawDOMPoint(resultOfInsertingBRElement.inspect())); + EditorRawDOMPoint(insertBRElementResult.GetNewNode())); if (MOZ_UNLIKELY(rv == NS_ERROR_EDITOR_DESTROYED)) { NS_WARNING( "EditorBase::CollapseSelectionTo() caused destroying the editor"); @@ -5306,15 +5307,22 @@ HTMLEditor::AutoDeleteRangesHandler::AutoEmptyBlockAncestorDeleter:: if (HTMLEditUtils::IsAnyListElement(atParentOfEmptyListItem.GetContainer())) { return RefPtr(); } - Result, nsresult> resultOfInsertingBRElement = - aHTMLEditor.InsertBRElement(WithTransaction::Yes, - atParentOfEmptyListItem); - NS_WARNING_ASSERTION( - resultOfInsertingBRElement.isOk(), - "HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - MOZ_ASSERT_IF(resultOfInsertingBRElement.isOk(), - resultOfInsertingBRElement.inspect()); - return resultOfInsertingBRElement; + CreateElementResult insertBRElementResult = aHTMLEditor.InsertBRElement( + WithTransaction::Yes, atParentOfEmptyListItem); + if (insertBRElementResult.isErr()) { + NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); + return Err(insertBRElementResult.unwrapErr()); + } + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + aHTMLEditor, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return Err(rv); + } + MOZ_ASSERT(insertBRElementResult.GetNewNode()); + return insertBRElementResult.UnwrapNewNode(); } Result HTMLEditor::AutoDeleteRangesHandler:: diff --git a/editor/libeditor/WSRunObject.cpp b/editor/libeditor/WSRunObject.cpp index aad151f58469..d1e4a3de6ad6 100644 --- a/editor/libeditor/WSRunObject.cpp +++ b/editor/libeditor/WSRunObject.cpp @@ -6,6 +6,7 @@ #include "WSRunObject.h" #include "EditorDOMPoint.h" +#include "EditorUtils.h" #include "HTMLEditor.h" #include "HTMLEditUtils.h" #include "SelectionState.h" @@ -871,15 +872,26 @@ Result, nsresult> WhiteSpaceVisibilityKeeper::InsertBRElement( } } - Result, nsresult> resultOfInsertingBRElement = - aHTMLEditor.InsertBRElement(HTMLEditor::WithTransaction::Yes, - pointToInsert, nsIEditor::eNone); + CreateElementResult insertBRElementResult = aHTMLEditor.InsertBRElement( + HTMLEditor::WithTransaction::Yes, pointToInsert); + if (insertBRElementResult.isErr()) { + NS_WARNING("HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); + return Err(insertBRElementResult.unwrapErr()); + } + // XXX Is this intentional selection change? + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + aHTMLEditor, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return Err(rv); + } NS_WARNING_ASSERTION( - resultOfInsertingBRElement.isOk(), - "HTMLEditor::InsertBRElement(WithTransaction::Yes, eNone) failed"); - MOZ_ASSERT_IF(resultOfInsertingBRElement.isOk(), - resultOfInsertingBRElement.inspect()); - return resultOfInsertingBRElement; + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); + return insertBRElementResult.UnwrapNewNode(); } // static @@ -3114,15 +3126,27 @@ nsresult WhiteSpaceVisibilityKeeper::NormalizeVisibleWhiteSpacesAt( // the beginning of soft wrapped lines, and lets the user see 2 spaces // when they type 2 spaces. - Result, nsresult> resultOfInsertingBRElement = + const CreateElementResult insertBRElementResult = aHTMLEditor.InsertBRElement(HTMLEditor::WithTransaction::Yes, atEndOfVisibleWhiteSpaces); - if (resultOfInsertingBRElement.isErr()) { + if (insertBRElementResult.isErr()) { NS_WARNING( "HTMLEditor::InsertBRElement(WithTransaction::Yes) failed"); - return resultOfInsertingBRElement.unwrapErr(); + return insertBRElementResult.unwrapErr(); } - MOZ_ASSERT(resultOfInsertingBRElement.inspect()); + // XXX Is this intentional selection change? + nsresult rv = insertBRElementResult.SuggestCaretPointTo( + aHTMLEditor, {SuggestCaret::OnlyIfHasSuggestion, + SuggestCaret::OnlyIfTransactionsAllowedToDoIt, + SuggestCaret::AndIgnoreTrivialError}); + if (NS_FAILED(rv)) { + NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed"); + return rv; + } + NS_WARNING_ASSERTION( + rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR, + "CreateElementResult::SuggestCaretPointTo() failed, but ignored"); + MOZ_ASSERT(insertBRElementResult.GetNewNode()); atPreviousCharOfEndOfVisibleWhiteSpaces = textFragmentData.GetPreviousEditableCharPoint(