printf ("Sel. Extend to %p %s %d\n", content, tagCString, aOffset);
PRINTF ("Sel. Extend to %p %s %d\n", content, tagCString, aOffset);
nsDOMSelection::Extend(nsIDOMNode* aParentNode, PRInt32 aOffset){  if (!aParentNode)    return NS_ERROR_INVALID_ARG;  // First, find the range containing the old focus point:  if (!mRangeArray || !mAnchorFocusRange)    return NS_ERROR_NOT_INITIALIZED;  //mFrameSelection->InvalidateDesiredX();  nsCOMPtr<nsIDOMRange> difRange;  nsresult res;  NS_NewRange(getter_AddRefs(difRange));  nsCOMPtr<nsIDOMRange> range;  if (FetchFocusNode() ==  aParentNode && FetchFocusOffset() == aOffset)    return NS_ERROR_FAILURE;//same node nothing to do!  res = mAnchorFocusRange->CloneRange(getter_AddRefs(range));  //range = mAnchorFocusRange;  nsCOMPtr<nsIDOMNode> startNode;  nsCOMPtr<nsIDOMNode> endNode;  PRInt32 startOffset;  PRInt32 endOffset;  range->GetStartContainer(getter_AddRefs(startNode));  range->GetEndContainer(getter_AddRefs(endNode));  range->GetStartOffset(&startOffset);  range->GetEndOffset(&endOffset);  nsDirection dir = GetDirection();  PRBool fixupState = PR_FALSE; //if there was a previous fixup the optimal drawing erasing will NOT work  if (NS_FAILED(res))    return res;  NS_NewRange(getter_AddRefs(difRange));  //compare anchor to old cursor.  if (NS_FAILED(res))    return res;  PRInt32 result1 = ComparePoints(FetchAnchorNode(), FetchAnchorOffset()                                   ,FetchFocusNode(), FetchFocusOffset());  //compare old cursor to new cursor  PRInt32 result2 = ComparePoints(FetchFocusNode(), FetchFocusOffset(),                            aParentNode, aOffset );  //compare anchor to new cursor  PRInt32 result3 = ComparePoints(FetchAnchorNode(), FetchAnchorOffset(),                            aParentNode , aOffset );  if (result2 == 0) //not selecting anywhere    return NS_OK;  nsCOMPtr<nsIPresContext>  presContext;  GetPresContext(getter_AddRefs(presContext));  if ((result1 == 0 && result3 < 0) || (result1 <= 0 && result2 < 0)){//a1,2  a,1,2    //select from 1 to 2 unless they are collapsed    res = range->SetEnd(aParentNode,aOffset);    if (NS_FAILED(res))      return res;    dir = eDirNext;    res = difRange->SetEnd(FetchEndParent(range), FetchEndOffset(range));    res |= difRange->SetStart(FetchFocusNode(), FetchFocusOffset());    if (NS_FAILED(res))      return res;#if OLD_SELECTION    res = FixupSelectionPoints(range, &dir, &fixupState);#endif    if (NS_FAILED(res))      return res;    if (fixupState)     {#if OLD_SELECTION      selectFrames(mAnchorFocusRange, PR_FALSE);      selectFrames(range, PR_TRUE);#endif    }    else{      selectFrames(presContext, difRange , PR_TRUE);    }    res = CopyRangeToAnchorFocus(range);    if (NS_FAILED(res))      return res;  }  else if (result1 == 0 && result3 > 0){//2, a1    //select from 2 to 1a    dir = eDirPrevious;    res = range->SetStart(aParentNode,aOffset);    if (NS_FAILED(res))      return res;#if OLD_SELECTION    res = FixupSelectionPoints(range, &dir, &fixupState);    if (NS_FAILED(res))      return res;    if (fixupState) //unselect previous and select new state has changed to not fixed up    {      selectFrames(mAnchorFocusRange, PR_FALSE);      selectFrames(range, PR_TRUE);    }    else#endif      selectFrames(presContext, range, PR_TRUE);    res = CopyRangeToAnchorFocus(range);    if (NS_FAILED(res))      return res;  }  else if (result3 <= 0 && result2 >= 0) {//a,2,1 or a2,1 or a,21 or a21    //deselect from 2 to 1    res = difRange->SetEnd(FetchFocusNode(), FetchFocusOffset());    res |= difRange->SetStart(aParentNode, aOffset);    if (NS_FAILED(res))      return res;    res = range->SetEnd(aParentNode,aOffset);    if (NS_FAILED(res))      return res;#if OLD_SELECTION        dir = eDirNext;    res = FixupSelectionPoints(range, &dir, &fixupState);#endif    if (NS_FAILED(res))      return res;    if (fixupState) //unselect previous and select new state has changed to not fixed up    {#if OLD_SELECTION          selectFrames(mAnchorFocusRange, PR_FALSE);      selectFrames(range, PR_TRUE);#endif    }    else     {      res = CopyRangeToAnchorFocus(range);      if (NS_FAILED(res))        return res;      selectFrames(presContext, difRange, 0);//deselect now if fixup succeeded      difRange->SetEnd(FetchEndParent(range),FetchEndOffset(range));      selectFrames(presContext, difRange, PR_TRUE);//must reselect last node maybe more if fixup did something    }  }  else if (result1 >= 0 && result3 <= 0) {//1,a,2 or 1a,2 or 1,a2 or 1a2    if (GetDirection() == eDirPrevious){      res = range->SetStart(endNode,endOffset);      if (NS_FAILED(res))        return res;    }    dir = eDirNext;    res = range->SetEnd(aParentNode,aOffset);    if (NS_FAILED(res))      return res;#if OLD_SELECTION    res = FixupSelectionPoints(range, &dir, &fixupState);    if (NS_FAILED(res))      return res;    if (fixupState) //unselect previous and select new state has changed to not fixed up    {      selectFrames(mAnchorFocusRange, PR_FALSE);      selectFrames(range, PR_TRUE);    }    else #endif    {      if (FetchFocusNode() != FetchAnchorNode() || FetchFocusOffset() != FetchAnchorOffset() ){//if collapsed diff dont do anything        res = difRange->SetStart(FetchFocusNode(), FetchFocusOffset());        res |= difRange->SetEnd(FetchAnchorNode(), FetchAnchorOffset());        if (NS_FAILED(res))          return res;        res = CopyRangeToAnchorFocus(range);        if (NS_FAILED(res))          return res;        //deselect from 1 to a        selectFrames(presContext, difRange , PR_FALSE);      }      else      {        res = CopyRangeToAnchorFocus(range);        if (NS_FAILED(res))          return res;      }      //select from a to 2      selectFrames(presContext, range , PR_TRUE);    }  }  else if (result2 <= 0 && result3 >= 0) {//1,2,a or 12,a or 1,2a or 12a    //deselect from 1 to 2    res = difRange->SetEnd(aParentNode, aOffset);    res |= difRange->SetStart(FetchFocusNode(), FetchFocusOffset());    if (NS_FAILED(res))      return res;    dir = eDirPrevious;    res = range->SetStart(aParentNode,aOffset);    if (NS_FAILED(res))      return res;#if OLD_SELECTION    res = FixupSelectionPoints(range, &dir, &fixupState);#endif    if (NS_FAILED(res))      return res;    if (fixupState) //unselect previous and select new state has changed to not fixed up    {#if OLD_SELECTION      selectFrames(mAnchorFocusRange, PR_FALSE);      selectFrames(range, PR_TRUE);#endif    }    else     {      res = CopyRangeToAnchorFocus(range);      if (NS_FAILED(res))        return res;      selectFrames(presContext, difRange , PR_FALSE);      difRange->SetStart(FetchStartParent(range),FetchStartOffset(range));      selectFrames(presContext, difRange, PR_TRUE);//must reselect last node    }  }  else if (result3 >= 0 && result1 <= 0) {//2,a,1 or 2a,1 or 2,a1 or 2a1    if (GetDirection() == eDirNext){      range->SetEnd(startNode,startOffset);    }    dir = eDirPrevious;    res = range->SetStart(aParentNode,aOffset);    if (NS_FAILED(res))      return res;#if OLD_SELECTION    res = FixupSelectionPoints(range, &dir, &fixupState);    if (NS_FAILED(res))      return res;    if (fixupState) //unselect previous and select new state has changed to not fixed up    {      selectFrames(mAnchorFocusRange, PR_FALSE);      selectFrames(range, PR_TRUE);    }    else#endif    {      //deselect from a to 1      if (FetchFocusNode() != FetchAnchorNode() || FetchFocusOffset() != FetchAnchorOffset() ){//if collapsed diff dont do anything        res = difRange->SetStart(FetchAnchorNode(), FetchAnchorOffset());        res |= difRange->SetEnd(FetchFocusNode(), FetchFocusOffset());        res = CopyRangeToAnchorFocus(range);        if (NS_FAILED(res))          return res;        selectFrames(presContext, difRange, 0);      }      else      {        res = CopyRangeToAnchorFocus(range);        if (NS_FAILED(res))          return res;      }      //select from 2 to a      selectFrames(presContext, range , PR_TRUE);    }  }  else if (result2 >= 0 && result1 >= 0) {//2,1,a or 21,a or 2,1a or 21a    //select from 2 to 1    res = range->SetStart(aParentNode,aOffset);    if (NS_FAILED(res))      return res;    dir = eDirPrevious;    res = difRange->SetEnd(FetchFocusNode(), FetchFocusOffset());    res |= difRange->SetStart(FetchStartParent(range), FetchStartOffset(range));    if (NS_FAILED(res))      return res;#if OLD_SELECTION    res = FixupSelectionPoints(range, &dir, &fixupState);#endif    if (NS_FAILED(res))      return res;    if (fixupState) //unselect previous and select new state has changed to not fixed up    {#if OLD_SELECTION      selectFrames(mAnchorFocusRange, PR_FALSE);      selectFrames(range, PR_TRUE);#endif    }    else {      selectFrames(presContext, difRange, PR_TRUE);    }    res = CopyRangeToAnchorFocus(range);    if (NS_FAILED(res))      return res;  }  DEBUG_OUT_RANGE(range);#if 0  if (eDirNext == mDirection)    printf("    direction = 1  LEFT TO RIGHT\n");  else    printf("    direction = 0  RIGHT TO LEFT\n");#endif  SetDirection(dir);#ifdef DEBUG_SELECTION  if (aParentNode)  {    nsCOMPtr<nsIContent>content;    content = do_QueryInterface(aParentNode);    nsIAtom *tag;    content->GetTag(tag);    if (tag)    {	    nsAutoString tagString;	    tag->ToString(tagString);	    char * tagCString = tagString.ToNewCString();	    printf ("Sel. Extend to %p %s %d\n", content, tagCString, aOffset);	    delete [] tagCString;    }  }  else {    printf ("Sel. Extend set to null parent.\n");  }#endif  if (!mFrameSelection)    return NS_OK;//nothing to do  return mFrameSelection->NotifySelectionListeners(GetType());}