Bug 1762115 - part 5: Make SplitNodeResult store and suggest new caret position r=m_kato

This patch allows methods which split some nodes to return new candidate
caret position and makes callers of them consider whether applying it to
the selection immediately or not.

Differential Revision: https://phabricator.services.mozilla.com/D144648
This commit is contained in:
Masayuki Nakano
2022-05-03 00:06:16 +00:00
parent 8312d58153
commit adea39d1ad
10 changed files with 631 additions and 280 deletions

View File

@@ -726,14 +726,14 @@ nsresult HTMLEditor::HTMLWithContextInserter::Run(
!!HTMLEditor::GetLinkElement(pointToInsert.GetContainer());
if (pointToInsert.IsInTextNode()) {
SplitNodeResult splitNodeResult =
const SplitNodeResult splitNodeResult =
MOZ_KnownLive(mHTMLEditor)
.SplitNodeDeepWithTransaction(
MOZ_KnownLive(*pointToInsert.GetContainerAsContent()),
pointToInsert, SplitAtEdges::eAllowToCreateEmptyContainer);
if (MOZ_UNLIKELY(splitNodeResult.Failed())) {
if (splitNodeResult.isErr()) {
NS_WARNING("HTMLEditor::SplitNodeDeepWithTransaction() failed");
return splitNodeResult.Rv();
return splitNodeResult.unwrapErr();
}
pointToInsert = splitNodeResult.AtSplitPoint<EditorDOMPoint>();
if (MOZ_UNLIKELY(!pointToInsert.IsSet())) {
@@ -742,6 +742,9 @@ nsresult HTMLEditor::HTMLWithContextInserter::Run(
"point");
return NS_ERROR_FAILURE;
}
// When adding caret suggestion to SplitNodeResult, here didn't change
// selection so that just ignore it.
splitNodeResult.IgnoreCaretPointSuggestion();
}
{ // Block only for AutoHTMLFragmentBoundariesFixer to hide it from the
@@ -1050,16 +1053,21 @@ nsresult HTMLEditor::HTMLWithContextInserter::MoveCaretOutsideOfLink(
// above just placed selection inside that. So we need to split it instead.
// XXX Sounds like that it's not really expensive comparing with the reason
// to use SplitNodeDeepWithTransaction() here.
SplitNodeResult splitLinkResult =
const SplitNodeResult splitLinkResult =
MOZ_KnownLive(mHTMLEditor)
.SplitNodeDeepWithTransaction(
aLinkElement, aPointToPutCaret,
SplitAtEdges::eDoNotCreateEmptyContainer);
if (MOZ_UNLIKELY(NS_WARN_IF(splitLinkResult.EditorDestroyed()))) {
if (splitLinkResult.EditorDestroyed()) {
NS_WARNING(
"HTMLEditor::SplitNodeDeepWithTransaction() caused destroying the "
"editor");
return NS_ERROR_EDITOR_DESTROYED;
}
// Will update selection bellow if it's necessary.
splitLinkResult.IgnoreCaretPointSuggestion();
NS_WARNING_ASSERTION(
splitLinkResult.Succeeded(),
splitLinkResult.isOk(),
"HTMLEditor::SplitNodeDeepWithTransaction() failed, but ignored");
if (nsIContent* previousContentOfSplitPoint =
splitLinkResult.GetPreviousContent()) {