PresShell::ScrollFrameIntoView(nsIFrame *aFrame, PRIntn   aVPercent, PRIntn   aHPercent) const { nsresult rv = NS_OK; if (!aFrame) {
PresShell::ScrollFrameIntoView(nsIFrame *aFrame){ if (!aFrame)
PresShell::ScrollFrameIntoView(nsIFrame *aFrame,                               PRIntn   aVPercent,                                PRIntn   aHPercent) const{  nsresult rv = NS_OK;  if (!aFrame) {    return NS_ERROR_NULL_POINTER;  }  if (mViewManager) {    // Get the viewport scroller    nsIScrollableView* scrollingView;    mViewManager->GetRootScrollableView(&scrollingView);    if (scrollingView) {      nsIView*  scrolledView;      nsPoint   offset;      nsIView*  closestView;                // Determine the offset from aFrame to the scrolled view. We do that by      // getting the offset from its closest view and then walking up      scrollingView->GetScrolledView(scrolledView);      aFrame->GetOffsetFromView(offset, &closestView);      // XXX Deal with the case where there is a scrolled element, e.g., a      // DIV in the middle...      while ((closestView != nsnull) && (closestView != scrolledView)) {        nscoord x, y;        // Update the offset        closestView->GetPosition(&x, &y);        offset.MoveBy(x, y);        // Get its parent view        closestView->GetParent(closestView);      }      // Determine the visible rect in the scrolled view's coordinate space.      // The size of the visible area is the clip view size      const nsIView*  clipView;      nsRect          visibleRect;      scrollingView->GetScrollPosition(visibleRect.x, visibleRect.y);      scrollingView->GetClipView(&clipView);      clipView->GetDimensions(&visibleRect.width, &visibleRect.height);      // The actual scroll offsets      nscoord scrollOffsetX = visibleRect.x;      nscoord scrollOffsetY = visibleRect.y;      // The frame's bounds in the coordinate space of the scrolled frame      nsRect  frameBounds;      aFrame->GetRect(frameBounds);      frameBounds.x = offset.x;      frameBounds.y = offset.y;      // See how the frame should be positioned vertically      if (NS_PRESSHELL_SCROLL_ANYWHERE == aVPercent) {        // The caller doesn't care where the frame is positioned vertically,        // so long as it's fully visible        if (frameBounds.y < visibleRect.y) {          // Scroll up so the frame's top edge is visible          scrollOffsetY = frameBounds.y;        } else if (frameBounds.YMost() > visibleRect.YMost()) {          // Scroll down so the frame's bottom edge is visible. Make sure the          // frame's top edge is still visible          scrollOffsetY += frameBounds.YMost() - visibleRect.YMost();          if (scrollOffsetY > frameBounds.y) {            scrollOffsetY = frameBounds.y;          }        }      } else {        // Align the frame edge according to the specified percentage        nscoord frameAlignY = frameBounds.y + (frameBounds.height * aVPercent) / 100;        scrollOffsetY = frameAlignY - (visibleRect.height * aVPercent) / 100;      }      // See how the frame should be positioned horizontally      if (NS_PRESSHELL_SCROLL_ANYWHERE == aHPercent) {        // The caller doesn't care where the frame is positioned horizontally,        // so long as it's fully visible        if (frameBounds.x < visibleRect.x) {          // Scroll left so the frame's left edge is visible          scrollOffsetX = frameBounds.x;        } else if (frameBounds.XMost() > visibleRect.XMost()) {          // Scroll right so the frame's right edge is visible. Make sure the          // frame's left edge is still visible          scrollOffsetX += frameBounds.XMost() - visibleRect.XMost();          if (scrollOffsetX > frameBounds.x) {            scrollOffsetX = frameBounds.x;          }        }            } else {        // Align the frame edge according to the specified percentage        nscoord frameAlignX = frameBounds.x + (frameBounds.width * aHPercent) / 100;        scrollOffsetX = frameAlignX - (visibleRect.width * aHPercent) / 100;      }            scrollingView->ScrollTo(scrollOffsetX, scrollOffsetY, NS_VMREFRESH_IMMEDIATE);    }  }  return rv;}