Bug 1762115 - part 2: Make `InsertNodeTransaction::DoTransaction()` stop touching `Selection` directly r=m_kato

Unfortunately, we need to keep `InsertNodeTransaction::RedoTransaction()`
touching `Selection` for now because redoing `Selection` management is out
of scope of this bug (it's probably require big changes, but no benefits for
now).

Differential Revision: https://phabricator.services.mozilla.com/D144645
This commit is contained in:
Masayuki Nakano 2022-05-02 23:17:18 +00:00
Родитель 51c51fce83
Коммит c80b6f4515
4 изменённых файлов: 58 добавлений и 23 удалений

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

@ -2066,6 +2066,17 @@ nsresult EditorBase::InsertNodeWithTransaction(
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditorBase::DoTransactionInternal() failed");
if (NS_SUCCEEDED(rv) && AllowsTransactionsToChangeSelection()) {
const auto pointToPutCaret =
transaction->SuggestPointToPutCaret<EditorRawDOMPoint>();
if (pointToPutCaret.IsSet()) {
DebugOnly<nsresult> rvIgnored = CollapseSelectionTo(pointToPutCaret);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored) || rvIgnored == NS_ERROR_EDITOR_DESTROYED,
"EditorBase::CollapseSelectionTo() failed, but ignored");
}
}
DebugOnly<nsresult> rvIgnored =
RangeUpdaterRef().SelAdjInsertNode(aPointToInsert);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
@ -2075,7 +2086,7 @@ nsresult EditorBase::InsertNodeWithTransaction(
TopLevelEditSubActionDataRef().DidInsertContent(*this, aContentToInsert);
}
return rv;
return MOZ_UNLIKELY(Destroyed()) ? NS_ERROR_EDITOR_DESTROYED : rv;
}
CreateElementResult

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

@ -3022,6 +3022,18 @@ Result<RefPtr<Element>, nsresult> HTMLEditor::CreateAndInsertElement(
rv = aWithTransaction == WithTransaction::Yes
? DoTransactionInternal(transaction)
: transaction->DoTransaction();
if (NS_SUCCEEDED(rv) && AllowsTransactionsToChangeSelection()) {
const auto pointToPutCaret =
transaction->SuggestPointToPutCaret<EditorRawDOMPoint>();
if (pointToPutCaret.IsSet()) {
DebugOnly<nsresult> rvIgnored =
CollapseSelectionTo(pointToPutCaret);
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored) ||
rvIgnored == NS_ERROR_EDITOR_DESTROYED,
"EditorBase::CollapseSelectionTo() failed, but ignored");
}
}
}
}
if (MOZ_UNLIKELY(NS_FAILED(rv))) {

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

@ -5,14 +5,13 @@
#include "InsertNodeTransaction.h"
#include "mozilla/EditorBase.h" // for EditorBase
#include "mozilla/EditorDOMPoint.h" // for EditorDOMPoint
#include "mozilla/HTMLEditor.h" // for HTMLEditor
#include "mozilla/Logging.h"
#include "mozilla/TextEditor.h" // for TextEditor
#include "mozilla/ToString.h"
#include "EditorBase.h" // for EditorBase
#include "EditorDOMPoint.h" // for EditorDOMPoint
#include "HTMLEditor.h" // for HTMLEditor
#include "TextEditor.h" // for TextEditor
#include "mozilla/dom/Selection.h" // for Selection
#include "mozilla/Logging.h"
#include "mozilla/ToString.h"
#include "nsAString.h"
#include "nsDebug.h" // for NS_WARNING, etc.
@ -139,20 +138,6 @@ NS_IMETHODIMP InsertNodeTransaction::DoTransaction() {
return error.StealNSResult();
}
if (!mEditorBase->AllowsTransactionsToChangeSelection()) {
return NS_OK;
}
RefPtr<Selection> selection = mEditorBase->GetSelection();
if (NS_WARN_IF(!selection)) {
return NS_ERROR_FAILURE;
}
// Place the selection just after the inserted element.
editorBase->CollapseSelectionTo(EditorRawDOMPoint::After(contentToInsert),
error);
NS_WARNING_ASSERTION(!error.Failed(),
"EditorBase::CollapseSelectionTo() failed, but ignored");
return NS_OK;
}
@ -179,7 +164,22 @@ NS_IMETHODIMP InsertNodeTransaction::RedoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p InsertNodeTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
return DoTransaction();
nsresult rv = DoTransaction();
if (MOZ_UNLIKELY(NS_FAILED(rv))) {
NS_WARNING("InsertNodeTransaction::RedoTransaction() failed");
return rv;
}
if (!mEditorBase->AllowsTransactionsToChangeSelection()) {
return NS_OK;
}
OwningNonNull<EditorBase> editorBase(*mEditorBase);
rv = editorBase->CollapseSelectionTo(
SuggestPointToPutCaret<EditorRawDOMPoint>());
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditorBase::CollapseSelectionTo() failed, but ignored");
return NS_OK;
}
} // namespace mozilla

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

@ -58,6 +58,18 @@ class InsertNodeTransaction final : public EditTransactionBase {
MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
/**
* SuggestPointToPutCaret() suggests a point after doing or redoing the
* transaction.
*/
template <typename EditorDOMPointType>
EditorDOMPointType SuggestPointToPutCaret() const {
if (MOZ_UNLIKELY(!mPointToInsert.IsSet() || !mContentToInsert)) {
return EditorDOMPointType();
}
return EditorDOMPointType::After(mContentToInsert);
}
friend std::ostream& operator<<(std::ostream& aStream,
const InsertNodeTransaction& aTransaction);