bug 758112 - use mozilla::Selection in accessible/ r=surkov
authorTrevor Saunders <trev.saunders@gmail.com>
Fri, 06 Apr 2012 20:09:31 -0400
changeset 98470 20137a4f2187a2ea5e4a37a023f0f1aac3cf84c5
parent 98469 2433bf50d64ecb810c70640971c02b8c87e9000d
child 98471 eb110cad75a5a3dd07640d19a906160ec97ac50a
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewerssurkov
bugs758112
milestone16.0a1
bug 758112 - use mozilla::Selection in accessible/ r=surkov From 60355eaaee7ef9edbc475c355133e02bc7c1a89f Mon Sep 17 00:00:00 2001 --- accessible/src/generic/HyperTextAccessible.cpp | 106 +++++++++--------------- accessible/src/generic/RootAccessible.cpp | 1 - 2 files changed, 39 insertions(+), 68 deletions(-)
accessible/src/generic/HyperTextAccessible.cpp
accessible/src/generic/RootAccessible.cpp
--- a/accessible/src/generic/HyperTextAccessible.cpp
+++ b/accessible/src/generic/HyperTextAccessible.cpp
@@ -20,22 +20,22 @@
 #include "nsIEditingSession.h"
 #include "nsIEditor.h"
 #include "nsIFrame.h"
 #include "nsFrameSelection.h"
 #include "nsILineIterator.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsIPlaintextEditor.h"
 #include "nsIScrollableFrame.h"
-#include "nsISelectionPrivate.h"
 #include "nsIServiceManager.h"
 #include "nsTextFragment.h"
 #include "mozilla/Selection.h"
 #include "gfxSkipChars.h"
 
+using namespace mozilla;
 using namespace mozilla::a11y;
 
 ////////////////////////////////////////////////////////////////////////////////
 // HyperTextAccessible
 ////////////////////////////////////////////////////////////////////////////////
 
 HyperTextAccessible::
   HyperTextAccessible(nsIContent* aNode, DocAccessible* aDoc) :
