Bug 1770877 - part 5: Make HTMLEditor::MoveNodesIntoNewBlockquoteElement stop touching Selection directly r=m_kato

Similar to the previous patches, nobody uses the result, but it should return
the suggestion.

Differential Revision: https://phabricator.services.mozilla.com/D149069
This commit is contained in:
Masayuki Nakano 2022-06-16 08:16:43 +00:00
parent ad9533d7bc
commit 8f49ba6026
2 changed files with 57 additions and 56 deletions

View File

@ -3965,11 +3965,18 @@ nsresult HTMLEditor::FormatBlockContainerWithTransaction(nsAtom& blockType) {
// whatever is approriate. Woohoo! Note: blockquote is handled a little
// differently.
if (&blockType == nsGkAtoms::blockquote) {
nsresult rv = MoveNodesIntoNewBlockquoteElement(arrayOfContents);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"HTMLEditor::MoveNodesIntoNewBlockquoteElement() failed");
return rv;
Result<EditorDOMPoint, nsresult> wrapContentsInBlockquoteElementsResult =
WrapContentsInBlockquoteElementsWithTransaction(arrayOfContents,
*editingHost);
if (wrapContentsInBlockquoteElementsResult.isErr()) {
NS_WARNING(
"HTMLEditor::WrapContentsInBlockquoteElementsWithTransaction() "
"failed");
return wrapContentsInBlockquoteElementsResult.unwrapErr();
}
// Selection will be restored by `restoreSelectionLater`. Therefore, we
// should ignore the suggested caret point.
return NS_OK;
}
if (&blockType == nsGkAtoms::normal || &blockType == nsGkAtoms::_empty) {
Result<EditorDOMPoint, nsresult> removeBlockContainerElementsResult =
@ -8238,15 +8245,12 @@ HTMLEditor::HandleInsertParagraphInListItemElement(
return forwardScanFromStartOfListItemResult.Point<EditorDOMPoint>();
}
nsresult HTMLEditor::MoveNodesIntoNewBlockquoteElement(
nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents) {
Result<EditorDOMPoint, nsresult>
HTMLEditor::WrapContentsInBlockquoteElementsWithTransaction(
const nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents,
const Element& aEditingHost) {
MOZ_ASSERT(IsTopLevelEditSubActionDataAvailable());
RefPtr<Element> editingHost = ComputeEditingHost();
if (MOZ_UNLIKELY(NS_WARN_IF(!editingHost))) {
return NS_ERROR_FAILURE;
}
// The idea here is to put the nodes into a minimal number of blockquotes.
// When the user blockquotes something, they expect one blockquote. That
// may not be possible (for instance, if they have two table cells selected,
@ -8264,10 +8268,17 @@ nsresult HTMLEditor::MoveNodesIntoNewBlockquoteElement(
// Recursion time
AutoTArray<OwningNonNull<nsIContent>, 24> childContents;
HTMLEditor::GetChildNodesOf(*content, childContents);
nsresult rv = MoveNodesIntoNewBlockquoteElement(childContents);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::MoveNodesIntoNewBlockquoteElement() failed");
return rv;
Result<EditorDOMPoint, nsresult> wrapChildrenInAnotherBlockquoteResult =
WrapContentsInBlockquoteElementsWithTransaction(childContents,
aEditingHost);
if (MOZ_UNLIKELY(wrapChildrenInAnotherBlockquoteResult.isErr())) {
NS_WARNING(
"HTMLEditor::WrapContentsInBlockquoteElementsWithTransaction() "
"failed");
return wrapChildrenInAnotherBlockquoteResult;
}
if (wrapChildrenInAnotherBlockquoteResult.inspect().IsSet()) {
pointToPutCaret = wrapChildrenInAnotherBlockquoteResult.unwrap();
}
}
@ -8288,20 +8299,15 @@ nsresult HTMLEditor::MoveNodesIntoNewBlockquoteElement(
CreateElementResult createNewBlockQuoteElementResult =
InsertElementWithSplittingAncestorsWithTransaction(
*nsGkAtoms::blockquote, EditorDOMPoint(content),
BRElementNextToSplitPoint::Keep, *editingHost);
BRElementNextToSplitPoint::Keep, aEditingHost);
if (createNewBlockQuoteElementResult.isErr()) {
NS_WARNING(
"HTMLEditor::InsertElementWithSplittingAncestorsWithTransaction("
"nsGkAtoms::blockquote) failed");
return createNewBlockQuoteElementResult.unwrapErr();
}
nsresult rv = createNewBlockQuoteElementResult.SuggestCaretPointTo(
*this, {SuggestCaret::OnlyIfHasSuggestion,
SuggestCaret::OnlyIfTransactionsAllowedToDoIt});
if (NS_FAILED(rv)) {
NS_WARNING("CreateElementResult::SuggestCaretPointTo() failed");
return rv;
return Err(createNewBlockQuoteElementResult.unwrapErr());
}
createNewBlockQuoteElementResult.MoveCaretPointTo(
pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion});
RefPtr<Element> newBlockQuoteElement =
createNewBlockQuoteElementResult.UnwrapNewNode();
MOZ_ASSERT(newBlockQuoteElement);
@ -8312,25 +8318,16 @@ nsresult HTMLEditor::MoveNodesIntoNewBlockquoteElement(
}
// MOZ_KnownLive because 'aArrayOfContents' is guaranteed to/ keep it alive.
const MoveNodeResult moveNodeResult =
MoveNodeResult moveNodeResult =
MoveNodeToEndWithTransaction(MOZ_KnownLive(content), *curBlock);
if (moveNodeResult.isErr()) {
NS_WARNING("HTMLEditor::MoveNodeToEndWithTransaction() failed");
return moveNodeResult.unwrapErr();
return Err(moveNodeResult.unwrapErr());
}
nsresult rv = moveNodeResult.SuggestCaretPointTo(
*this, {SuggestCaret::OnlyIfHasSuggestion,
SuggestCaret::OnlyIfTransactionsAllowedToDoIt,
SuggestCaret::AndIgnoreTrivialError});
if (NS_FAILED(rv)) {
NS_WARNING("MoveNodeResult::SuggestCaretPointTo() failed");
return rv;
}
NS_WARNING_ASSERTION(
rv != NS_SUCCESS_EDITOR_BUT_IGNORED_TRIVIAL_ERROR,
"MoveNodeResult::SuggestCaretPointTo() failed, but ignored");
moveNodeResult.MoveCaretPointTo(pointToPutCaret,
{SuggestCaret::OnlyIfHasSuggestion});
}
return NS_OK;
return pointToPutCaret;
}
Result<EditorDOMPoint, nsresult>

