diff --git a/layout/generic/nsFrameSelection.cpp b/layout/generic/nsFrameSelection.cpp index 91fa7a066e8e..e92b9561bb19 100644 --- a/layout/generic/nsFrameSelection.cpp +++ b/layout/generic/nsFrameSelection.cpp @@ -277,23 +277,28 @@ struct MOZ_RAII AutoPrepareFocusRange { range->SetIsGenerated(false); aSelection->mAnchorFocusRange = range; - // Remove all generated ranges (including the old mAnchorFocusRange). - RefPtr presContext = aSelection->GetPresContext(); - size_t i = len; - while (i--) { - range = aSelection->mStyledRanges.mRanges[i].mRange; - if (range->IsGenerated()) { - range->UnregisterSelection(); - aSelection->SelectFrames(presContext, range, false); - aSelection->mStyledRanges.mRanges.RemoveElementAt(i); - } - } + RemoveGeneratedRanges(*aSelection); + if (aSelection->mFrameSelection) { aSelection->mFrameSelection->InvalidateDesiredCaretPos(); } } private: + static void RemoveGeneratedRanges(Selection& aSelection) { + RefPtr presContext = aSelection.GetPresContext(); + nsTArray& ranges = aSelection.mStyledRanges.mRanges; + size_t i = ranges.Length(); + while (i--) { + nsRange* range = ranges[i].mRange; + if (range->IsGenerated()) { + range->UnregisterSelection(); + aSelection.SelectFrames(presContext, range, false); + ranges.RemoveElementAt(i); + } + } + } + /** * @aParam aSelectionChangeReasons can be multiple of the reasons defined in nsISelectionListener.idl.