Bug 1200194 - Expose nsCaret::GetFrameAndOffset for reuse. r=roc draft
authorTing-Yu Lin <tlin@mozilla.com>
Tue, 08 Sep 2015 17:04:26 +0800
changeset 290879 c6b6684cf89cd19fb4701b90e5362559c948b953
parent 290878 61851e591c0f852a4aaab9494a3377450c427a6c
child 290880 9e413aabc6bc9d8b4b3d8cd8c5fc8fc8d4db628d
push id5185
push usertlin@mozilla.com
push dateTue, 08 Sep 2015 09:19:58 +0000
reviewersroc
bugs1200194
milestone43.0a1
Bug 1200194 - Expose nsCaret::GetFrameAndOffset for reuse. r=roc This function can be reused by AccessibleCaret.
layout/base/AccessibleCaretManager.cpp
layout/base/nsCaret.cpp
layout/base/nsCaret.h
--- 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();