Bug 1792641 - Make `JoinNodesResult` treated as ok type of `Result` r=m_kato

Depends on D158332

Differential Revision: https://phabricator.services.mozilla.com/D158333
This commit is contained in:
Masayuki Nakano 2022-10-03 23:17:02 +00:00
Родитель 884f6c1501
Коммит 158095f12c
6 изменённых файлов: 54 добавлений и 69 удалений

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

@ -628,29 +628,17 @@ class MOZ_STACK_CLASS SplitNodeResult final {
*****************************************************************************/
class MOZ_STACK_CLASS JoinNodesResult final {
public:
bool Succeeded() const { return NS_SUCCEEDED(mRv); }
bool Failed() const { return NS_FAILED(mRv); }
nsresult Rv() const { return mRv; }
bool Handled() const { return Succeeded(); }
bool EditorDestroyed() const { return mRv == NS_ERROR_EDITOR_DESTROYED; }
MOZ_KNOWN_LIVE nsIContent* ExistingContent() const {
MOZ_ASSERT(Succeeded());
return mJoinedPoint.ContainerAs<nsIContent>();
}
template <typename EditorDOMPointType>
EditorDOMPointType AtExistingContent() const {
MOZ_ASSERT(Succeeded());
return EditorDOMPointType(mJoinedPoint.ContainerAs<nsIContent>());
}
MOZ_KNOWN_LIVE nsIContent* RemovedContent() const {
MOZ_ASSERT(Succeeded());
return mRemovedContent;
}
MOZ_KNOWN_LIVE nsIContent* RemovedContent() const { return mRemovedContent; }
template <typename EditorDOMPointType>
EditorDOMPointType AtRemovedContent() const {
MOZ_ASSERT(Succeeded());
if (mRemovedContent) {
return EditorDOMPointType(mRemovedContent);
}
@ -659,8 +647,7 @@ class MOZ_STACK_CLASS JoinNodesResult final {
template <typename EditorDOMPointType>
EditorDOMPointType AtJoinedPoint() const {
MOZ_ASSERT(Succeeded());
return mJoinedPoint;
return mJoinedPoint.To<EditorDOMPointType>();
}
JoinNodesResult() = delete;
@ -676,25 +663,18 @@ class MOZ_STACK_CLASS JoinNodesResult final {
*/
JoinNodesResult(const EditorDOMPoint& aJoinedPoint,
nsIContent& aRemovedContent, JoinNodesDirection aDirection)
: mJoinedPoint(aJoinedPoint),
mRemovedContent(&aRemovedContent),
mRv(NS_OK) {
: mJoinedPoint(aJoinedPoint), mRemovedContent(&aRemovedContent) {
MOZ_DIAGNOSTIC_ASSERT(aJoinedPoint.IsInContentNode());
}
/**
* This constructor shouldn't be used by anybody except methods which
* use this as error result when it fails.
*/
explicit JoinNodesResult(nsresult aRv) : mRv(aRv) {
MOZ_DIAGNOSTIC_ASSERT(NS_FAILED(mRv));
}
JoinNodesResult(const JoinNodesResult& aOther) = delete;
JoinNodesResult& operator=(const JoinNodesResult& aOther) = delete;
JoinNodesResult(JoinNodesResult&& aOther) = default;
JoinNodesResult& operator=(JoinNodesResult&& aOther) = default;
private:
EditorDOMPoint mJoinedPoint;
nsCOMPtr<nsIContent> mRemovedContent;
nsresult mRv;
MOZ_KNOWN_LIVE nsCOMPtr<nsIContent> mRemovedContent;
};
/*****************************************************************************

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

@ -8857,15 +8857,15 @@ nsresult HTMLEditor::JoinNearestEditableNodesWithTransaction(
// Separate join rules for differing blocks
if (HTMLEditUtils::IsAnyListElement(&aNodeLeft) || aNodeLeft.IsText()) {
// For lists, merge shallow (wouldn't want to combine list items)
JoinNodesResult joinNodesResult =
Result<JoinNodesResult, nsresult> joinNodesResult =
JoinNodesWithTransaction(aNodeLeft, aNodeRight);
if (MOZ_UNLIKELY(joinNodesResult.Failed())) {
if (MOZ_UNLIKELY(joinNodesResult.isErr())) {
NS_WARNING("HTMLEditor::JoinNodesWithTransaction failed");
return joinNodesResult.Rv();
return joinNodesResult.unwrapErr();
}
*aNewFirstChildOfRightNode =
joinNodesResult.AtJoinedPoint<EditorDOMPoint>();
return joinNodesResult.Rv();
joinNodesResult.inspect().AtJoinedPoint<EditorDOMPoint>();
return NS_OK;
}
// Remember the last left child, and first right child
@ -8884,11 +8884,11 @@ nsresult HTMLEditor::JoinNearestEditableNodesWithTransaction(
}
// For list items, divs, etc., merge smart
JoinNodesResult joinNodesResult =
Result<JoinNodesResult, nsresult> joinNodesResult =
JoinNodesWithTransaction(aNodeLeft, aNodeRight);
if (MOZ_UNLIKELY(joinNodesResult.Failed())) {
if (MOZ_UNLIKELY(joinNodesResult.isErr())) {
NS_WARNING("HTMLEditor::JoinNodesWithTransaction() failed");
return joinNodesResult.Rv();
return joinNodesResult.unwrapErr();
}
if ((lastEditableChildOfLeftContent->IsText() ||
@ -8904,7 +8904,8 @@ nsresult HTMLEditor::JoinNearestEditableNodesWithTransaction(
"HTMLEditor::JoinNearestEditableNodesWithTransaction() failed");
return rv;
}
*aNewFirstChildOfRightNode = joinNodesResult.AtJoinedPoint<EditorDOMPoint>();
*aNewFirstChildOfRightNode =
joinNodesResult.inspect().AtJoinedPoint<EditorDOMPoint>();
return NS_OK;
}

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

@ -4542,15 +4542,18 @@ nsresult HTMLEditor::CollapseAdjacentTextNodes(nsRange& aInRange) {
continue;
}
JoinNodesResult result = JoinNodesWithTransaction(
MOZ_KnownLive(*leftTextNode), MOZ_KnownLive(*rightTextNode));
if (MOZ_UNLIKELY(result.Failed())) {
Result<JoinNodesResult, nsresult> joinNodesResult =
JoinNodesWithTransaction(MOZ_KnownLive(*leftTextNode),
MOZ_KnownLive(*rightTextNode));
if (MOZ_UNLIKELY(joinNodesResult.isErr())) {
NS_WARNING("HTMLEditor::JoinNodesWithTransaction() failed");
return result.Rv();
return joinNodesResult.unwrapErr();
}
if (MOZ_LIKELY(result.RemovedContent() == leftTextNode)) {
if (MOZ_LIKELY(joinNodesResult.inspect().RemovedContent() ==
leftTextNode)) {
textNodes.RemoveElementAt(0u);
} else if (MOZ_LIKELY(result.RemovedContent() == rightTextNode)) {
} else if (MOZ_LIKELY(joinNodesResult.inspect().RemovedContent() ==
rightTextNode)) {
textNodes.RemoveElementAt(1u);
} else {
MOZ_ASSERT_UNREACHABLE(
@ -5145,7 +5148,7 @@ SplitNodeResult HTMLEditor::DoSplitNode(const EditorDOMPoint& aStartOfRightNode,
aDirection);
}
JoinNodesResult HTMLEditor::JoinNodesWithTransaction(
Result<JoinNodesResult, nsresult> HTMLEditor::JoinNodesWithTransaction(
nsIContent& aLeftContent, nsIContent& aRightContent) {
MOZ_ASSERT(IsEditActionDataAvailable());
MOZ_ASSERT(&aLeftContent != &aRightContent);
@ -5157,47 +5160,47 @@ JoinNodesResult HTMLEditor::JoinNodesWithTransaction(
AutoEditSubActionNotifier startToHandleEditSubAction(
*this, EditSubAction::eJoinNodes, nsIEditor::ePrevious, ignoredError);
if (NS_WARN_IF(ignoredError.ErrorCodeIs(NS_ERROR_EDITOR_DESTROYED))) {
return JoinNodesResult(ignoredError.StealNSResult());
return Err(ignoredError.StealNSResult());
}
NS_WARNING_ASSERTION(
!ignoredError.Failed(),
"HTMLEditor::OnStartToHandleTopLevelEditSubAction() failed, but ignored");
if (MOZ_UNLIKELY(NS_WARN_IF(!aRightContent.GetParentNode()))) {
return JoinNodesResult(NS_ERROR_FAILURE);
if (NS_WARN_IF(!aRightContent.GetParentNode())) {
return Err(NS_ERROR_FAILURE);
}
RefPtr<JoinNodesTransaction> transaction =
JoinNodesTransaction::MaybeCreate(*this, aLeftContent, aRightContent);
if (MOZ_UNLIKELY(!transaction)) {
NS_WARNING("JoinNodesTransaction::MaybeCreate() failed");
return JoinNodesResult(NS_ERROR_FAILURE);
return Err(NS_ERROR_FAILURE);
}
const nsresult rv = DoTransactionInternal(transaction);
// FYI: Now, DidJoinNodesTransaction() must have been run if succeeded.
if (MOZ_UNLIKELY(NS_WARN_IF(Destroyed()))) {
return JoinNodesResult(NS_ERROR_EDITOR_DESTROYED);
if (NS_WARN_IF(Destroyed())) {
return Err(NS_ERROR_EDITOR_DESTROYED);
}
// This shouldn't occur unless the cycle collector runs by chrome script
// forcibly.
if (MOZ_UNLIKELY(NS_WARN_IF(!transaction->GetRemovedContent()) ||
NS_WARN_IF(!transaction->GetExistingContent()))) {
return JoinNodesResult(NS_ERROR_UNEXPECTED);
if (NS_WARN_IF(!transaction->GetRemovedContent()) ||
NS_WARN_IF(!transaction->GetExistingContent())) {
return Err(NS_ERROR_UNEXPECTED);
}
// If joined node is moved to different place, offset may not have any
// meaning. In this case, the web app modified the DOM tree takes on the
// responsibility for the remaning things.
if (MOZ_UNLIKELY(NS_WARN_IF(transaction->GetExistingContent()->GetParent() !=
transaction->GetParentNode()))) {
return JoinNodesResult(NS_ERROR_EDITOR_UNEXPECTED_DOM_TREE);
if (NS_WARN_IF(transaction->GetExistingContent()->GetParent() !=
transaction->GetParentNode())) {
return Err(NS_ERROR_EDITOR_UNEXPECTED_DOM_TREE);
}
if (MOZ_UNLIKELY(NS_FAILED(rv))) {
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::DoTransactionInternal() failed");
return JoinNodesResult(rv);
return Err(rv);
}
return JoinNodesResult(transaction->CreateJoinedPoint<EditorDOMPoint>(),

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

@ -1719,7 +1719,7 @@ class HTMLEditor final : public EditorBase,
* @param aRightContent The node which will be new container of the content
* of aLeftContent.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT JoinNodesResult
[[nodiscard]] MOZ_CAN_RUN_SCRIPT Result<JoinNodesResult, nsresult>
JoinNodesWithTransaction(nsIContent& aLeftContent, nsIContent& aRightContent);
/**

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

@ -4440,15 +4440,16 @@ Result<EditorDOMPoint, nsresult> HTMLEditor::AutoDeleteRangesHandler::
HTMLEditUtils::CanContentsBeJoined(
*leftContentToJoin, *rightContentToJoin, kCompareStyle)) {
// Do the join
JoinNodesResult joinNodesResult = aHTMLEditor.JoinNodesWithTransaction(
*leftContentToJoin, *rightContentToJoin);
if (MOZ_UNLIKELY(joinNodesResult.Failed())) {
Result<JoinNodesResult, nsresult> joinNodesResult =
aHTMLEditor.JoinNodesWithTransaction(*leftContentToJoin,
*rightContentToJoin);
if (MOZ_UNLIKELY(joinNodesResult.isErr())) {
NS_WARNING("HTMLEditor::JoinNodesWithTransaction() failed");
return Err(joinNodesResult.Rv());
return joinNodesResult.propagateErr();
}
ret = joinNodesResult.AtJoinedPoint<EditorDOMPoint>();
if (MOZ_UNLIKELY(NS_WARN_IF(!ret.IsSet()))) {
ret = joinNodesResult.inspect().AtJoinedPoint<EditorDOMPoint>();
if (NS_WARN_IF(!ret.IsSet())) {
return Err(NS_ERROR_FAILURE);
}

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

@ -737,14 +737,14 @@ Result<EditorDOMPoint, nsresult> HTMLEditor::SetInlinePropertyOnNodeImpl(
// JoinNodesWithTransaction (DoJoinNodes) tries to collapse selection to
// the joined point and we want to skip updating `Selection` here.
AutoTransactionsConserveSelection dontChangeMySelection(*this);
JoinNodesResult joinNodesResult =
Result<JoinNodesResult, nsresult> joinNodesResult =
JoinNodesWithTransaction(*previousSibling, *nextSibling);
if (joinNodesResult.Failed()) {
if (MOZ_UNLIKELY(joinNodesResult.isErr())) {
NS_WARNING("HTMLEditor::JoinNodesWithTransaction() failed");
return Err(joinNodesResult.Rv());
return joinNodesResult.propagateErr();
}
// So, let's take it.
return joinNodesResult.AtJoinedPoint<EditorDOMPoint>();
return joinNodesResult.inspect().AtJoinedPoint<EditorDOMPoint>();
}
}