Bug 1739935 - Make HTMLEditor::SplitNodeWithTransaction() return SplitNodeResult r=m_kato

With this patch, callers of it can handle left and right node logically instead
of assuming that one is split and the other is new one.

Depends on D131748

Differential Revision: https://phabricator.services.mozilla.com/D131749
This commit is contained in:
Masayuki Nakano 2021-11-24 02:27:41 +00:00
parent 5697db1a15
commit ac63f96c0a
4 changed files with 69 additions and 67 deletions

View File

@ -3156,16 +3156,17 @@ EditActionResult HTMLEditor::ChangeSelectedHardLinesToList(
if (NS_WARN_IF(!atContent.GetContainerAsContent())) {
return EditActionResult(NS_ERROR_FAILURE);
}
Result<nsCOMPtr<nsIContent>, nsresult> newLeftNodeOrError =
SplitNodeResult splitListItemParentResult =
SplitNodeWithTransaction(atContent);
if (MOZ_UNLIKELY(newLeftNodeOrError.isErr())) {
if (MOZ_UNLIKELY(splitListItemParentResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return EditActionResult(newLeftNodeOrError.unwrapErr());
return EditActionResult(splitListItemParentResult.Rv());
}
MOZ_ASSERT(splitListItemParentResult.DidSplit());
Result<RefPtr<Element>, nsresult> maybeNewListElement =
CreateAndInsertElementWithTransaction(
aListElementTagName,
EditorDOMPoint(atContent.GetContainer()));
splitListItemParentResult.AtNextContent<EditorDOMPoint>());
if (maybeNewListElement.isErr()) {
NS_WARNING(
"HTMLEditor::CreateAndInsertElementWithTransaction() failed");
@ -6385,18 +6386,18 @@ nsresult HTMLEditor::SplitTextNodesAtRangeEnd(
if (!atEnd.IsStartOfContainer() && !atEnd.IsEndOfContainer()) {
// Split the text node.
Result<nsCOMPtr<nsIContent>, nsresult> newLeftNodeOrError =
SplitNodeWithTransaction(atEnd);
if (MOZ_UNLIKELY(newLeftNodeOrError.isErr())) {
SplitNodeResult splitAtEndResult = SplitNodeWithTransaction(atEnd);
if (MOZ_UNLIKELY(splitAtEndResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return newLeftNodeOrError.unwrapErr();
return splitAtEndResult.Rv();
}
// Correct the range.
// The new end parent becomes the parent node of the text.
EditorRawDOMPoint atContainerOfSplitNode(atEnd.GetContainer());
MOZ_ASSERT(!range->IsInSelection());
range->SetEnd(atContainerOfSplitNode, ignoredError);
range->SetEnd(splitAtEndResult.AtNextContent<EditorRawDOMPoint>()
.ToRawRangeBoundary(),
ignoredError);
NS_WARNING_ASSERTION(!ignoredError.Failed(),
"nsRange::SetEnd() failed, but ignored");
ignoredError.SuppressException();
@ -7063,13 +7064,14 @@ EditActionResult HTMLEditor::HandleInsertParagraphInParagraph(
if (pointToSplitOrError.inspect().IsSet()) {
pointToSplitParentDivOrP = pointToSplitOrError.unwrap();
}
Result<nsCOMPtr<nsIContent>, nsresult> newLeftTextNodeOrError =
SplitNodeResult splitParentDivOrPResult =
SplitNodeWithTransaction(pointToSplitParentDivOrP);
if (MOZ_UNLIKELY(newLeftTextNodeOrError.isErr())) {
if (MOZ_UNLIKELY(splitParentDivOrPResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return EditActionResult(newLeftTextNodeOrError.unwrapErr());
return EditActionResult(splitParentDivOrPResult.Rv());
}
pointToSplitParentDivOrP.SetToEndOf(newLeftTextNodeOrError.unwrap());
pointToSplitParentDivOrP.SetToEndOf(
splitParentDivOrPResult.GetPreviousContent());
}
// We need to put new <br> after the left node if given node was split
@ -7256,13 +7258,13 @@ nsresult HTMLEditor::HandleInsertParagraphInListItemElement(
if (!HTMLEditUtils::IsLastChild(aListItem,
{WalkTreeOption::IgnoreNonEditableNode})) {
// We need to split the list!
Result<nsCOMPtr<nsIContent>, nsresult> newListElementOrError =
SplitNodeResult splitListItemParentResult =
SplitNodeWithTransaction(EditorDOMPoint(&aListItem));
if (MOZ_UNLIKELY(newListElementOrError.isErr())) {
if (MOZ_UNLIKELY(splitListItemParentResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return newListElementOrError.unwrapErr();
return splitListItemParentResult.Rv();
}
leftListNode = newListElementOrError.unwrap();
leftListNode = splitListItemParentResult.GetPreviousContent();
}
// Are we in a sublist?
@ -8871,13 +8873,14 @@ nsresult HTMLEditor::LiftUpListItemElement(
return NS_ERROR_FAILURE;
}
MOZ_ASSERT(atListItemElement.IsSetAndValid());
Result<nsCOMPtr<nsIContent>, nsresult> leftListElementOrError =
SplitNodeResult splitListItemParentResult =
SplitNodeWithTransaction(atListItemElement);
if (MOZ_UNLIKELY(leftListElementOrError.isErr())) {
if (MOZ_UNLIKELY(splitListItemParentResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return leftListElementOrError.unwrapErr();
return splitListItemParentResult.Rv();
}
leftListElement = Element::FromNodeOrNull(leftListElementOrError.unwrap());
leftListElement =
Element::FromNodeOrNull(splitListItemParentResult.GetPreviousContent());
if (MOZ_UNLIKELY(!leftListElement)) {
NS_WARNING(
"HTMLEditor::SplitNodeWithTransaction() didn't return left list "

View File

@ -3578,11 +3578,11 @@ Result<EditorDOMPoint, nsresult> HTMLEditor::PrepareToInsertBRElement(
{
// Unfortunately, we need to split the text node at the offset.
Result<nsCOMPtr<nsIContent>, nsresult> newLeftTextNodeOrError =
SplitNodeResult splitTextNodeResult =
SplitNodeWithTransaction(aPointToInsert);
if (MOZ_UNLIKELY(newLeftTextNodeOrError.isErr())) {
if (MOZ_UNLIKELY(splitTextNodeResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return Err(newLeftTextNodeOrError.unwrapErr());
return Err(splitTextNodeResult.Rv());
}
}
@ -4271,18 +4271,18 @@ nsresult HTMLEditor::RemoveBlockContainerWithTransaction(Element& aElement) {
return rv;
}
Result<nsCOMPtr<nsIContent>, nsresult> HTMLEditor::SplitNodeWithTransaction(
SplitNodeResult HTMLEditor::SplitNodeWithTransaction(
const EditorDOMPoint& aStartOfRightNode) {
MOZ_ASSERT(IsEditActionDataAvailable());
if (MOZ_UNLIKELY(NS_WARN_IF(!aStartOfRightNode.IsInContentNode()))) {
return Err(NS_ERROR_INVALID_ARG);
return SplitNodeResult(NS_ERROR_INVALID_ARG);
}
MOZ_ASSERT(aStartOfRightNode.IsSetAndValid());
if (MOZ_UNLIKELY(NS_WARN_IF(!HTMLEditUtils::IsSplittableNode(
*aStartOfRightNode.ContainerAsContent())))) {
return Err(NS_ERROR_EDITOR_UNEXPECTED_DOM_TREE);
return SplitNodeResult(NS_ERROR_EDITOR_UNEXPECTED_DOM_TREE);
}
IgnoredErrorResult ignoredError;
@ -4290,7 +4290,7 @@ Result<nsCOMPtr<nsIContent>, nsresult> HTMLEditor::SplitNodeWithTransaction(
*this, EditSubAction::eSplitNode, nsIEditor::eNext, ignoredError);
if (MOZ_UNLIKELY(
NS_WARN_IF(ignoredError.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED)))) {
return Err(NS_ERROR_EDITOR_DESTROYED);
return SplitNodeResult(NS_ERROR_EDITOR_DESTROYED);
}
NS_WARNING_ASSERTION(
!ignoredError.Failed(),
@ -4325,14 +4325,18 @@ Result<nsCOMPtr<nsIContent>, nsresult> HTMLEditor::SplitNodeWithTransaction(
}
if (MOZ_UNLIKELY(NS_WARN_IF(Destroyed()))) {
return Err(NS_ERROR_EDITOR_DESTROYED);
return SplitNodeResult(NS_ERROR_EDITOR_DESTROYED);
}
if (MOZ_UNLIKELY(NS_FAILED(rv))) {
return Err(rv);
return SplitNodeResult(rv);
}
return newLeftContent;
MOZ_ASSERT(newLeftContent);
MOZ_ASSERT(aStartOfRightNode.GetContainerAsContent());
return SplitNodeResult(std::move(newLeftContent),
aStartOfRightNode.ContainerAsContent(),
SplitNodeDirection::LeftNodeIsNewOne);
}
SplitNodeResult HTMLEditor::SplitNodeDeepWithTransaction(
@ -4384,23 +4388,20 @@ SplitNodeResult HTMLEditor::SplitNodeDeepWithTransaction(
!atStartOfRightNode.GetContainerAsText()) ||
(!atStartOfRightNode.IsStartOfContainer() &&
!atStartOfRightNode.IsEndOfContainer())) {
Result<nsCOMPtr<nsIContent>, nsresult> newLeftContentOrError =
SplitNodeWithTransaction(atStartOfRightNode);
if (MOZ_UNLIKELY(newLeftContentOrError.isErr())) {
lastSplitNodeResult = SplitNodeWithTransaction(atStartOfRightNode);
if (MOZ_UNLIKELY(lastSplitNodeResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return SplitNodeResult(newLeftContentOrError.unwrapErr());
return lastSplitNodeResult;
}
lastSplitNodeResult =
SplitNodeResult(newLeftContentOrError.unwrap(), currentRightNode,
SplitNodeDirection::LeftNodeIsNewOne);
MOZ_ASSERT(lastSplitNodeResult.GetOriginalContent() == currentRightNode);
if (currentRightNode == &aMostAncestorToSplit) {
// Actually, we split aMostAncestorToSplit.
return lastSplitNodeResult;
}
// Then, try to split its parent before current node.
atStartOfRightNode.Set(currentRightNode);
atStartOfRightNode.Set(lastSplitNodeResult.GetNextContent());
}
// If the split point is end of the node and it is a text node or we're not
// allowed to create empty container node, try to split its parent after it.
@ -5066,20 +5067,20 @@ nsresult HTMLEditor::DeleteSelectionAndPrepareToCreateNode() {
return error.StealNSResult();
}
Result<nsCOMPtr<nsIContent>, nsresult> newLeftNodeOrError =
SplitNodeWithTransaction(atAnchor);
if (MOZ_UNLIKELY(newLeftNodeOrError.isErr())) {
SplitNodeResult splitAtAnchorResult = SplitNodeWithTransaction(atAnchor);
if (MOZ_UNLIKELY(splitAtAnchorResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return newLeftNodeOrError.unwrapErr();
return splitAtAnchorResult.Rv();
}
EditorRawDOMPoint atRightNode(atAnchor.GetContainer());
if (NS_WARN_IF(!atRightNode.IsSet())) {
const EditorRawDOMPoint& atRightContent =
splitAtAnchorResult.AtNextContent<EditorRawDOMPoint>();
if (MOZ_UNLIKELY(NS_WARN_IF(!atRightContent.IsSet()))) {
return NS_ERROR_FAILURE;
}
MOZ_ASSERT(atRightNode.IsSetAndValid());
MOZ_ASSERT(atRightContent.IsSetAndValid());
ErrorResult error;
SelectionRef().CollapseInLimiter(atRightNode, error);
SelectionRef().CollapseInLimiter(atRightContent.ToRawRangeBoundary(), error);
NS_WARNING_ASSERTION(!error.Failed(),
"Selection::CollapseInLimiter() failed");
return error.StealNSResult();

View File

@ -2120,7 +2120,7 @@ class HTMLEditor final : public EditorBase,
* next sibling. And the point will be start
* of the right node.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT Result<nsCOMPtr<nsIContent>, nsresult>
[[nodiscard]] MOZ_CAN_RUN_SCRIPT SplitNodeResult
SplitNodeWithTransaction(const EditorDOMPoint& aStartOfRightNode);
enum class SplitAtEdges {

View File

@ -466,24 +466,23 @@ nsresult HTMLEditor::SetInlinePropertyOnTextNode(
EditorDOMPoint atEnd(textNodeForTheRange, aEndOffset);
if (!atEnd.IsEndOfContainer()) {
// We need to split off back of text node
Result<nsCOMPtr<nsIContent>, nsresult> newLeftTextNodeOrError =
SplitNodeWithTransaction(atEnd);
if (MOZ_UNLIKELY(newLeftTextNodeOrError.isErr())) {
SplitNodeResult splitAtEndResult = SplitNodeWithTransaction(atEnd);
if (MOZ_UNLIKELY(splitAtEndResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return newLeftTextNodeOrError.unwrapErr();
return splitAtEndResult.Rv();
}
textNodeForTheRange = Text::FromNodeOrNull(newLeftTextNodeOrError.unwrap());
textNodeForTheRange =
Text::FromNodeOrNull(splitAtEndResult.GetPreviousContent());
}
// Split at the start of the range.
EditorDOMPoint atStart(textNodeForTheRange, aStartOffset);
if (!atStart.IsStartOfContainer()) {
// We need to split off front of text node
Result<nsCOMPtr<nsIContent>, nsresult> newLeftTextNodeOrError =
SplitNodeWithTransaction(atStart);
if (MOZ_UNLIKELY(newLeftTextNodeOrError.isErr())) {
SplitNodeResult splitAtStartResult = SplitNodeWithTransaction(atStart);
if (MOZ_UNLIKELY(splitAtStartResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return newLeftTextNodeOrError.unwrapErr();
return splitAtStartResult.Rv();
}
}
@ -2389,13 +2388,13 @@ nsresult HTMLEditor::RelativeFontChangeOnTextNode(FontSize aDir,
EditorDOMPoint atEnd(textNodeForTheRange, aEndOffset);
if (!atEnd.IsEndOfContainer()) {
// We need to split off back of text node
Result<nsCOMPtr<nsIContent>, nsresult> newLeftTextNodeOrError =
SplitNodeWithTransaction(atEnd);
if (MOZ_UNLIKELY(newLeftTextNodeOrError.isErr())) {
SplitNodeResult splitAtEndResult = SplitNodeWithTransaction(atEnd);
if (MOZ_UNLIKELY(splitAtEndResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return newLeftTextNodeOrError.unwrapErr();
return splitAtEndResult.Rv();
}
textNodeForTheRange = Text::FromNodeOrNull(newLeftTextNodeOrError.unwrap());
textNodeForTheRange =
Text::FromNodeOrNull(splitAtEndResult.GetPreviousContent());
MOZ_DIAGNOSTIC_ASSERT(textNodeForTheRange);
}
@ -2403,11 +2402,10 @@ nsresult HTMLEditor::RelativeFontChangeOnTextNode(FontSize aDir,
EditorDOMPoint atStart(textNodeForTheRange, aStartOffset);
if (!atStart.IsStartOfContainer()) {
// We need to split off front of text node
Result<nsCOMPtr<nsIContent>, nsresult> newLeftTextNodeOrError =
SplitNodeWithTransaction(atStart);
if (MOZ_UNLIKELY(newLeftTextNodeOrError.isErr())) {
SplitNodeResult splitAtStartResult = SplitNodeWithTransaction(atStart);
if (MOZ_UNLIKELY(splitAtStartResult.Failed())) {
NS_WARNING("HTMLEditor::SplitNodeWithTransaction() failed");
return newLeftTextNodeOrError.unwrapErr();
return splitAtStartResult.Rv();
}
}