if (NS_FAILED(result)) return result;
if (NS_FAILED(result) || !tempNode2) return NS_ERROR_FAILURE;
nsRangeList::FixupSelectionPoints(nsIDOMRange *aRange , nsDirection *aDir, PRBool *aFixupState, SelectionType aType){  if (!aRange || !aFixupState)    return NS_ERROR_NULL_POINTER;  *aFixupState = PR_FALSE;  nsresult res;  //startNode is the beginning or "anchor" of the range  //end Node is the end or "focus of the range  nsCOMPtr<nsIDOMNode> startNode;  nsCOMPtr<nsIDOMNode> endNode;  PRInt32 startOffset;  PRInt32 endOffset;  nsresult result;  if (*aDir == eDirNext)  {    if (NS_FAILED(GetOriginalAnchorPoint(getter_AddRefs(startNode), &startOffset, aType)))    {      aRange->GetStartParent(getter_AddRefs(startNode));      aRange->GetStartOffset(&startOffset);    }    aRange->GetEndParent(getter_AddRefs(endNode));    aRange->GetEndOffset(&endOffset);  }  else  {    if (NS_FAILED(GetOriginalAnchorPoint(getter_AddRefs(startNode), &startOffset,aType)))    {      aRange->GetEndParent(getter_AddRefs(startNode));      aRange->GetEndOffset(&startOffset);    }    aRange->GetStartParent(getter_AddRefs(endNode));    aRange->GetStartOffset(&endOffset);  }  if (!startNode || !endNode)    return NS_ERROR_FAILURE;  // if end node is a tbody then all bets are off we cannot select "rows"  nsCOMPtr<nsIAtom> atom;  atom = GetTag(endNode);  if (atom.get() == sTbodyAtom)    return NS_ERROR_FAILURE; //cannot select INTO row node ony cells  //get common parent  nsCOMPtr<nsIDOMNode> parent;  res = aRange->GetCommonParent(getter_AddRefs(parent));  if (NS_FAILED(res) || !parent)    return res;   //look for dest. if you see a cell you are in "cell mode"  //if you see a table you select "whole" table  //src first   nsCOMPtr<nsIDOMNode> tempNode;  nsCOMPtr<nsIDOMNode> tempNode2;  PRBool cellMode = PR_FALSE;  PRBool dirty = PR_FALSE;  if (startNode != endNode)  {    if (parent != startNode)    {      result = startNode->GetParentNode(getter_AddRefs(tempNode));      if (NS_FAILED(result))        return result;      while (tempNode != parent)      {        atom = GetTag(tempNode);        if (atom.get() == sTableAtom) //select whole table  if in cell mode, wait for cell        {          result = ParentOffset(tempNode, getter_AddRefs(startNode), &startOffset);          if (NS_FAILED(result))            return result;          if (*aDir == eDirPrevious) //select after            startOffset++;          dirty = PR_TRUE;          cellMode = PR_FALSE;        }        else if (atom.get() == sCellAtom) //you are in "cell" mode put selection to end of cell        {          cellMode = PR_TRUE;          result = ParentOffset(tempNode, getter_AddRefs(startNode), &startOffset);          if (NS_FAILED(result))            return result;          if (*aDir == eDirPrevious) //select after            startOffset++;          dirty = PR_TRUE;        }        result = tempNode->GetParentNode(getter_AddRefs(tempNode2));        if (NS_FAILED(result))          return result;        tempNode = tempNode2;      }    }    //now for dest node    if (parent != endNode)    {      result = endNode->GetParentNode(getter_AddRefs(tempNode));      PRBool found = !cellMode;      if (NS_FAILED(result))        return result;      while (tempNode != parent)      {        atom = GetTag(tempNode);        if (atom.get() == sTableAtom) //select whole table  if in cell mode, wait for cell        {          if (!cellMode)          {            result = ParentOffset(tempNode, getter_AddRefs(endNode), &endOffset);            if (NS_FAILED(result))              return result;            if (*aDir == eDirNext) //select after              endOffset++;            dirty = PR_TRUE;          }          else            found = PR_FALSE; //didnt find the right cell yet        }        else if (atom.get() == sCellAtom) //you are in "cell" mode put selection to end of cell        {          result = ParentOffset(tempNode, getter_AddRefs(endNode), &endOffset);          if (NS_FAILED(result))            return result;          if (*aDir == eDirNext) //select after            endOffset++;          found = PR_TRUE;          dirty = PR_TRUE;        }        result = tempNode->GetParentNode(getter_AddRefs(tempNode2));        if (NS_FAILED(result))          return result;        tempNode = tempNode2;      }      if (!found)        return NS_ERROR_FAILURE;    }  }  if (FetchAnchorNode(aType) != startNode.get() || startOffset != FetchAnchorOffset(aType))    dirty = PR_TRUE; //something has changed we are dirty no matter what  if (dirty && *aDir != mDirection[aType]) //fixup took place but new direction all bets are off  {    *aFixupState = PR_TRUE;    mFixupState = PR_FALSE;  }  else    if (startNode.get() != FetchOriginalAnchorNode(aType) && PR_TRUE == mFixupState) //no longer a fixup    {      *aFixupState = PR_TRUE;      mFixupState = PR_FALSE;    }    else    {      mFixupState = dirty;      *aFixupState = dirty;    }  if (dirty){    if (*aDir == eDirNext)    {      if (NS_FAILED(aRange->SetStart(startNode,startOffset)) || NS_FAILED(aRange->SetEnd(endNode, endOffset)))      {        *aDir = eDirPrevious;        aRange->SetStart(endNode, endOffset);        aRange->SetEnd(startNode, startOffset);      }    }    else    {      if (NS_FAILED(aRange->SetStart(endNode,endOffset)) || NS_FAILED(aRange->SetEnd(startNode, startOffset)))      {        *aDir = eDirNext;        aRange->SetStart(startNode, startOffset);        aRange->SetEnd(endNode, endOffset);      }    }  }  return NS_OK;}