result = frame->GetRect(*aRect);  if (NS_FAILED(result)) return result;
*aRect = frame->GetRect();
nsTypedSelection::GetSelectionRegionRectAndScrollableView(SelectionRegion aRegion, nsRect *aRect, nsIScrollableView **aScrollableView){  nsresult result = NS_OK;  if (!mFrameSelection)    return NS_ERROR_FAILURE;//nothing to do  if (!aRect || !aScrollableView)    return NS_ERROR_NULL_POINTER;  // Init aRect:  aRect->x = 0;  aRect->y = 0;  aRect->width  = 0;  aRect->height = 0;  *aScrollableView = 0;  nsIDOMNode *node       = 0;  PRInt32     nodeOffset = 0;  PRBool      isEndNode  = PR_TRUE;  nsIFrame   *frame      = 0;  switch (aRegion)  {  case nsISelectionController::SELECTION_ANCHOR_REGION:    node       = FetchAnchorNode();    nodeOffset = FetchAnchorOffset();    isEndNode  = GetDirection() == eDirPrevious;    break;  case nsISelectionController::SELECTION_FOCUS_REGION:    node       = FetchFocusNode();    nodeOffset = FetchFocusOffset();    isEndNode  = GetDirection() == eDirNext;    break;  default:    return NS_ERROR_FAILURE;  }  if (!node)    return NS_ERROR_NULL_POINTER;  nsCOMPtr<nsIContent> content = do_QueryInterface(node);  PRInt32 frameOffset = 0;  if (content)  {    nsIFrameSelection::HINT hint;    mFrameSelection->GetHint(&hint);    result = mFrameSelection->GetFrameForNodeOffset(content, nodeOffset, hint, &frame, &frameOffset);  }  else    result = NS_ERROR_FAILURE;  if(NS_FAILED(result))    return result;  if (!frame)    return NS_ERROR_NULL_POINTER;  //  // Get the frame's scrollable view.  //  nsCOMPtr<nsIPresContext> presContext;  result = GetPresContext(getter_AddRefs(presContext));  if (NS_FAILED(result))    return result;  if (!presContext)    return NS_ERROR_FAILURE;  nsIView *view = 0;  nsIFrame *parentWithView = 0;  result = frame->GetParentWithView(presContext, &parentWithView);  if (NS_FAILED(result))    return result;  if (!parentWithView)    return NS_ERROR_FAILURE;  view = parentWithView->GetView(presContext);  result = GetClosestScrollableView(view, aScrollableView);  if (NS_FAILED(result))    return result;  if (!*aScrollableView)    return NS_OK;  //  // Figure out what node type we have, then get the  // appropriate rect for it's nodeOffset.  //  PRUint16 nodeType = nsIDOMNode::ELEMENT_NODE;  result = node->GetNodeType(&nodeType);  if (NS_FAILED(result))    return NS_ERROR_NULL_POINTER;  if (nodeType == nsIDOMNode::TEXT_NODE)  {    nsIFrame *childFrame = 0;    frameOffset  = 0;    result = frame->GetChildFrameContainingOffset(nodeOffset, mFrameSelection->mHint, &frameOffset, &childFrame);    if (NS_FAILED(result))      return result;    if (!childFrame)      return NS_ERROR_NULL_POINTER;    frame = childFrame;    //    // Get the x coordinate of the offset into the text frame.    // The x coordinate will be relative to the frame's origin,    // so we'll have to translate it into the root view's coordinate    // system.    //    nsPoint pt;    result = GetCachedFrameOffset(frame, nodeOffset, pt);    if (NS_FAILED(result))      return result;        //    // Get the frame's rect.    //    result = frame->GetRect(*aRect);    if (NS_FAILED(result))      return result;    //    // Translate the frame's rect into root view coordinates.    //    result = GetFrameToScrolledViewOffsets(*aScrollableView, frame, &aRect->x, &aRect->y);    if (NS_FAILED(result))      return result;    //    // Now add the offset's x coordinate.    //    aRect->x += pt.x;    //    // Adjust the width of the rect to account for any neccessary    // padding!    //    const nsIView* clipView = 0;    result = (*aScrollableView)->GetClipView(&clipView);    if (NS_FAILED(result))      return result;    nsRect clipRect;    result = clipView->GetBounds(clipRect);    if (NS_FAILED(result))      return result;    result = (*aScrollableView)->GetScrollPosition(clipRect.x, clipRect.y);    if (NS_FAILED(result))      return result;    //    // If the point we are interested is outside the clip    // region, we will scroll it into view with a padding    // equal to a quarter of the clip's width.    //    PRInt32 pad = clipRect.width >> 2;    if (pad <= 0)      pad = 3; // Arbitrary    if (aRect->x >= clipRect.XMost())      aRect->width = pad;    else if (aRect->x <= clipRect.x)    {      aRect->x -= pad;      aRect->width = pad;    }    else      aRect->width = 60; // Arbitrary    //    // Clip the x dimensions of aRect so that they are    // completely within the bounds of the scrolledView.    // This helps avoid unnecessary scrolling of parent    // scrolled views.    //    // Note that aRect is in the coordinate system    // of the scrolledView, so there is no need to take    // into account scrolledView's x and y position.    // We can just assume that (0,0) corresponds to the    // upper left corner, and (svRect.width, svRect.height)    // the lower right corner of the scrolledView.    //    nsIView* scrolledView = 0;    result = (*aScrollableView)->GetScrolledView(scrolledView);    if (NS_FAILED(result))      return result;    nsRect svRect;    result = scrolledView->GetBounds(svRect);    if (NS_FAILED(result))      return result;    if (aRect->x < 0)      aRect->x = 0;    else if (aRect->x >= svRect.width)      aRect->x = svRect.width - 1;    if (aRect->XMost() > svRect.width)      aRect->width = svRect.width - aRect->x;  }  else  {    //    // Must be a non-text frame, just scroll the frame    // into view.    //    result = frame->GetRect(*aRect);    if (NS_FAILED(result))      return result;    result = GetFrameToScrolledViewOffsets(*aScrollableView, frame, &aRect->x, &aRect->y);  }  return result;}