@@ -1510,28 +1510,22 @@ HyperTextAccessible::SetSelectionRange(P
   // Set the selection
   SetSelectionBounds(0, aStartPos, aEndPos);
 
   // If range 0 was successfully set, clear any additional selection 
   // ranges remaining from previous selection
   nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
   NS_ENSURE_STATE(frameSelection);
 
-  nsCOMPtr<nsISelection> domSel =
+  Selection* domSel =
     frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
   NS_ENSURE_STATE(domSel);
 
-  PRInt32 numRanges = 0;
-  domSel->GetRangeCount(&numRanges);
-
-  for (PRInt32 count = 0; count < numRanges - 1; count ++) {
-    nsCOMPtr<nsIDOMRange> range;
-    domSel->GetRangeAt(1, getter_AddRefs(range));
-    domSel->RemoveRange(range);
-  }
+  for (PRInt32 idx = domSel->GetRangeCount() - 1; idx > 0; idx--)
+    domSel->RemoveRange(domSel->GetRangeAt(idx));
 
   // When selection is done, move the focus to the selection if accessible is
   // not focusable. That happens when selection is set within hypertext
   // accessible.
   if (isFocusable)
     return NS_OK;
 
   nsFocusManager* DOMFocusManager = nsFocusManager::GetFocusManager();
@@ -1580,31 +1574,25 @@ HyperTextAccessible::GetCaretOffset(PRIn
   if (focusDisp == FocusManager::eNone)
     return NS_OK;
 
   // Turn the focus node and offset of the selection into caret hypretext
   // offset.
   nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
   NS_ENSURE_STATE(frameSelection);
 
-  nsISelection* domSel =
+  Selection* domSel =
     frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
   NS_ENSURE_STATE(domSel);
 
-  nsCOMPtr<nsIDOMNode> focusDOMNode;
-  nsresult rv = domSel->GetFocusNode(getter_AddRefs(focusDOMNode));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  PRInt32 focusOffset;
-  rv = domSel->GetFocusOffset(&focusOffset);
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsINode* focusNode = domSel->GetFocusNode();
+  PRInt32 focusOffset = domSel->GetFocusOffset();
 
   // No caret if this DOM node is inside of focused node but the selection's
   // focus point is not inside of this DOM node.
-  nsCOMPtr<nsINode> focusNode(do_QueryInterface(focusDOMNode));
   if (focusDisp == FocusManager::eContainedByFocus) {
     nsINode *resultNode =
       nsCoreUtils::GetDOMNodeFromDOMPoint(focusNode, focusOffset);
 
     nsINode* thisNode = GetNode();
     if (resultNode != thisNode &&
         !nsCoreUtils::IsAncestorOf(thisNode, resultNode))
       return NS_OK;
@@ -1618,29 +1606,31 @@ PRInt32
 HyperTextAccessible::CaretLineNumber()
 {
   // Provide the line number for the caret, relative to the
   // currently focused node. Use a 1-based index
   nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
   if (!frameSelection)
     return -1;
 
-  nsISelection* domSel =
+  Selection* domSel =
     frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
   if (!domSel)
     return - 1;
 
-  nsCOMPtr<nsIDOMNode> caretNode;
-  domSel->GetFocusNode(getter_AddRefs(caretNode));
-  nsCOMPtr<nsIContent> caretContent = do_QueryInterface(caretNode);
-  if (!caretContent || !nsCoreUtils::IsAncestorOf(GetNode(), caretContent))
+  nsINode* caretNode = domSel->GetFocusNode();
+  if (!caretNode || !caretNode->IsContent())
     return -1;
 
-  PRInt32 caretOffset, returnOffsetUnused;
-  domSel->GetFocusOffset(&caretOffset);
+  nsIContent* caretContent = caretNode->AsContent();
+  if (!nsCoreUtils::IsAncestorOf(GetNode(), caretContent))
+    return -1;
+
+  PRInt32 returnOffsetUnused;
+  PRInt32 caretOffset = domSel->GetFocusOffset();
   nsFrameSelection::HINT hint = frameSelection->GetHint();
   nsIFrame *caretFrame = frameSelection->GetFrameForNodeOffset(caretContent, caretOffset,
                                                                hint, &returnOffsetUnused);
   NS_ENSURE_TRUE(caretFrame, -1);
 
   PRInt32 lineNumber = 1;
   nsAutoLineIterator lineIterForCaret;
   nsIContent *hyperTextContent = IsContent() ? mContent.get() : nsnull;
@@ -1691,35 +1681,34 @@ HyperTextAccessible::FrameSelection()
 void
 HyperTextAccessible::GetSelectionDOMRanges(PRInt16 aType,
                                            nsTArray<nsRange*>* aRanges)
 {
   nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
   if (!frameSelection)
     return;
 
-  nsISelection* domSel = frameSelection->GetSelection(aType);
+  Selection* domSel = frameSelection->GetSelection(aType);
   if (!domSel)
     return;
 
   nsCOMPtr<nsINode> startNode = GetNode();
 
   nsCOMPtr<nsIEditor> editor = GetEditor();
   if (editor) {
     nsCOMPtr<nsIDOMElement> editorRoot;
     editor->GetRootElement(getter_AddRefs(editorRoot));
     startNode = do_QueryInterface(editorRoot);
   }
 
   if (!startNode)
     return;
 
   PRUint32 childCount = startNode->GetChildCount();
-  nsCOMPtr<nsISelectionPrivate> privSel(do_QueryInterface(domSel));
-  nsresult rv = privSel->
+  nsresult rv = domSel->
     GetRangesForIntervalArray(startNode, 0, startNode, childCount, true, aRanges);
   NS_ENSURE_SUCCESS(rv,);
 
   // Remove collapsed ranges
   PRUint32 numRanges = aRanges->Length();
   for (PRUint32 idx = 0; idx < numRanges; idx ++) {
     if ((*aRanges)[idx]->Collapsed()) {
       aRanges->RemoveElementAt(idx);
@@ -1797,52 +1786,42 @@ HyperTextAccessible::GetSelectionBounds(
 /*
  * Changes the start and end offset of the specified selection.
  */
 NS_IMETHODIMP
 HyperTextAccessible::SetSelectionBounds(PRInt32 aSelectionNum,
                                         PRInt32 aStartOffset,
                                         PRInt32 aEndOffset)
 {
+  if (IsDefunct())
+    return NS_ERROR_FAILURE;
+
+  if (aSelectionNum < 0)
+    return NS_ERROR_INVALID_ARG;
+
   nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
   NS_ENSURE_STATE(frameSelection);
 
-  nsCOMPtr<nsISelection> domSel =
+  Selection* domSel =
     frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
   NS_ENSURE_STATE(domSel);
 
+  PRUint32 rangeCount = domSel->GetRangeCount();
+  if (rangeCount < static_cast<PRUint32>(aSelectionNum))
+    return NS_ERROR_INVALID_ARG;
+
   // Caret is a collapsed selection
   bool isOnlyCaret = (aStartOffset == aEndOffset);
-
-  PRInt32 rangeCount = 0;
-  domSel->GetRangeCount(&rangeCount);
-  nsCOMPtr<nsIDOMRange> range;
-  if (aSelectionNum == rangeCount) { // Add a range
+  nsRefPtr<nsRange> range;
+  if (aSelectionNum == rangeCount)
     range = new nsRange();
-  }
-  else if (aSelectionNum < 0 || aSelectionNum > rangeCount) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  else {
-    domSel->GetRangeAt(aSelectionNum, getter_AddRefs(range));
-    NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
-  }
+  else
+    range = domSel->GetRangeAt(aSelectionNum);
 
-  // XXX bug 758112 will make this nice, but for now just take the easy dumb approach
-  nsRefPtr<nsRange> tempRange = new nsRange();
-  nsresult rv = HypertextOffsetsToDOMRange(aStartOffset, aEndOffset, tempRange);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsCOMPtr<nsIDOMNode> startDOMNode = do_QueryInterface(tempRange->GetStartParent());
-  rv = range->SetStart(startDOMNode, tempRange->StartOffset());
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsCOMPtr<nsIDOMNode> endDOMNode = do_QueryInterface(tempRange->GetEndParent());
-  rv = isOnlyCaret ? range->Collapse(true) :
-                     range->SetEnd(endDOMNode, tempRange->EndOffset());
+  nsresult rv = HypertextOffsetsToDOMRange(aStartOffset, aEndOffset, range);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // If new range was created then add it, otherwise notify selection listeners
   // that existing selection range was changed.
   if (aSelectionNum == rangeCount)
     return domSel->AddRange(range);
 
   domSel->RemoveRange(range);
@@ -1854,47 +1833,40 @@ HyperTextAccessible::SetSelectionBounds(
  * Adds a selection bounded by the specified offsets.
  */
 NS_IMETHODIMP
 HyperTextAccessible::AddSelection(PRInt32 aStartOffset, PRInt32 aEndOffset)
 {
   nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
   NS_ENSURE_STATE(frameSelection);
 
-  nsCOMPtr<nsISelection> domSel =
+  Selection* domSel =
     frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
   NS_ENSURE_STATE(domSel);
 
-  PRInt32 rangeCount = 0;
-  domSel->GetRangeCount(&rangeCount);
-
-  return SetSelectionBounds(rangeCount, aStartOffset, aEndOffset);
+  return SetSelectionBounds(domSel->GetRangeCount(), aStartOffset, aEndOffset);
 }
 
 /*
  * Removes the specified selection.
  */
 NS_IMETHODIMP
 HyperTextAccessible::RemoveSelection(PRInt32 aSelectionNum)
 {
   nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
   NS_ENSURE_STATE(frameSelection);
 
-  nsCOMPtr<nsISelection> domSel =
+  Selection* domSel =
     frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL);
   NS_ENSURE_STATE(domSel);
 
-  PRInt32 rangeCount;
-  domSel->GetRangeCount(&rangeCount);
-  if (aSelectionNum < 0 || aSelectionNum >= rangeCount)
+  if (aSelectionNum < 0 || aSelectionNum >= domSel->GetRangeCount())
     return NS_ERROR_INVALID_ARG;
 
-  nsCOMPtr<nsIDOMRange> range;
-  domSel->GetRangeAt(aSelectionNum, getter_AddRefs(range));
-  return domSel->RemoveRange(range);
+  return domSel->RemoveRange(domSel->GetRangeAt(aSelectionNum));
 }
 
 // void nsIAccessibleText::
 //   scrollSubstringTo(in long startIndex, in long endIndex,
 //                     in unsigned long scrollType);
 NS_IMETHODIMP
 HyperTextAccessible::ScrollSubstringTo(PRInt32 aStartIndex, PRInt32 aEndIndex,
                                        PRUint32 aScrollType)
--- a/accessible/src/generic/RootAccessible.cpp
+++ b/accessible/src/generic/RootAccessible.cpp
@@ -39,17 +39,16 @@
 #include "nsIDOMDataContainerEvent.h"
 #include "nsIDOMNSEvent.h"
 #include "nsIDOMXULMultSelectCntrlEl.h"
 #include "nsIDOMXULPopupElement.h"
 #include "nsIDocument.h"
 #include "nsEventListenerManager.h"
 #include "nsIFrame.h"
 #include "nsIInterfaceRequestorUtils.h"
-#include "nsISelectionPrivate.h"
 #include "nsIServiceManager.h"
 #include "nsPIDOMWindow.h"
 #include "nsIWebBrowserChrome.h"
 #include "nsReadableUtils.h"
 #include "nsFocusManager.h"
 
 #ifdef MOZ_XUL
 #include "nsIXULDocument.h"