Bug 1770877 - part 4: Make HTMLEditor::RemoveBlockContainerElements stop touching Selection directly r=m_kato

For aligning the style of similar methods, this makes it suggest caret position,
but nobody uses it.

Differential Revision: https://phabricator.services.mozilla.com/D149068
This commit is contained in:
Masayuki Nakano 2022-06-16 03:34:19 +00:00
parent 297c111411
commit 853b00c6ba
2 changed files with 56 additions and 71 deletions

View File

@ -3972,10 +3972,16 @@ nsresult HTMLEditor::FormatBlockContainerWithTransaction(nsAtom& blockType) {
return rv;
}
if (&blockType == nsGkAtoms::normal || &blockType == nsGkAtoms::_empty) {
nsresult rv = RemoveBlockContainerElements(arrayOfContents);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::RemoveBlockContainerElements() failed");
return rv;
Result<EditorDOMPoint, nsresult> removeBlockContainerElementsResult =
RemoveBlockContainerElementsWithTransaction(arrayOfContents);
if (MOZ_UNLIKELY(removeBlockContainerElementsResult.isErr())) {
NS_WARNING(
"HTMLEditor::RemoveBlockContainerElementsWithTransaction() failed");
removeBlockContainerElementsResult.unwrapErr();
}
// Selection will be restored by `restoreSelectionLater`. Therefore, we
// should ignore the suggested caret point.
return NS_OK;
}
Result<EditorDOMPoint, nsresult> wrapContentsInBlockElementResult =
CreateOrChangeBlockContainerElement(arrayOfContents, blockType,
@ -8327,8 +8333,9 @@ nsresult HTMLEditor::MoveNodesIntoNewBlockquoteElement(
return NS_OK;
}
nsresult HTMLEditor::RemoveBlockContainerElements(
nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents) {
Result<EditorDOMPoint, nsresult>
HTMLEditor::RemoveBlockContainerElementsWithTransaction(
const nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents) {
MOZ_ASSERT(IsEditActionDataAvailable());
// Intent of this routine is to be used for converting to/from headers,
@ -8336,6 +8343,7 @@ nsresult HTMLEditor::RemoveBlockContainerElements(
// inline things...
RefPtr<Element> blockElement;
nsCOMPtr<nsIContent> firstContent, lastContent;
EditorDOMPoint pointToPutCaret;
for (auto& content : aArrayOfContents) {
// If curNode is an <address>, <p>, <hn>, or <pre>, remove it.
if (HTMLEditUtils::IsFormatNode(content)) {
@ -8348,37 +8356,25 @@ nsresult HTMLEditor::RemoveBlockContainerElements(
NS_WARNING(
"HTMLEditor::RemoveBlockContainerElementWithTransactionBetween() "
"failed");
return unwrapBlockElementResult.unwrapErr();
}
nsresult rv = unwrapBlockElementResult.SuggestCaretPointTo(
*this, {SuggestCaret::OnlyIfHasSuggestion,
SuggestCaret::OnlyIfTransactionsAllowedToDoIt});
if (NS_FAILED(rv)) {
NS_WARNING(
"SplitRangeOffFromNodeResult::SuggestCaretPointTo() failed");
return rv;
return Err(unwrapBlockElementResult.unwrapErr());
}
unwrapBlockElementResult.MoveCaretPointTo(
pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion});
firstContent = lastContent = blockElement = nullptr;
}
if (!EditorUtils::IsEditableContent(content, EditorType::HTML)) {
continue;
}
// Remove current block
const Result<EditorDOMPoint, nsresult> unwrapFormatBlockResult =
Result<EditorDOMPoint, nsresult> unwrapFormatBlockResult =
RemoveBlockContainerWithTransaction(
MOZ_KnownLive(*content->AsElement()));
if (MOZ_UNLIKELY(unwrapFormatBlockResult.isErr())) {
NS_WARNING("HTMLEditor::RemoveBlockContainerWithTransaction() failed");
return unwrapFormatBlockResult.inspectErr();
return unwrapFormatBlockResult;
}
const EditorDOMPoint& pointToPutCaret = unwrapFormatBlockResult.inspect();
if (!AllowsTransactionsToChangeSelection() || !pointToPutCaret.IsSet()) {
continue;
}
nsresult rv = CollapseSelectionTo(pointToPutCaret);
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::CollapseSelectionTo() failed");
return rv;
if (unwrapFormatBlockResult.inspect().IsSet()) {
pointToPutCaret = unwrapFormatBlockResult.unwrap();
}
continue;
}
@ -8397,16 +8393,10 @@ nsresult HTMLEditor::RemoveBlockContainerElements(
NS_WARNING(
"HTMLEditor::RemoveBlockContainerElementWithTransactionBetween() "
"failed");
return unwrapBlockElementResult.unwrapErr();
}
nsresult rv = unwrapBlockElementResult.SuggestCaretPointTo(
*this, {SuggestCaret::OnlyIfHasSuggestion,
SuggestCaret::OnlyIfTransactionsAllowedToDoIt});
if (NS_FAILED(rv)) {
NS_WARNING(
"SplitRangeOffFromNodeResult::SuggestCaretPointTo() failed");
return rv;
return Err(unwrapBlockElementResult.unwrapErr());
}
unwrapBlockElementResult.MoveCaretPointTo(
pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion});
firstContent = lastContent = blockElement = nullptr;
}
if (!EditorUtils::IsEditableContent(content, EditorType::HTML)) {
@ -8415,10 +8405,15 @@ nsresult HTMLEditor::RemoveBlockContainerElements(
// Recursion time
AutoTArray<OwningNonNull<nsIContent>, 24> childContents;
HTMLEditor::GetChildNodesOf(*content, childContents);
nsresult rv = RemoveBlockContainerElements(childContents);
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::RemoveBlockContainerElements() failed");
return rv;
Result<EditorDOMPoint, nsresult> removeBlockContainerElementsResult =
RemoveBlockContainerElementsWithTransaction(childContents);
if (MOZ_UNLIKELY(removeBlockContainerElementsResult.isErr())) {
NS_WARNING(
"HTMLEditor::RemoveBlockContainerElementsWithTransaction() failed");
return removeBlockContainerElementsResult;
}
if (removeBlockContainerElementsResult.inspect().IsSet()) {
pointToPutCaret = removeBlockContainerElementsResult.unwrap();
}
continue;
}
@ -8441,16 +8436,10 @@ nsresult HTMLEditor::RemoveBlockContainerElements(
NS_WARNING(
"HTMLEditor::RemoveBlockContainerElementWithTransactionBetween() "
"failed");
return unwrapBlockElementResult.unwrapErr();
}
nsresult rv = unwrapBlockElementResult.SuggestCaretPointTo(
*this, {SuggestCaret::OnlyIfHasSuggestion,
SuggestCaret::OnlyIfTransactionsAllowedToDoIt});
if (NS_FAILED(rv)) {
NS_WARNING(
"SplitRangeOffFromNodeResult::SuggestCaretPointTo() failed");
return rv;
return Err(unwrapBlockElementResult.unwrapErr());
}
unwrapBlockElementResult.MoveCaretPointTo(
pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion});
firstContent = lastContent = blockElement = nullptr;
// Fall out and handle content
}
@ -8476,15 +8465,10 @@ nsresult HTMLEditor::RemoveBlockContainerElements(
NS_WARNING(
"HTMLEditor::RemoveBlockContainerElementWithTransactionBetween() "
"failed");
return unwrapBlockElementResult.unwrapErr();
}
nsresult rv = unwrapBlockElementResult.SuggestCaretPointTo(
*this, {SuggestCaret::OnlyIfHasSuggestion,
SuggestCaret::OnlyIfTransactionsAllowedToDoIt});
if (NS_FAILED(rv)) {
NS_WARNING("SplitRangeOffFromNodeResult::SuggestCaretPointTo() failed");
return rv;
return Err(unwrapBlockElementResult.unwrapErr());
}
unwrapBlockElementResult.MoveCaretPointTo(
pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion});
firstContent = lastContent = blockElement = nullptr;
continue;
}
@ -8498,18 +8482,13 @@ nsresult HTMLEditor::RemoveBlockContainerElements(
NS_WARNING(
"HTMLEditor::RemoveBlockContainerElementWithTransactionBetween() "
"failed");
return unwrapBlockElementResult.unwrapErr();
}
nsresult rv = unwrapBlockElementResult.SuggestCaretPointTo(
*this, {SuggestCaret::OnlyIfHasSuggestion,
SuggestCaret::OnlyIfTransactionsAllowedToDoIt});
if (NS_FAILED(rv)) {
NS_WARNING("SplitRangeOffFromNodeResult::SuggestCaretPointTo() failed");
return rv;
return Err(unwrapBlockElementResult.unwrapErr());
}
unwrapBlockElementResult.MoveCaretPointTo(
pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion});
firstContent = lastContent = blockElement = nullptr;
}
return NS_OK;
return pointToPutCaret;
}
Result<EditorDOMPoint, nsresult>

