while (NS_SUCCEEDED(lastFrame->GetNextSibling(&lookahead)) && lookahead)
while ((lookahead = lastFrame->GetNextSibling()) != nsnull)
nsSelection::GetPrevNextBidiLevels(nsIPresContext *aPresContext,                                   nsIContent *aNode,                                   PRUint32 aContentOffset,                                   nsIFrame **aPrevFrame,                                   nsIFrame **aNextFrame,                                   PRUint8 *aPrevLevel,                                   PRUint8 *aNextLevel){  if (!aPrevFrame || !aNextFrame)    return NS_ERROR_NULL_POINTER;  // Get the level of the frames on each side  nsIFrame    *currentFrame;  PRInt32     currentOffset;  PRInt32     frameStart, frameEnd;  nsDirection direction;  nsresult    result;  *aPrevLevel = *aNextLevel = 0;  result = GetFrameForNodeOffset(aNode, aContentOffset, mHint, &currentFrame, &currentOffset);  if (NS_FAILED(result))    return result;  currentFrame->GetOffsets(frameStart, frameEnd);  if (0 == frameStart && 0 == frameEnd)    direction = eDirPrevious;  else if (frameStart == currentOffset)    direction = eDirPrevious;  else if (frameEnd == currentOffset)    direction = eDirNext;  else {    // we are neither at the beginning nor at the end of the frame, so we have no worries    *aPrevFrame = *aNextFrame = currentFrame;    currentFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::embeddingLevel,                                  (void**)aNextLevel, sizeof(*aNextLevel) );    *aPrevLevel = *aNextLevel;    return NS_OK;  }  /*  we have to find the next or previous *logical* frame.  Unfortunately |GetFrameFromDirection| has already been munged to return the next/previous *visual* frame, so we can't use that.  The following code is taken from there without the Bidi changes.  XXX is there a simpler way to do this?   */  nsIFrame *blockFrame = currentFrame;  nsIFrame *thisBlock;  PRInt32   thisLine;  nsCOMPtr<nsILineIteratorNavigator> it;   result = NS_ERROR_FAILURE;  while (NS_FAILED(result) && blockFrame)  {    thisBlock = blockFrame;    result = blockFrame->GetParent(&blockFrame);    if (NS_SUCCEEDED(result) && blockFrame) {      it = do_QueryInterface(blockFrame, &result);    }    else {      blockFrame = nsnull;    }  }  if (!blockFrame || !it)    return NS_ERROR_FAILURE;  result = it->FindLineContaining(thisBlock, &thisLine);  if (NS_FAILED(result))    return result;  if (thisLine < 0)     return NS_ERROR_FAILURE;  nsIFrame *firstFrame;  nsIFrame *lastFrame;  nsRect    nonUsedRect;  PRInt32   lineFrameCount;  PRUint32  lineFlags;  result = it->GetLine(thisLine, &firstFrame, &lineFrameCount,nonUsedRect,                       &lineFlags);  if (NS_FAILED(result))    return result;  lastFrame = firstFrame;  for (;lineFrameCount > 1;lineFrameCount --) {    result = lastFrame->GetNextSibling(&lastFrame);    if (NS_FAILED(result)){      NS_ASSERTION(0,"should not be reached nsFrame\n");      return NS_ERROR_FAILURE;    }  }  // GetFirstLeaf  nsIFrame *lookahead = nsnull;  while (1) {    result = firstFrame->FirstChild(aPresContext, nsnull, &lookahead);    if (NS_FAILED(result) || !lookahead)      break; //nothing to do    firstFrame = lookahead;  }  // GetLastLeaf  lookahead = nsnull;  while (1) {    result = lastFrame->FirstChild(aPresContext, nsnull, &lookahead);    if (NS_FAILED(result) || !lookahead)      break; //nothing to do    lastFrame = lookahead;    while (NS_SUCCEEDED(lastFrame->GetNextSibling(&lookahead)) && lookahead)      lastFrame = lookahead;  }  //END LINE DATA CODE  if (direction == eDirNext && lastFrame == currentFrame) { // End of line: set aPrevFrame to the current frame                                                            //              set aPrevLevel to the embedding level of the current frame                                                            //              set aNextFrame to null                                                            //              set aNextLevel to the paragraph embedding level    *aPrevFrame = currentFrame;    currentFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::embeddingLevel,                                  (void**)aPrevLevel, sizeof(*aPrevLevel) );    currentFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::baseLevel,                                  (void**)aNextLevel, sizeof(*aNextLevel) );    *aNextFrame = nsnull;    return NS_OK;  }  if (direction == eDirPrevious && firstFrame == currentFrame) { // Beginning of line: set aPrevFrame to null                                                                 //                    set aPrevLevel to the paragraph embedding level                                                                 //                    set aNextFrame to the current frame                                                                 //                    set aNextLevel to the embedding level of the current frame    *aNextFrame = currentFrame;    currentFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::embeddingLevel,                                  (void**)aNextLevel, sizeof(*aNextLevel) );    currentFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::baseLevel,                                  (void**)aPrevLevel, sizeof(*aPrevLevel) );    *aPrevFrame = nsnull;    return NS_OK;  }  // Find the adjacent frame  nsCOMPtr<nsIBidirectionalEnumerator> frameTraversal;  nsCOMPtr<nsIFrameTraversal> trav(do_CreateInstance(kFrameTraversalCID,&result));  if (NS_FAILED(result))    return result;  result = trav->NewFrameTraversal(getter_AddRefs(frameTraversal),LEAF, aPresContext, currentFrame);  if (NS_FAILED(result))    return result;  nsISupports *isupports = nsnull;  if (direction == eDirNext)    result = frameTraversal->Next();  else     result = frameTraversal->Prev();  if (NS_FAILED(result))    return result;  result = frameTraversal->CurrentItem(&isupports);  if (NS_FAILED(result))    return result;  if (!isupports)    return NS_ERROR_NULL_POINTER;  //we must CAST here to an nsIFrame. nsIFrame doesnt really follow the rules  //for speed reasons  nsIFrame *newFrame = (nsIFrame *)isupports;  if (direction == eDirNext) {    *aPrevFrame = currentFrame;    currentFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::embeddingLevel,                                  (void**)aPrevLevel, sizeof(*aPrevLevel) );    *aNextFrame = newFrame;    newFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::embeddingLevel,                              (void**)aNextLevel, sizeof(*aNextLevel) );  }  else {    *aNextFrame = currentFrame;    currentFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::embeddingLevel,                                  (void**)aNextLevel, sizeof(*aNextLevel) );    *aPrevFrame = newFrame;    newFrame->GetBidiProperty(aPresContext, nsLayoutAtoms::embeddingLevel,                              (void**)aPrevLevel, sizeof(*aPrevLevel) );  }  return NS_OK;}