Bug 1945711 - part 4: Rename the aParent/aOffset version of nsContentUtils::ComparePoints to ComparePointsWithIndices r=jjaschke,dom-core

To make it easier to find the callers, we should give different name instead
of overloading.

Differential Revision: https://phabricator.services.mozilla.com/D236794
This commit is contained in:
Masayuki Nakano
2025-02-10 01:29:40 +00:00
parent e595f1e9aa
commit 01bac70fb9
6 changed files with 56 additions and 36 deletions

View File

@@ -217,7 +217,7 @@ nsresult RangeUtils::CompareNodeToRangeBoundaries(
}
*aNodeIsBeforeRange = *order > 0;
// is RANGE(end) >= NODE(end) ?
order = nsContentUtils::ComparePoints(
order = nsContentUtils::ComparePointsWithIndices(
aEndBoundary.Container(),
*aEndBoundary.Offset(
RangeBoundaryBase<EPT, ERT>::OffsetFilter::kValidOrInvalidOffsets),

View File

@@ -3082,14 +3082,17 @@ void Selection::Extend(nsINode& aContainer, uint32_t aOffset,
const uint32_t endOffset = range->MayCrossShadowBoundaryEndOffset();
bool shouldClearRange = false;
const Maybe<int32_t> anchorOldFocusOrder = nsContentUtils::ComparePoints(
anchorNode, anchorOffset, focusNode, focusOffset);
const Maybe<int32_t> anchorOldFocusOrder =
nsContentUtils::ComparePointsWithIndices(anchorNode, anchorOffset,
focusNode, focusOffset);
shouldClearRange |= !anchorOldFocusOrder;
const Maybe<int32_t> oldFocusNewFocusOrder = nsContentUtils::ComparePoints(
focusNode, focusOffset, &aContainer, aOffset);
const Maybe<int32_t> oldFocusNewFocusOrder =
nsContentUtils::ComparePointsWithIndices(focusNode, focusOffset,
&aContainer, aOffset);
shouldClearRange |= !oldFocusNewFocusOrder;
const Maybe<int32_t> anchorNewFocusOrder = nsContentUtils::ComparePoints(
anchorNode, anchorOffset, &aContainer, aOffset);
const Maybe<int32_t> anchorNewFocusOrder =
nsContentUtils::ComparePointsWithIndices(anchorNode, anchorOffset,
&aContainer, aOffset);
shouldClearRange |= !anchorNewFocusOrder;
// If the points are disconnected, the range will be collapsed below,

View File

@@ -3388,11 +3388,9 @@ Maybe<int32_t> nsContentUtils::CompareChildNodeAndChildOffset(
}
/* static */
Maybe<int32_t> nsContentUtils::ComparePoints(const nsINode* aParent1,
uint32_t aOffset1,
const nsINode* aParent2,
uint32_t aOffset2,
NodeIndexCache* aIndexCache) {
Maybe<int32_t> nsContentUtils::ComparePointsWithIndices(
const nsINode* aParent1, uint32_t aOffset1, const nsINode* aParent2,
uint32_t aOffset2, NodeIndexCache* aIndexCache) {
MOZ_ASSERT(aParent1);
MOZ_ASSERT(aParent2);
@@ -3555,9 +3553,9 @@ Maybe<int32_t> nsContentUtils::ComparePoints(
// RangeBoundaryBase instances may be initialized only with a child node or an
// offset in the container. If both instances have computed offset, we can
// use the other ComparePoints() which works with offsets.
// use ComparePointsWithIndices() which works with offsets.
if (aBoundary1.HasOffset() && aBoundary2.HasOffset()) {
return ComparePoints(
return ComparePointsWithIndices(
aBoundary1.Container(), *aBoundary1.Offset(kValidOrInvalidOffsets1),
aBoundary2.Container(), *aBoundary2.Offset(kValidOrInvalidOffsets2),
aIndexCache);
@@ -3581,7 +3579,7 @@ Maybe<int32_t> nsContentUtils::ComparePoints(
// Otherwise, we need to compare the common ancestor children which is the
// most distant different inclusive ancestors of the containers. So, the
// following implementation is similar to the other ComparePoints(), but we
// following implementation is similar to ComparePointsWithIndices(), but we
// don't have offset, so, we cannot use offset when we compare the boundaries
// whose one is a descendant of the other.
const CommonAncestors commonAncestors(*aBoundary1.Container(),

View File

@@ -688,17 +688,28 @@ class nsContentUtils {
/**
* Utility routine to compare two "points", where a point is a node/offset
* pair.
* Pass a cache object as aParent1Cache if you expect to repeatedly
* call this function with the same value as aParent1.
* Pass a cache object as aIndexCache if you expect to repeatedly
* call this function with the same value as aParent1 or aParent2.
*
* @return -1 if point1 < point2,
* 1 if point1 > point2,
* 0 if point1 == point2.
* `Nothing` if the two nodes aren't in the same connected subtree.
*/
static mozilla::Maybe<int32_t> ComparePoints(
static mozilla::Maybe<int32_t> ComparePointsWithIndices(
const nsINode* aParent1, uint32_t aOffset1, const nsINode* aParent2,
uint32_t aOffset2, NodeIndexCache* aIndexCache = nullptr);
/**
* Utility routine to compare two "points", where a point is a RangeBoundary.
* Pass a cache object as aIndexCache if you expect to repeatedly call this
* function with the same value as aBoundary1 or aBoundary2.
*
* @return -1 if point1 < point2,
* 1 if point1 > point2,
* 0 if point1 == point2.
* `Nothing` if the two nodes aren't in the same connected subtree.
*/
template <typename PT1, typename RT1, typename PT2, typename RT2>
static mozilla::Maybe<int32_t> ComparePoints(
const mozilla::RangeBoundaryBase<PT1, RT1>& aBoundary1,
@@ -737,12 +748,19 @@ class nsContentUtils {
}
// Otherwise, aOffset1 nor aOffset2 is referred so that any value is fine
// if negative.
return ComparePoints(
aParent1, aOffset1 < 0 ? UINT32_MAX : static_cast<uint32_t>(aOffset1),
return ComparePointsWithIndices(
aParent1,
// Avoid warnings.
aOffset1 < 0 ? aParent1->GetChildCount()
: std::min(static_cast<uint32_t>(aOffset1),
aParent1->GetChildCount()),
aParent2,
aOffset2 < 0 ? UINT32_MAX : static_cast<uint32_t>(aOffset2));
// Avoid warnings.
aOffset2 < 0 ? aParent2->GetChildCount()
: std::min(static_cast<uint32_t>(aOffset2),
aParent2->GetChildCount()));
}
return ComparePoints(aParent1, aOffset1, aParent2, aOffset2);
return ComparePointsWithIndices(aParent1, aOffset1, aParent2, aOffset2);
}
/**
@@ -3585,8 +3603,9 @@ class nsContentUtils {
NodeIndexCache* aIndexCache = nullptr);
/**
* Helper method for ComparePoints(). This includes odd traditional behavior.
* Therefore, do not use this method as a utility method.
* Helper method for ComparePoints() and ComparePointsWithIndices(). This
* includes odd traditional behavior. Therefore, do not use this method as a
* utility method.
*/
static mozilla::Maybe<int32_t> CompareClosestCommonAncestorChildren(
const nsINode&, const nsINode*, const nsINode*, NodeIndexCache*);

View File

@@ -1836,10 +1836,10 @@ void nsRange::CutContents(DocumentFragment** aFragment, ErrorResult& aRv) {
// has a common ancestor with start and end, hence both have to be
// comparable to it.
if (doctype &&
*nsContentUtils::ComparePoints(startContainer, startOffset, doctype,
0) < 0 &&
*nsContentUtils::ComparePoints(doctype, 0, endContainer, endOffset) <
0) {
*nsContentUtils::ComparePointsWithIndices(startContainer, startOffset,
doctype, 0) < 0 &&
*nsContentUtils::ComparePointsWithIndices(doctype, 0, endContainer,
endOffset) < 0) {
aRv.ThrowHierarchyRequestError("Start or end position isn't valid.");
return;
}

View File

@@ -1832,9 +1832,9 @@ nsresult TextServicesDocument::GetCollapsedSelection(
uint32_t offset = range->StartOffset();
const Maybe<int32_t> e1s1 = nsContentUtils::ComparePoints(
const Maybe<int32_t> e1s1 = nsContentUtils::ComparePointsWithIndices(
eStart->mTextNode, eStartOffset, parent, offset);
const Maybe<int32_t> e2s1 = nsContentUtils::ComparePoints(
const Maybe<int32_t> e2s1 = nsContentUtils::ComparePointsWithIndices(
eEnd->mTextNode, eEndOffset, parent, offset);
if (MOZ_UNLIKELY(NS_WARN_IF(!e1s1) || NS_WARN_IF(!e2s1))) {
@@ -2029,14 +2029,14 @@ nsresult TextServicesDocument::GetUncollapsedSelection(
NS_ENSURE_SUCCESS(rv, rv);
e1s2 = nsContentUtils::ComparePoints(eStart->mTextNode, eStartOffset,
endContainer, endOffset);
e1s2 = nsContentUtils::ComparePointsWithIndices(
eStart->mTextNode, eStartOffset, endContainer, endOffset);
if (NS_WARN_IF(!e1s2)) {
return NS_ERROR_FAILURE;
}
e2s1 = nsContentUtils::ComparePoints(eEnd->mTextNode, eEndOffset,
startContainer, startOffset);
e2s1 = nsContentUtils::ComparePointsWithIndices(
eEnd->mTextNode, eEndOffset, startContainer, startOffset);
if (NS_WARN_IF(!e2s1)) {
return NS_ERROR_FAILURE;
}
@@ -2057,13 +2057,13 @@ nsresult TextServicesDocument::GetUncollapsedSelection(
}
// Now that we have an intersecting range, find out more info:
const Maybe<int32_t> e1s1 = nsContentUtils::ComparePoints(
const Maybe<int32_t> e1s1 = nsContentUtils::ComparePointsWithIndices(
eStart->mTextNode, eStartOffset, startContainer, startOffset);
if (NS_WARN_IF(!e1s1)) {
return NS_ERROR_FAILURE;
}
const Maybe<int32_t> e2s2 = nsContentUtils::ComparePoints(
const Maybe<int32_t> e2s2 = nsContentUtils::ComparePointsWithIndices(
eEnd->mTextNode, eEndOffset, endContainer, endOffset);
if (NS_WARN_IF(!e2s2)) {
return NS_ERROR_FAILURE;