nsCOMPtr<nsIContent> content; result = frame->GetContent(getter_AddRefs(content)); nsCOMPtr<nsIDOMNode> node = do_QueryInterface(content);
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(frame->GetContent());
nsSelection::MoveCaret(PRUint32 aKeycode, PRBool aContinue, nsSelectionAmount aAmount){  nsCOMPtr<nsIPresContext> context;  nsresult result = mTracker->GetPresContext(getter_AddRefs(context));  if (NS_FAILED(result) || !context)    return result?result:NS_ERROR_FAILURE;  nsCOMPtr<nsIDOMNode> weakNodeUsed;  PRInt32 offsetused = 0;  PRBool isCollapsed;  nscoord desiredX = 0; //we must keep this around and revalidate it when its just UP/DOWN  PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);  result = mDomSelections[index]->GetIsCollapsed(&isCollapsed);  if (NS_FAILED(result))    return result;  if (aKeycode == nsIDOMKeyEvent::DOM_VK_UP || aKeycode == nsIDOMKeyEvent::DOM_VK_DOWN)  {    result = FetchDesiredX(desiredX);    if (NS_FAILED(result))      return result;    SetDesiredX(desiredX);  }  if (!isCollapsed && !aContinue) {    switch (aKeycode){      case nsIDOMKeyEvent::DOM_VK_LEFT  :       case nsIDOMKeyEvent::DOM_VK_UP    : {          if ((mDomSelections[index]->GetDirection() == eDirPrevious)) { //f,a            offsetused = mDomSelections[index]->FetchFocusOffset();            weakNodeUsed = mDomSelections[index]->FetchFocusNode();          }          else {            offsetused = mDomSelections[index]->FetchAnchorOffset();            weakNodeUsed = mDomSelections[index]->FetchAnchorNode();          }          result = mDomSelections[index]->Collapse(weakNodeUsed,offsetused);          mDomSelections[index]->ScrollIntoView();          mHint = HINTRIGHT;          return NS_OK;         } break;      case nsIDOMKeyEvent::DOM_VK_RIGHT :       case nsIDOMKeyEvent::DOM_VK_DOWN  : {          if ((mDomSelections[index]->GetDirection() == eDirPrevious)) { //f,a            offsetused = mDomSelections[index]->FetchAnchorOffset();            weakNodeUsed = mDomSelections[index]->FetchAnchorNode();          }          else {            offsetused = mDomSelections[index]->FetchFocusOffset();            weakNodeUsed = mDomSelections[index]->FetchFocusNode();          }          result = mDomSelections[index]->Collapse(weakNodeUsed,offsetused);          mDomSelections[index]->ScrollIntoView();          mHint = HINTLEFT;          return NS_OK;         } break;          }//      if (keyEvent->keyCode == nsIDOMKeyEvent::DOM_VK_UP || keyEvent->keyCode == nsIDOMKeyEvent::DOM_VK_DOWN)//        SetDesiredX(desiredX);  }  nsCOMPtr<nsICaret> caret;  nsCOMPtr<nsIPresShell> shell;  result = context->GetShell(getter_AddRefs(shell));  if (NS_FAILED(result) || !shell)    return 0;  result = shell->GetCaret(getter_AddRefs(caret));  if (NS_FAILED(result) || !caret)    return 0;  offsetused = mDomSelections[index]->FetchFocusOffset();  weakNodeUsed = mDomSelections[index]->FetchFocusNode();      nsIFrame *frame;  result = mDomSelections[index]->GetPrimaryFrameForFocusNode(&frame, &offsetused);  if (NS_FAILED(result) || !frame)    return result?result:NS_ERROR_FAILURE;  nsCOMPtr<nsIContent> content;  result = frame->GetContent(getter_AddRefs(content));  nsCOMPtr<nsIDOMNode> node = do_QueryInterface(content);  nsCOMPtr<nsIDOMNode> parentNode;  nsPeekOffsetStruct pos;  //set data using mLimiter to stop on scroll views.  If we have a limiter then we stop peeking  //when we hit scrollable views.  If no limiter then just let it go ahead  pos.SetData(mTracker, desiredX, aAmount, eDirPrevious, offsetused, PR_FALSE,              PR_TRUE, PR_TRUE, mLimiter != nsnull, PR_TRUE);  HINT tHint(mHint); //temporary variable so we dont set mHint until it is necessary  switch (aKeycode){    case nsIDOMKeyEvent::DOM_VK_RIGHT :         InvalidateDesiredX();        pos.mDirection = eDirNext;        tHint = HINTLEFT;//stick to this line        PostReason(nsISelectionListener::KEYPRESS_REASON);      break;    case nsIDOMKeyEvent::DOM_VK_LEFT  : //no break        InvalidateDesiredX();        tHint = HINTRIGHT;//stick to opposite of movement        PostReason(nsISelectionListener::KEYPRESS_REASON);      break;    case nsIDOMKeyEvent::DOM_VK_DOWN :         pos.mAmount = eSelectLine;        pos.mDirection = eDirNext;//no break here        PostReason(nsISelectionListener::KEYPRESS_REASON);      break;    case nsIDOMKeyEvent::DOM_VK_UP :         pos.mAmount = eSelectLine;        PostReason(nsISelectionListener::KEYPRESS_REASON);      break;    case nsIDOMKeyEvent::DOM_VK_HOME :        InvalidateDesiredX();        pos.mAmount = eSelectBeginLine;        tHint = HINTRIGHT;//stick to opposite of movement        PostReason(nsISelectionListener::KEYPRESS_REASON);      break;    case nsIDOMKeyEvent::DOM_VK_END :        InvalidateDesiredX();        pos.mAmount = eSelectEndLine;        tHint = HINTLEFT;//stick to this line        PostReason(nsISelectionListener::KEYPRESS_REASON);     break;  default :return NS_ERROR_FAILURE;  }  pos.mPreferLeft = tHint;  if (NS_SUCCEEDED(result) && NS_SUCCEEDED(result = frame->PeekOffset(context, &pos)) && pos.mResultContent)  {    tHint = (HINT)pos.mPreferLeft;    PRBool bidiEnabled = PR_FALSE;    context->GetBidiEnabled(&bidiEnabled);    if (bidiEnabled)    {      nsIFrame *theFrame;      PRInt32 currentOffset, frameStart, frameEnd;      PRUint8 level;      // XXX - I expected to be able to use pos.mResultFrame, but when we move from frame to frame      //       and |PeekOffset| is called recursively, pos.mResultFrame on exit is sometimes set to the original      //       frame, not the frame that we ended up in, so I need this call to |GetFrameForNodeOffset|.      //       I don't know if that could or should be changed or if it would break something else.      GetFrameForNodeOffset(pos.mResultContent, pos.mContentOffset, tHint, &theFrame, &currentOffset);      theFrame->GetOffsets(frameStart, frameEnd);      // the hint might have been reversed by an RTL frame, so make sure of it      if (nsIDOMKeyEvent::DOM_VK_HOME == aKeycode)        pos.mPreferLeft = PR_TRUE;      else if (nsIDOMKeyEvent::DOM_VK_END == aKeycode)        pos.mPreferLeft = PR_FALSE;      tHint = (HINT)pos.mPreferLeft;      if (frameStart !=0 || frameEnd !=0) // Otherwise the frame is not a text frame, so nothing more to do      {        switch (aKeycode) {          case nsIDOMKeyEvent::DOM_VK_HOME:          case nsIDOMKeyEvent::DOM_VK_END:            // force the offset to the logical beginning (for HOME) or end (for END) of the frame            // (if it is an RTL frame it will be at the visual beginning or end, which we don't want in this case)            if (nsIDOMKeyEvent::DOM_VK_HOME == aKeycode)              pos.mContentOffset = frameStart;            else              pos.mContentOffset = frameEnd;            // set the cursor Bidi level to the paragraph embedding level            theFrame->GetBidiProperty(context, nsLayoutAtoms::baseLevel, (void**)&level,                                      sizeof(level) );            shell->SetCaretBidiLevel(level);            break;          default:            // If the current position is not a frame boundary, it's enough just to take the Bidi level of the current frame            if ((pos.mContentOffset != frameStart && pos.mContentOffset != frameEnd)                || (eSelectDir == aAmount)                || (eSelectLine == aAmount))            {              theFrame->GetBidiProperty(context, nsLayoutAtoms::embeddingLevel, (void**)&level,                                        sizeof(level) );              shell->SetCaretBidiLevel(level);            }            else              BidiLevelFromMove(context, shell, pos.mResultContent, pos.mContentOffset, aKeycode);        }      }#ifdef VISUALSELECTION      // Handle visual selection      if (aContinue)      {        result = VisualSelectFrames(context, theFrame, pos);        if (NS_FAILED(result)) // Back out by collapsing the selection to the current position          result = TakeFocus(pos.mResultContent, pos.mContentOffset, pos.mContentOffset, PR_FALSE, PR_FALSE);      }          else        result = TakeFocus(pos.mResultContent, pos.mContentOffset, pos.mContentOffset, aContinue, PR_FALSE);    }    else#else    }#endif // VISUALSELECTION    result = TakeFocus(pos.mResultContent, pos.mContentOffset, pos.mContentOffset, aContinue, PR_FALSE);  }  if (NS_SUCCEEDED(result))  {    mHint = tHint; //save the hint parameter now for the next time    result = mDomSelections[index]->ScrollIntoView();  }  return result;}