Bug 771873 part 1 - Check for reversed indices in Selection::GetIndicesForInterval; r=ehsan

This commit is contained in:
Aryeh Gregor 2012-07-12 18:56:30 +03:00
Родитель aa41501b44
Коммит 98c0f7c228
2 изменённых файлов: 30 добавлений и 21 удалений

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

@ -188,10 +188,10 @@ private:
bool EqualsRangeAtPoint(nsINode* aBeginNode, PRInt32 aBeginOffset, bool EqualsRangeAtPoint(nsINode* aBeginNode, PRInt32 aBeginOffset,
nsINode* aEndNode, PRInt32 aEndOffset, nsINode* aEndNode, PRInt32 aEndOffset,
PRInt32 aRangeIndex); PRInt32 aRangeIndex);
void GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset, nsresult GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset,
nsINode* aEndNode, PRInt32 aEndOffset, nsINode* aEndNode, PRInt32 aEndOffset,
bool aAllowAdjacent, bool aAllowAdjacent,
PRInt32 *aStartIndex, PRInt32 *aEndIndex); PRInt32* aStartIndex, PRInt32* aEndIndex);
RangeData* FindRangeData(nsIDOMRange* aRange); RangeData* FindRangeData(nsIDOMRange* aRange);
// These are the ranges inside this selection. They are kept sorted in order // These are the ranges inside this selection. They are kept sorted in order

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

