Skip to content

Commit

Permalink
Bug 1739315 - part 1: Make HTMLEditor::JoinNodesWithTransaction() d…
Browse files Browse the repository at this point in the history
…irectly notify `TopLevelEditSubActionData::DidJoinNodes()` of joined position r=m_kato

`TopLevelEditSubActionData::WillJoinNodes()` and
`TopLevelEditSubActionData::DidJoinNodes()` are called only by
`HTMLEditor::JoinNodesWithTransaction()`.  `WillJoinNodes()` assumes that
all children or text data in `aLeftContent` is moved to head of `aRightContent`.
Therefore, it just stores length of `aLeftContent` and `DidJoinNodes()` lets
`AddPointToChangedRange()` know the joined point.

Same value is stored by `HTMLEditor::JoinNodesWithTransaction()`.  Therefore,
it can create same DOM point at calling `DidJoinNodes()` so that we can get
rid of `WillJoinNodes()`.

Differential Revision: https://phabricator.services.mozilla.com/D130348
  • Loading branch information
masayuki-nakano committed Nov 5, 2021
1 parent f914460 commit 4dd2a40
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 41 deletions.
28 changes: 3 additions & 25 deletions editor/libeditor/EditorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6539,27 +6539,8 @@ void EditorBase::TopLevelEditSubActionData::DidSplitContent(
"failed, but ignored");
}

void EditorBase::TopLevelEditSubActionData::WillJoinContents(
EditorBase& aEditorBase, nsIContent& aLeftContent,
nsIContent& aRightContent) {
MOZ_ASSERT(aEditorBase.AsHTMLEditor());

if (!aEditorBase.mInitSucceeded || aEditorBase.Destroyed()) {
return; // We have not been initialized yet or already been destroyed.
}

if (!aEditorBase.EditSubActionDataRef().mAdjustChangedRangeFromListener) {
return; // Temporarily disabled by edit sub-action handler.
}

// remember split point
aEditorBase.EditSubActionDataRef().mJoinedLeftNodeLength =
aLeftContent.Length();
}

void EditorBase::TopLevelEditSubActionData::DidJoinContents(
EditorBase& aEditorBase, nsIContent& aLeftContent,
nsIContent& aRightContent) {
EditorBase& aEditorBase, const EditorRawDOMPoint& aJoinedPoint) {
MOZ_ASSERT(aEditorBase.AsHTMLEditor());

if (!aEditorBase.mInitSucceeded || aEditorBase.Destroyed()) {
Expand All @@ -6570,11 +6551,8 @@ void EditorBase::TopLevelEditSubActionData::DidJoinContents(
return; // Temporarily disabled by edit sub-action handler.
}

DebugOnly<nsresult> rvIgnored = AddPointToChangedRange(
*aEditorBase.AsHTMLEditor(),
EditorRawDOMPoint(
&aRightContent,
aEditorBase.EditSubActionDataRef().mJoinedLeftNodeLength));
DebugOnly<nsresult> rvIgnored =
AddPointToChangedRange(*aEditorBase.AsHTMLEditor(), aJoinedPoint);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"TopLevelEditSubActionData::AddPointToChangedRange() "
"failed, but ignored");
Expand Down
13 changes: 3 additions & 10 deletions editor/libeditor/EditorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -878,10 +878,8 @@ class EditorBase : public nsIEditor,
void DidSplitContent(EditorBase& aEditorBase,
nsIContent& aExistingRightContent,
nsIContent& aNewLeftContent);
void WillJoinContents(EditorBase& aEditorBase, nsIContent& aLeftContent,
nsIContent& aRightContent);
void DidJoinContents(EditorBase& aEditorBase, nsIContent& aLeftContent,
nsIContent& aRightContent);
void DidJoinContents(EditorBase& aEditorBase,
const EditorRawDOMPoint& aJoinedPoint);
void DidInsertText(EditorBase& aEditorBase,
const EditorRawDOMPoint& aInsertionBegin,
const EditorRawDOMPoint& aInsertionEnd);
Expand Down Expand Up @@ -937,19 +935,14 @@ class EditorBase : public nsIEditor,
};

struct MOZ_STACK_CLASS EditSubActionData final {
uint32_t mJoinedLeftNodeLength;

// While this is set to false, TopLevelEditSubActionData::mChangedRange
// shouldn't be modified since in some cases, modifying it in the setter
// itself may be faster. Note that we should affect this only for current
// edit sub action since mutation event listener may edit different range.
bool mAdjustChangedRangeFromListener;

private:
void Clear() {
mJoinedLeftNodeLength = 0;
mAdjustChangedRangeFromListener = true;
}
void Clear() { mAdjustChangedRangeFromListener = true; }

friend EditorBase;
};
Expand Down
9 changes: 3 additions & 6 deletions editor/libeditor/HTMLEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4591,10 +4591,7 @@ nsresult HTMLEditor::JoinNodesWithTransaction(nsINode& aLeftNode,
return NS_ERROR_FAILURE;
}
// Find the number of children of the lefthand node
uint32_t oldLeftNodeLen = aLeftNode.Length();

TopLevelEditSubActionDataRef().WillJoinContents(*this, *aLeftNode.AsContent(),
*aRightNode.AsContent());
const uint32_t oldLeftNodeLen = aLeftNode.Length();

RefPtr<JoinNodeTransaction> transaction = JoinNodeTransaction::MaybeCreate(
*this, *aLeftNode.AsContent(), *aRightNode.AsContent());
Expand All @@ -4616,8 +4613,8 @@ nsresult HTMLEditor::JoinNodesWithTransaction(nsINode& aLeftNode,
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"RangeUpdater::SelAdjJoinNodes() failed, but ignored");

TopLevelEditSubActionDataRef().DidJoinContents(*this, *aLeftNode.AsContent(),
*aRightNode.AsContent());
TopLevelEditSubActionDataRef().DidJoinContents(
*this, EditorRawDOMPoint(&aRightNode, oldLeftNodeLen));

if (mInlineSpellChecker) {
RefPtr<mozInlineSpellChecker> spellChecker = mInlineSpellChecker;
Expand Down

0 comments on commit 4dd2a40

Please sign in to comment.