View File

@ -1591,15 +1591,19 @@ class HTMLEditor final : public EditorBase,
nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents);
/**
* RemoveBlockContainerElements() removes all format blocks, table related
* element, etc in aArrayOfContents from the DOM tree.
* If aArrayOfContents has a format node, it will be removed and its contents
* RemoveBlockContainerElementsWithTransaction() removes all format blocks,
* table related element, etc in aArrayOfContents from the DOM tree. If
* aArrayOfContents has a format node, it will be removed and its contents
* will be moved to where it was.
* If aArrayOfContents has a table related element, <li>, <blockquote> or
* <div>, it will be removed and its contents will be moved to where it was.
*
* @return A suggest point to put caret if succeeded, but it may be
* unset if there is no suggestion.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult RemoveBlockContainerElements(
nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents);
[[nodiscard]] MOZ_CAN_RUN_SCRIPT Result<EditorDOMPoint, nsresult>
RemoveBlockContainerElementsWithTransaction(
const nsTArray<OwningNonNull<nsIContent>>& aArrayOfContents);
/**
* CreateOrChangeBlockContainerElement() formats all nodes in aArrayOfContents
@ -1633,10 +1637,12 @@ class HTMLEditor final : public EditorBase,
*
* @param aBlockType New block tag name.
* If nsGkAtoms::normal or nsGkAtoms::_empty,
* RemoveBlockContainerElements() will be called.
* RemoveBlockContainerElementsWithTransaction()
* will be called.
* If nsGkAtoms::blockquote,
* MoveNodesIntoNewBlockquoteElement() will be
* called. Otherwise,
* called.
* Otherwise,
* CreateOrChangeBlockContainerElement() will be
* called.
*/