Invalidate(aPresContext, rect, PR_FALSE);
if (!rect.IsEmpty()) Invalidate(aPresContext, rect, PR_FALSE);
nsTextFrame::SetSelected(nsIPresContext* aPresContext,                         nsIDOMRange *aRange,                         PRBool aSelected,                         nsSpread aSpread){  nsresult result;  if (aSelected && ParentDisablesSelection())    return NS_OK;  nsFrameState  frameState;  GetFrameState(&frameState);#if 0  PRBool isSelected = ((frameState & NS_FRAME_SELECTED_CONTENT) == NS_FRAME_SELECTED_CONTENT);  if (!aSelected && !isSelected) //already set thanks  {    return NS_OK;  }#endif  // check whether style allows selection  PRBool selectable;  IsSelectable(&selectable, nsnull);  if (!selectable)    return NS_OK;//do not continue no selection for this frame.  PRBool found = PR_FALSE;  PRBool wholeContentFound = PR_FALSE;//if the entire content we look at is selected.  if (aRange) {    //lets see if the range contains us, if so we must redraw!    nsCOMPtr<nsIDOMNode> endNode;    PRInt32 endOffset;    nsCOMPtr<nsIDOMNode> startNode;    PRInt32 startOffset;    aRange->GetEndContainer(getter_AddRefs(endNode));    aRange->GetEndOffset(&endOffset);    aRange->GetStartContainer(getter_AddRefs(startNode));    aRange->GetStartOffset(&startOffset);    nsCOMPtr<nsIContent> content;    result = GetContent(getter_AddRefs(content));    nsCOMPtr<nsIDOMNode> thisNode;    thisNode = do_QueryInterface(content);    if (thisNode == startNode)    {      if ((mContentOffset + mContentLength) >= startOffset)      {        found = PR_TRUE;        if (thisNode == endNode)        { //special case          if (endOffset == startOffset) //no need to redraw since drawing takes place with cursor            found = PR_FALSE;          if (mContentOffset > endOffset)            found = PR_FALSE;        }      }    }    else if (thisNode == endNode)    {      if (mContentOffset < endOffset)        found = PR_TRUE;      else      {        found = PR_FALSE;        wholeContentFound = PR_TRUE;      }    }    else    {      found = PR_TRUE;    }  }  else {    if ( aSelected != (PRBool)(frameState | NS_FRAME_SELECTED_CONTENT) ){      found = PR_TRUE;    }  }  if ( aSelected )    frameState |=  NS_FRAME_SELECTED_CONTENT;  else  {//we need to see if any other selection available.    SelectionDetails *details = nsnull;    nsCOMPtr<nsIPresShell> shell;    nsCOMPtr<nsIFrameSelection> frameSelection;    nsresult rv = aPresContext->GetShell(getter_AddRefs(shell));    if (NS_SUCCEEDED(rv) && shell){      nsCOMPtr<nsISelectionController> selCon;      rv = GetSelectionController(aPresContext, getter_AddRefs(selCon));      if (NS_SUCCEEDED(rv) && selCon)      {        frameSelection = do_QueryInterface(selCon); //this MAY implement      }      if (!frameSelection)        rv = shell->GetFrameSelection(getter_AddRefs(frameSelection));      if (NS_SUCCEEDED(rv) && frameSelection){        nsCOMPtr<nsIContent> content;        PRInt32 offset;        PRInt32 length;        rv = GetContentAndOffsetsForSelection(aPresContext, getter_AddRefs(content),&offset,&length);        if (NS_SUCCEEDED(rv) && content){          rv = frameSelection->LookUpSelection(content, offset,                                length , &details, PR_TRUE);// PR_TRUE last param used here! we need to see if we are still selected. so no shortcut        }      }    }    if (!details)      frameState &= ~NS_FRAME_SELECTED_CONTENT;    else    {      SelectionDetails *sdptr = details;      while ((sdptr = details->mNext) != nsnull) {        delete details;        details = sdptr;      }      delete details;    }  }  SetFrameState(frameState);  if (found){ //if range contains this frame...    nsRect frameRect;    GetRect(frameRect);    nsRect rect(0, 0, frameRect.width, frameRect.height);    Invalidate(aPresContext, rect, PR_FALSE);//    ForceDrawFrame(this);  }  if (aSpread == eSpreadDown)  {    nsIFrame *frame;    GetPrevInFlow(&frame);    while(frame){      frame->SetSelected(aPresContext, aRange,aSelected,eSpreadNone);      result = frame->GetPrevInFlow(&frame);      if (NS_FAILED(result))        break;    }    GetNextInFlow(&frame);    while (frame){      frame->SetSelected(aPresContext, aRange,aSelected,eSpreadNone);      result = frame->GetNextInFlow(&frame);      if (NS_FAILED(result))        break;    }#ifdef IBMBIDI    GetBidiProperty(aPresContext, nsLayoutAtoms::nextBidi, (void**) &frame,sizeof(frame));    if (frame) {      frame->SetSelected(aPresContext, aRange, aSelected, aSpread);    }#endif // IBMBIDI  }  return NS_OK;}