View File

@ -1578,17 +1578,22 @@ class HTMLEditor final : public EditorBase,
nsIContent& aEndOfRange);
/**
* MoveNodesIntoNewBlockquoteElement() inserts at least one <blockquote>
* element and moves nodes in aArrayOfContents into new <blockquote>
* elements.
* If aArrayOfContents includes a table related element except <table>,
* this calls itself recursively to insert <blockquote> into the cell.
* WrapContentsInBlockquoteElementsWithTransaction() inserts at least one
* <blockquote> element and moves nodes in aArrayOfContents into new
* <blockquote> elements. If aArrayOfContents includes a table related element
* except <table>, this calls itself recursively to insert <blockquote> into
* the cell.
*
* @param aArrayOfContents Nodes which will be moved into created
* <blockquote> elements.
* @param aEditingHost The editing host.
* @return A suggest of caret position if succeeded. It
* may be unset if there is no suggestion.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult MoveNodesIntoNewBlockquoteElement(
nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents);
[[nodiscard]] MOZ_CAN_RUN_SCRIPT Result<EditorDOMPoint, nsresult>
WrapContentsInBlockquoteElementsWithTransaction(
const nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents,
const Element& aEditingHost);
/**
* RemoveBlockContainerElementsWithTransaction() removes all format blocks,
@ -1635,16 +1640,15 @@ class HTMLEditor final : public EditorBase,
* NOTE: This creates AutoSelectionRestorer. Therefore, even when this
* return NS_OK, editor may have been destroyed.
*
* @param aBlockType New block tag name.
* If nsGkAtoms::normal or nsGkAtoms::_empty,
* RemoveBlockContainerElementsWithTransaction()
* will be called.
* If nsGkAtoms::blockquote,
* MoveNodesIntoNewBlockquoteElement() will be
* called.
* Otherwise,
* CreateOrChangeBlockContainerElement() will be
* called.
* @param aBlockType New block tag name.
* If nsGkAtoms::normal or nsGkAtoms::_empty,
* RemoveBlockContainerElementsWithTransaction() will be
* called.
* If nsGkAtoms::blockquote,
* WrapContentsInBlockquoteElementsWithTransaction() will
* be called.
* Otherwise, CreateOrChangeBlockContainerElement() will be
* called.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult
FormatBlockContainerWithTransaction(nsAtom& aBlockType);