break; case eSelectLine : {  if (paintBuf != paintBufMem) { delete [] paintBuf; } if (ip != indicies) { delete [] ip; } return nsFrame::PeekOffset(aTracker, aDesiredX, aAmount, aDirection, aStartOffset, aResultContent, aContentOffset, aEatingWS); }
nsTextFrame::PeekOffset(nsIFocusTracker *aTracker,                         nscoord aDesiredX,                        nsSelectionAmount aAmount,                        nsDirection aDirection,                        PRInt32 aStartOffset,                        nsIContent **aResultContent,                         PRInt32 *aContentOffset,                        PRBool aEatingWS) {  if (!aResultContent || !aContentOffset || !mContent)    return NS_ERROR_NULL_POINTER;  if (aStartOffset < 0)    aStartOffset = mContentLength + mContentOffset;  if (aStartOffset < mContentOffset){    aStartOffset = mContentOffset;  }  if (aStartOffset > (mContentOffset + mContentLength)){    nsIFrame *nextInFlow;    nextInFlow = GetNextInFlow();    if (!nextInFlow){      NS_ASSERTION(PR_FALSE,"nsTextFrame::PeekOffset no more flow \n");      return NS_ERROR_INVALID_ARG;    }    return nextInFlow->PeekOffset(aTracker, aDesiredX, aAmount,aDirection,aStartOffset,                        aResultContent,aContentOffset,aEatingWS);  }  PRUnichar wordBufMem[WORD_BUF_SIZE];  PRUnichar paintBufMem[TEXT_BUF_SIZE];  PRInt32 indicies[TEXT_BUF_SIZE];  PRInt32* ip = indicies;  PRUnichar* paintBuf = paintBufMem;  if (mContentLength > TEXT_BUF_SIZE) {    ip = new PRInt32[mContentLength+1];    paintBuf = new PRUnichar[mContentLength];  }  PRInt32 textLength;  nsresult result(NS_OK);  switch (aAmount){  case eSelectNoAmount : {      *aResultContent = mContent;      if (*aResultContent)        (*aResultContent)->AddRef();      *aContentOffset = aStartOffset;    }    break;  case eSelectCharacter : {    // Transform text from content into renderable form    nsIDocument* doc;    result = mContent->GetDocument(doc);    if (NS_FAILED(result) || !doc)      return result;    nsCOMPtr<nsILineBreaker> lb;    doc->GetLineBreaker(getter_AddRefs(lb));    //nsCOMPtr<nsIWordBreaker> wb;    //doc->GetWordBreaker(getter_AddRefs(wb));    NS_RELEASE(doc);    nsTextTransformer tx(wordBufMem, WORD_BUF_SIZE,lb,nsnull);    PrepareUnicodeText(tx, ip, paintBuf, &textLength);    ip[mContentLength] = ip[mContentLength-1];    if ((ip[mContentLength]-mContentOffset) < textLength)//must set up last one for selection beyond edge if in boundary      ip[mContentLength]++;    nsIFrame *frameUsed = nsnull;    PRInt32 start;    PRBool found = PR_TRUE;    if (aDirection == eDirPrevious){      PRInt32 i;      for (i = aStartOffset -1 - mContentOffset; i >=0;  i--){        if (ip[i] < ip[aStartOffset - mContentOffset]){          *aContentOffset = i + mContentOffset;          break;        }      }      if (i <0){        found = PR_FALSE;        frameUsed = GetPrevInFlow();        start = mContentOffset;      }    }    else if (aDirection == eDirNext){      PRInt32 i;      for (i = aStartOffset +1 - mContentOffset; i <= mContentLength;  i++){        if (ip[i] > ip[aStartOffset - mContentOffset]){          *aContentOffset = i + mContentOffset;          break;        }      }/*      if (aStartOffset == 0 && (mFlags & TEXT_SKIP_LEADING_WS))        i--; //back up because we just skipped over some white space. why skip over the char also?        */      if (i > mContentLength){        found = PR_FALSE;        frameUsed = GetNextInFlow();        start = mContentOffset + mContentLength;      }    }    if (!found){      if (frameUsed){        result = frameUsed->PeekOffset(aTracker, aDesiredX, eSelectCharacter, aDirection,  start, aResultContent,               aContentOffset, aEatingWS);      }      else {//reached end ask the frame for help        result = nsFrame::PeekOffset(aTracker, aDesiredX, eSelectCharacter, aDirection, start, aResultContent,                  aContentOffset, aEatingWS);      }    }    else {      *aResultContent = mContent;      if (*aResultContent)        (*aResultContent)->AddRef();    }  }  break;  case eSelectWord : {    // Transform text from content into renderable form    nsIDocument* doc;    result = mContent->GetDocument(doc);    if (NS_FAILED(result) || !doc)      return result;    nsCOMPtr<nsILineBreaker> lb;    doc->GetLineBreaker(getter_AddRefs(lb));    nsCOMPtr<nsIWordBreaker> wb;    doc->GetWordBreaker(getter_AddRefs(wb));    NS_RELEASE(doc);    nsTextTransformer tx(wordBufMem, WORD_BUF_SIZE,lb,wb);    PrepareUnicodeText(tx, ip, paintBuf, &textLength);    nsIFrame *frameUsed = nsnull;    PRBool keepSearching; //if you run out of chars before you hit the end of word, maybe next frame has more text to select?    PRInt32 start;    PRBool found = PR_FALSE;    PRBool isWhitespace;    PRInt32 wordLen, contentLen;    if (aDirection == eDirPrevious){      keepSearching = PR_TRUE;      tx.Init(this, aStartOffset);      if (tx.GetPrevWord(PR_FALSE, wordLen, contentLen, isWhitespace, PR_FALSE)){        if ((aEatingWS && !isWhitespace) || !aEatingWS){          *aContentOffset = aStartOffset - contentLen;          //check for whitespace next.          if (*aContentOffset > mContentOffset)            keepSearching = PR_FALSE;//reached the beginning of a word          aEatingWS = !isWhitespace;//nowhite space, just eat chars.          while (isWhitespace && tx.GetPrevWord(PR_FALSE, wordLen, contentLen, isWhitespace, PR_FALSE)){            *aContentOffset -= contentLen;            aEatingWS = PR_FALSE;          }          keepSearching = *aContentOffset <= mContentOffset;          if (!isWhitespace){            if (!keepSearching)              found = PR_TRUE;            else              aEatingWS = PR_TRUE;          }        }        else {          *aContentOffset = mContentLength + mContentOffset;          found = PR_TRUE;        }      }      frameUsed = GetPrevInFlow();      start = -1; //start at end    }    else if (aDirection == eDirNext){      tx.Init(this, aStartOffset );#ifdef DEBUGWORDJUMPprintf("Next- Start=%d aEatingWS=%s\n", aStartOffset, aEatingWS ? "TRUE" : "FALSE");#endif      if (tx.GetNextWord(PR_FALSE, wordLen, contentLen, isWhitespace, PR_FALSE)){#ifdef DEBUGWORDJUMPprintf("GetNextWord return non null, wordLen%d, contentLen%d isWhitespace=%s\n",        wordLen, contentLen, isWhitespace ? "WS" : "NOT WS");#endif        if ((aEatingWS && isWhitespace) || !aEatingWS){          *aContentOffset = aStartOffset + contentLen;          //check for whitespace next.          keepSearching = PR_TRUE;          isWhitespace = PR_TRUE;          while (tx.GetNextWord(PR_FALSE, wordLen, contentLen, isWhitespace, PR_FALSE) && isWhitespace){#ifdef DEBUGWORDJUMPprintf("2-GetNextWord return non null, wordLen%d, contentLen%d isWhitespace=%s\n",        wordLen, contentLen, isWhitespace ? "WS" : "NOT WS");#endif            *aContentOffset += contentLen;            keepSearching = PR_FALSE;            isWhitespace = PR_FALSE;          }        }        else if (aEatingWS)          *aContentOffset = mContentOffset;        if (!isWhitespace){          found = PR_TRUE;          aEatingWS = PR_FALSE;        }        else if (!keepSearching) //we have found the "whole" word so just looking for WS          aEatingWS = PR_TRUE;      }      frameUsed = GetNextInFlow();      start = 0;    }#ifdef DEBUGWORDJUMPprintf("aEatingWS = %s\n" , aEatingWS ? "TRUE" : "FALSE");#endif    if (!found || (*aContentOffset > (mContentOffset + mContentLength)) || (*aContentOffset < mContentOffset)){ //gone too far      if (frameUsed){        result = frameUsed->PeekOffset(aTracker, aDesiredX, aAmount, aDirection,  start, aResultContent,               aContentOffset, aEatingWS);      }      else {//reached end ask the frame for help        result = nsFrame::PeekOffset(aTracker, aDesiredX, aAmount, aDirection, start, aResultContent,                  aContentOffset, aEatingWS);      }    }    else {      *aResultContent = mContent;      if (*aResultContent)        (*aResultContent)->AddRef();    }  }    break;  case eSelectLine :     {      // Cleanup      if (paintBuf != paintBufMem) {        delete [] paintBuf;      }      if (ip != indicies) {        delete [] ip;      }      return nsFrame::PeekOffset(aTracker, aDesiredX, aAmount, aDirection, aStartOffset,                        aResultContent, aContentOffset, aEatingWS);    }    break;  default: result = NS_ERROR_FAILURE; break;  }  // Cleanup  if (paintBuf != paintBufMem) {    delete [] paintBuf;  }  if (ip != indicies) {    delete [] ip;  }  if (NS_FAILED(result)){    *aResultContent = mContent;    if (*aResultContent)      (*aResultContent)->AddRef();    *aContentOffset = aStartOffset;    result = NS_OK;  }  return result;}