Bug 1200194 - Expose nsCaret::GetFrameAndOffset for reuse. r=roc
This function can be reused by AccessibleCaret.
--- a/layout/base/AccessibleCaretManager.cpp
+++ b/layout/base/AccessibleCaretManager.cpp
@@ -120,34 +120,20 @@ AccessibleCaretManager::UpdateCaretsForC
AC_LOG("%s, selection: %p", __FUNCTION__, GetSelection());
nsRefPtr<nsCaret> caret = mPresShell->GetCaret();
if (!caret || !caret->IsVisible()) {
HideCarets();
return;
}
- nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
- Selection* selection = GetSelection();
- if (!fs || !selection) {
- HideCarets();
- return;
- }
+ int32_t offset = 0;
+ nsIFrame* frame = nsCaret::GetFrameAndOffset(GetSelection(), nullptr, 0, &offset);
- nsINode* focusNode = selection->GetFocusNode();
- nsIContent* focusContent = focusNode->AsContent();
- uint32_t focusOffset = selection->FocusOffset();
-
- nsIFrame* frame = nullptr;
- int32_t offset = 0;
- nsresult rv = nsCaret::GetCaretFrameForNodeOffset(
- fs, focusContent, focusOffset, fs->GetHint(), fs->GetCaretBidiLevel(),
- &frame, &offset);
-
- if (NS_FAILED(rv) || !frame) {
+ if (!frame) {
HideCarets();
return;
}
Element* editingHost = frame->GetContent()->GetEditingHost();
if (!editingHost) {
HideCarets();
return;
--- a/layout/base/nsCaret.cpp
+++ b/layout/base/nsCaret.cpp
@@ -405,20 +405,20 @@ nsCaret::GetGeometryForFrame(nsIFrame* a
}
if (aBidiIndicatorSize) {
*aBidiIndicatorSize = caretMetrics.mBidiIndicatorSize;
}
return rect;
}
-static nsIFrame*
-GetFrameAndOffset(Selection* aSelection,
- nsINode* aOverrideNode, int32_t aOverrideOffset,
- int32_t* aFrameOffset)
+nsIFrame*
+nsCaret::GetFrameAndOffset(Selection* aSelection,
+ nsINode* aOverrideNode, int32_t aOverrideOffset,
+ int32_t* aFrameOffset)
{
nsINode* focusNode;
int32_t focusOffset;
if (aOverrideNode) {
focusNode = aOverrideNode;
focusOffset = aOverrideOffset;
} else if (aSelection) {
--- a/layout/base/nsCaret.h
+++ b/layout/base/nsCaret.h
@@ -163,16 +163,26 @@ class nsCaret final : public nsISelectio
CaretAssociationHint aFrameHint,
uint8_t aBidiLevel,
nsIFrame** aReturnFrame,
int32_t* aReturnOffset);
static nsRect GetGeometryForFrame(nsIFrame* aFrame,
int32_t aFrameOffset,
nscoord* aBidiIndicatorSize);
+ // Get the frame and frame offset based on the focus node and focus offset
+ // of aSelection. If aOverrideNode and aOverride are provided, use them
+ // instead.
+ // @param aFrameOffset return the frame offset if non-null.
+ // @return the frame of the focus node.
+ static nsIFrame* GetFrameAndOffset(mozilla::dom::Selection* aSelection,
+ nsINode* aOverrideNode,
+ int32_t aOverrideOffset,
+ int32_t* aFrameOffset);
+
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
protected:
static void CaretBlinkCallback(nsITimer *aTimer, void *aClosure);
bool IsBidiUI();
void CheckSelectionLanguageChange();