Bug 1770877 - part 18: Make `HTMLEditor::SplitTextNodesAtRangeEnd` stop touching `Selection` directly r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D149082
This commit is contained in:
Masayuki Nakano 2022-06-20 08:54:47 +00:00
Родитель 2a83b583b1
Коммит 1674784441
2 изменённых файлов: 27 добавлений и 28 удалений

Просмотреть файл

@ -6477,13 +6477,21 @@ nsresult HTMLEditor::SplitInlinesAndCollectEditTargetNodes(
nsTArray<OwningNonNull<nsIContent>>& aOutArrayOfContents,
EditSubAction aEditSubAction,
CollectNonEditableNodes aCollectNonEditableNodes) {
nsresult rv = SplitTextNodesAtRangeEnd(aArrayOfRanges);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"HTMLEditor::SplitTextNodesAtRangeEnd() failed");
if (NS_FAILED(rv)) {
return rv;
const Result<EditorDOMPoint, nsresult> splitTextNodesResult =
SplitTextNodesAtRangeEnd(aArrayOfRanges);
if (splitTextNodesResult.isErr()) {
NS_WARNING("HTMLEditor::SplitTextNodesAtRangeEnd() failed");
return splitTextNodesResult.inspectErr();
}
rv = SplitParentInlineElementsAtRangeEdges(aArrayOfRanges);
if (AllowsTransactionsToChangeSelection() &&
splitTextNodesResult.inspect().IsSet()) {
nsresult rv = CollapseSelectionTo(splitTextNodesResult.inspect());
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::CollapseSelectionTo() failed");
return rv;
}
}
nsresult rv = SplitParentInlineElementsAtRangeEdges(aArrayOfRanges);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"HTMLEditor::SplitParentInlineElementsAtRangeEdges() failed");
@ -6514,14 +6522,13 @@ nsresult HTMLEditor::SplitInlinesAndCollectEditTargetNodes(
return NS_OK;
}
nsresult HTMLEditor::SplitTextNodesAtRangeEnd(
nsTArray<OwningNonNull<nsRange>>& aArrayOfRanges) {
Result<EditorDOMPoint, nsresult> HTMLEditor::SplitTextNodesAtRangeEnd(
const nsTArray<OwningNonNull<nsRange>>& aArrayOfRanges) {
// Split text nodes. This is necessary, since given ranges may end in text
// nodes in case where part of a pre-formatted elements needs to be moved.
EditorDOMPoint pointToPutCaret;
nsresult rv = NS_OK;
IgnoredErrorResult ignoredError;
for (OwningNonNull<nsRange>& range : aArrayOfRanges) {
for (const OwningNonNull<nsRange>& range : aArrayOfRanges) {
EditorDOMPoint atEnd(range->EndRef());
if (NS_WARN_IF(!atEnd.IsSet()) || !atEnd.IsInTextNode()) {
continue;
@ -6532,12 +6539,10 @@ nsresult HTMLEditor::SplitTextNodesAtRangeEnd(
SplitNodeResult splitAtEndResult = SplitNodeWithTransaction(atEnd);
if (splitAtEndResult.isErr()) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
rv = splitAtEndResult.unwrapErr();
break;
return Err(splitAtEndResult.unwrapErr());
}
splitAtEndResult.MoveCaretPointTo(
pointToPutCaret, *this,
{SuggestCaret::OnlyIfTransactionsAllowedToDoIt});
splitAtEndResult.MoveCaretPointTo(pointToPutCaret,
{SuggestCaret::OnlyIfHasSuggestion});
MOZ_ASSERT_IF(AllowsTransactionsToChangeSelection(),
pointToPutCaret.IsSet());
@ -6553,17 +6558,7 @@ nsresult HTMLEditor::SplitTextNodesAtRangeEnd(
}
}
if (!pointToPutCaret.IsSet()) {
return rv;
}
nsresult rvOfCollapseSelection = CollapseSelectionTo(pointToPutCaret);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvOfCollapseSelection),
"EditorBase::CollapseSelectionTo() failed");
if (MOZ_UNLIKELY(rv == NS_ERROR_EDITOR_DESTROYED ||
rvOfCollapseSelection == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
}
return MOZ_LIKELY(NS_SUCCEEDED(rv)) ? rvOfCollapseSelection : rv;
return pointToPutCaret;
}
nsresult HTMLEditor::SplitParentInlineElementsAtRangeEdges(

Просмотреть файл

@ -1222,9 +1222,13 @@ class HTMLEditor final : public EditorBase,
/**
* SplitTextNodesAtRangeEnd() splits text nodes if each range end is in
* middle of a text node.
*
* @return A suggest point to put caret if succeeded, but it may be
* unset.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult
SplitTextNodesAtRangeEnd(nsTArray<OwningNonNull<nsRange>>& aArrayOfRanges);
[[nodiscard]] MOZ_CAN_RUN_SCRIPT Result<EditorDOMPoint, nsresult>
SplitTextNodesAtRangeEnd(
const nsTArray<OwningNonNull<nsRange>>& aArrayOfRanges);
/**
* CollectEditTargetNodes() collects edit target nodes in aArrayOfRanges.