Bug 1587746: remove RangeBoundaryBase::Set and use constructor instead. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D48826

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mirko Brodesser 2019-10-11 12:43:57 +00:00
parent a6c154dc1f
commit b761e516f6
4 changed files with 38 additions and 44 deletions

View File

@ -167,31 +167,6 @@ class RangeBoundaryBase {
mOffset.reset();
}
void Set(nsINode* aContainer, int32_t aOffset) {
mParent = aContainer;
if (mParent && mParent->IsContainerNode()) {
// Find a reference node
if (aOffset == static_cast<int32_t>(aContainer->GetChildCount())) {
mRef = aContainer->GetLastChild();
} else if (aOffset == 0) {
mRef = nullptr;
} else {
mRef = mParent->GetChildAt_Deprecated(aOffset - 1);
MOZ_ASSERT(mRef);
}
NS_WARNING_ASSERTION(mRef || aOffset == 0,
"Setting RangeBoundary to invalid value");
} else {
mRef = nullptr;
}
mOffset = mozilla::Some(aOffset);
NS_WARNING_ASSERTION(!mRef || mRef->GetParentNode() == mParent,
"Setting RangeBoundary to invalid value");
}
bool IsSet() const { return mParent && (mRef || mOffset.isSome()); }
bool IsSetAndValid() const {

View File

@ -20,6 +20,8 @@
#include "nsGkAtoms.h"
#include "nsContentUtils.h"
#include "nsTextFrame.h"
#include "mozilla/Assertions.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/ContentIterator.h"
#include "mozilla/dom/CharacterData.h"
#include "mozilla/dom/DocumentFragment.h"
@ -458,7 +460,7 @@ void nsRange::CharacterDataChanged(nsIContent* aContent,
NS_ASSERTION(mStart.Offset() <= aInfo.mChangeEnd + 1,
"mStart.Offset() is beyond the end of this node");
int32_t newStartOffset = mStart.Offset() - aInfo.mChangeStart;
newStart.Set(aInfo.mDetails->mNextSibling, newStartOffset);
newStart = {aInfo.mDetails->mNextSibling, newStartOffset};
if (MOZ_UNLIKELY(aContent == mRoot)) {
newRoot = RangeUtils::ComputeRootNode(newStart.Container());
}
@ -482,7 +484,7 @@ void nsRange::CharacterDataChanged(nsIContent* aContent,
: mStart.Offset() + aInfo.mChangeStart -
aInfo.mChangeEnd +
aInfo.mReplaceLength;
newStart.Set(mStart.Container(), newStartOffset);
newStart = {mStart.Container(), newStartOffset};
}
}
@ -495,10 +497,12 @@ void nsRange::CharacterDataChanged(nsIContent* aContent,
NS_ASSERTION(
aInfo.mDetails->mType == CharacterDataChangeInfo::Details::eSplit,
"only a split can start before the end");
NS_ASSERTION(mEnd.Offset() <= aInfo.mChangeEnd + 1,
"mEnd.Offset() is beyond the end of this node");
newEnd.Set(aInfo.mDetails->mNextSibling,
mEnd.Offset() - aInfo.mChangeStart);
MOZ_ASSERT(mEnd.Offset() <= aInfo.mChangeEnd + 1,
"mEnd.Offset() is beyond the end of this node");
const CheckedInt<int32_t> newEndOffset{mEnd.Offset() -
aInfo.mChangeStart};
newEnd = {aInfo.mDetails->mNextSibling, newEndOffset.value()};
bool isCommonAncestor =
IsInSelection() && mStart.Container() == mEnd.Container();
@ -516,7 +520,7 @@ void nsRange::CharacterDataChanged(nsIContent* aContent,
? aInfo.mChangeStart
: mEnd.Offset() + aInfo.mChangeStart -
aInfo.mChangeEnd + aInfo.mReplaceLength;
newEnd.Set(mEnd.Container(), newEndOffset);
newEnd = {mEnd.Container(), newEndOffset};
}
}
@ -526,13 +530,18 @@ void nsRange::CharacterDataChanged(nsIContent* aContent,
// that will be removed
nsIContent* removed = aInfo.mDetails->mNextSibling;
if (removed == mStart.Container()) {
newStart.Set(aContent, mStart.Offset() + aInfo.mChangeStart);
const CheckedInt<int32_t> newStartOffset{mStart.Offset() +
aInfo.mChangeStart};
newStart = {aContent, newStartOffset.value()};
if (MOZ_UNLIKELY(removed == mRoot)) {
newRoot = RangeUtils::ComputeRootNode(newStart.Container());
}
}
if (removed == mEnd.Container()) {
newEnd.Set(aContent, mEnd.Offset() + aInfo.mChangeStart);
const CheckedInt<int32_t> newEndOffset{mEnd.Offset() +
aInfo.mChangeStart};
newEnd = {aContent, newEndOffset.value()};
if (MOZ_UNLIKELY(removed == mRoot)) {
newRoot = RangeUtils::ComputeRootNode(newEnd.Container());
}
@ -547,12 +556,14 @@ void nsRange::CharacterDataChanged(nsIContent* aContent,
if (parentNode == mStart.Container() && mStart.Offset() > 0 &&
mStart.Offset() < parentNode->GetChildCount() &&
removed == mStart.GetChildAtOffset()) {
newStart.Set(aContent, aInfo.mChangeStart);
const CheckedInt<int32_t> newStartOffset{aInfo.mChangeStart};
newStart = {aContent, newStartOffset.value()};
}
if (parentNode == mEnd.Container() && mEnd.Offset() > 0 &&
mEnd.Offset() < parentNode->GetChildCount() &&
removed == mEnd.GetChildAtOffset()) {
newEnd.Set(aContent, aInfo.mChangeEnd);
const CheckedInt<int32_t> newEndOffset{aInfo.mChangeEnd};
newEnd = {aContent, newEndOffset.value()};
}
}

View File

@ -6,6 +6,8 @@
#include "ContentEventHandler.h"
#include "mozilla/Assertions.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/ContentIterator.h"
#include "mozilla/EditorUtils.h"
#include "mozilla/IMEStateManager.h"
@ -1472,7 +1474,7 @@ ContentEventHandler::GetFirstFrameInRangeForTextRect(
int32_t offsetInNode =
node == aRawRange.GetStartContainer() ? aRawRange.StartOffset() : 0;
if (static_cast<uint32_t>(offsetInNode) < node->Length()) {
nodePosition.Set(node, offsetInNode);
nodePosition = {node, offsetInNode};
break;
}
continue;
@ -1482,7 +1484,7 @@ ContentEventHandler::GetFirstFrameInRangeForTextRect(
// node causing text.
if (ShouldBreakLineBefore(node->AsContent(), mRootContent) ||
IsPaddingBR(node->AsContent())) {
nodePosition.Set(node, 0);
nodePosition = {node, 0};
}
}
@ -1549,13 +1551,14 @@ ContentEventHandler::GetLastFrameInRangeForTextRect(const RawRange& aRawRange) {
}
if (node->IsText()) {
uint32_t offset;
CheckedInt<int32_t> offset;
if (node == aRawRange.GetEndContainer()) {
offset = aRawRange.EndOffset();
} else {
offset = node->Length();
}
nodePosition.Set(node, offset);
nodePosition = {node, offset.value()};
// If the text node is empty or the last node of the range but the index
// is 0, we should store current position but continue looking for
@ -1569,7 +1572,7 @@ ContentEventHandler::GetLastFrameInRangeForTextRect(const RawRange& aRawRange) {
if (ShouldBreakLineBefore(node->AsContent(), mRootContent) ||
IsPaddingBR(node->AsContent())) {
nodePosition.Set(node, 0);
nodePosition = {node, 0};
break;
}
}
@ -1603,7 +1606,9 @@ ContentEventHandler::GetLastFrameInRangeForTextRect(const RawRange& aRawRange) {
// at least one text frame.
if (nodePosition.Offset() &&
nodePosition.Offset() == static_cast<uint32_t>(start)) {
nodePosition.Set(nodePosition.Container(), nodePosition.Offset() - 1);
const CheckedInt<int32_t> newNodePositionOffset{nodePosition.Offset() - 1};
nodePosition = {nodePosition.Container(), newNodePositionOffset.value()};
GetFrameForTextRect(nodePosition.Container(), nodePosition.Offset(), true,
&lastFrame);
if (NS_WARN_IF(!lastFrame)) {

View File

@ -9,6 +9,7 @@
#include "HTMLEditUtils.h"
#include "WSRunObject.h"
#include "mozilla/Assertions.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/ContentIterator.h"
#include "mozilla/CSSEditUtils.h"
#include "mozilla/EditAction.h"
@ -53,6 +54,7 @@
#include "nsTextNode.h"
#include "nsThreadUtils.h"
#include "nsUnicharUtils.h"
#include <algorithm>
// Workaround for windows headers
@ -7524,8 +7526,9 @@ void HTMLEditor::SelectBRElementIfCollapsedInEmptyBlock(
bool isEmptyNode = false;
IsEmptyNode(block, &isEmptyNode, true, false);
if (isEmptyNode) {
aStartRef.Set(block, 0);
aEndRef.Set(block, block->Length());
aStartRef = {block, 0};
const CheckedInt<int32_t> offset{block->Length()};
aEndRef = {block, offset.value()};
}
}