@ -3474,9 +3474,12 @@ Selection::AddItem(nsRange* aItem, PRInt32* aOutIndex)
} }
PRInt32 startIndex, endIndex; PRInt32 startIndex, endIndex;
GetIndicesForInterval(aItem->GetStartParent(), aItem->StartOffset(), nsresult rv = GetIndicesForInterval(aItem->GetStartParent(),
aItem->GetEndParent(), aItem->EndOffset(), aItem->StartOffset(),
false, &startIndex, &endIndex); aItem->GetEndParent(),
aItem->EndOffset(), false,
&startIndex, &endIndex);
NS_ENSURE_SUCCESS(rv, rv);
if (endIndex == -1) { if (endIndex == -1) {
// All ranges start after the given range. We can insert our range at // All ranges start after the given range. We can insert our range at
@ -3540,10 +3543,9 @@ Selection::AddItem(nsRange* aItem, PRInt32* aOutIndex)
// Insert the new element into our "leftovers" array // Insert the new element into our "leftovers" array
PRInt32 insertionPoint; PRInt32 insertionPoint;
nsresult rv = FindInsertionPoint(&temp, aItem->GetStartParent(), rv = FindInsertionPoint(&temp, aItem->GetStartParent(),
aItem->StartOffset(), aItem->StartOffset(), CompareToRangeStart,
CompareToRangeStart, &insertionPoint);
&insertionPoint);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
if (!temp.InsertElementAt(insertionPoint, RangeData(aItem))) if (!temp.InsertElementAt(insertionPoint, RangeData(aItem)))
@ -3738,8 +3740,11 @@ Selection::GetRangesForIntervalArray(nsINode* aBeginNode, PRInt32 aBeginOffset,
{ {
aRanges->Clear(); aRanges->Clear();
PRInt32 startIndex, endIndex; PRInt32 startIndex, endIndex;
GetIndicesForInterval(aBeginNode, aBeginOffset, aEndNode, aEndOffset, nsresult res = GetIndicesForInterval(aBeginNode, aBeginOffset,
aAllowAdjacent, &startIndex, &endIndex); aEndNode, aEndOffset, aAllowAdjacent,
&startIndex, &endIndex);
NS_ENSURE_SUCCESS(res, res);
if (startIndex == -1 || endIndex == -1) if (startIndex == -1 || endIndex == -1)
return NS_OK; return NS_OK;
@ -3757,7 +3762,7 @@ Selection::GetRangesForIntervalArray(nsINode* aBeginNode, PRInt32 aBeginOffset,
// instead this returns the indices into mRanges between which the // instead this returns the indices into mRanges between which the
// overlapping ranges lie. // overlapping ranges lie.
void nsresult
Selection::GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset, Selection::GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset,
nsINode* aEndNode, PRInt32 aEndOffset, nsINode* aEndNode, PRInt32 aEndOffset,
bool aAllowAdjacent, bool aAllowAdjacent,
@ -3775,7 +3780,7 @@ Selection::GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset,
*aEndIndex = -1; *aEndIndex = -1;
if (mRanges.Length() == 0) if (mRanges.Length() == 0)
return; return NS_OK;
bool intervalIsCollapsed = aBeginNode == aEndNode && bool intervalIsCollapsed = aBeginNode == aEndNode &&
aBeginOffset == aEndOffset; aBeginOffset == aEndOffset;
@ -3786,7 +3791,7 @@ Selection::GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset,
if (NS_FAILED(FindInsertionPoint(&mRanges, aEndNode, aEndOffset, if (NS_FAILED(FindInsertionPoint(&mRanges, aEndNode, aEndOffset,
&CompareToRangeStart, &CompareToRangeStart,
&endsBeforeIndex))) { &endsBeforeIndex))) {
return; return NS_OK;
} }
if (endsBeforeIndex == 0) { if (endsBeforeIndex == 0) {
@ -3795,7 +3800,7 @@ Selection::GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset,
// If the interval is strictly before the range at index 0, we can optimize // If the interval is strictly before the range at index 0, we can optimize
// by returning now - all ranges start after the given interval // by returning now - all ranges start after the given interval
if (!RangeMatchesBeginPoint(endRange, aEndNode, aEndOffset)) if (!RangeMatchesBeginPoint(endRange, aEndNode, aEndOffset))
return; return NS_OK;
// We now know that the start point of mRanges[0].mRange equals the end of // We now know that the start point of mRanges[0].mRange equals the end of
// the interval. Thus, when aAllowadjacent is true, the caller is always // the interval. Thus, when aAllowadjacent is true, the caller is always
@ -3803,7 +3808,7 @@ Selection::GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset,
// remember to include the range when both it and the given interval are // remember to include the range when both it and the given interval are
// collapsed to the same point // collapsed to the same point
if (!aAllowAdjacent && !(endRange->Collapsed() && intervalIsCollapsed)) if (!aAllowAdjacent && !(endRange->Collapsed() && intervalIsCollapsed))
return; return NS_OK;
} }
*aEndIndex = endsBeforeIndex; *aEndIndex = endsBeforeIndex;
@ -3811,10 +3816,10 @@ Selection::GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset,
if (NS_FAILED(FindInsertionPoint(&mRanges, aBeginNode, aBeginOffset, if (NS_FAILED(FindInsertionPoint(&mRanges, aBeginNode, aBeginOffset,
&CompareToRangeEnd, &CompareToRangeEnd,
&beginsAfterIndex))) { &beginsAfterIndex))) {
return; return NS_OK;
} }
if (beginsAfterIndex == (PRInt32) mRanges.Length()) if (beginsAfterIndex == (PRInt32) mRanges.Length())
return; // optimization: all ranges are strictly before us return NS_OK; // optimization: all ranges are strictly before us
if (aAllowAdjacent) { if (aAllowAdjacent) {
// At this point, one of the following holds: // At this point, one of the following holds:
@ -3874,9 +3879,13 @@ Selection::GetIndicesForInterval(nsINode* aBeginNode, PRInt32 aBeginOffset,
} }
} }
NS_ASSERTION(beginsAfterIndex <= endsBeforeIndex,
"Is mRanges not ordered?");
NS_ENSURE_STATE(beginsAfterIndex <= endsBeforeIndex);
*aStartIndex = beginsAfterIndex; *aStartIndex = beginsAfterIndex;
*aEndIndex = endsBeforeIndex; *aEndIndex = endsBeforeIndex;
return; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP