endOffset = startOffset+1;
if (*aDir == eDirPrevious) startOffset++;
nsDOMSelection::FixupSelectionPoints(nsIDOMRange *aRange , nsDirection *aDir, PRBool *aFixupState){  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)  {    aRange->GetStartParent(getter_AddRefs(startNode));    aRange->GetStartOffset(&startOffset);    aRange->GetEndParent(getter_AddRefs(endNode));    aRange->GetEndOffset(&endOffset);  }  else  {    aRange->GetEndParent(getter_AddRefs(startNode));    aRange->GetEndOffset(&startOffset);    aRange->GetStartParent(getter_AddRefs(endNode));    aRange->GetStartOffset(&endOffset);  }  if (!startNode || !endNode)    return NS_ERROR_FAILURE;  if (startNode == endNode)    return NS_OK;  //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 dirtystart = PR_FALSE;  nsCOMPtr<nsIAtom> atom;  if (parent != startNode)  {    result = startNode->GetParentNode(getter_AddRefs(tempNode));    if (NS_FAILED(result) || !tempNode)      return NS_ERROR_FAILURE;    while (tempNode != parent)    {      atom = GetTag(tempNode);      if (atom.get() == nsRangeList::sTableAtom) //select whole table  if in cell mode, wait for cell      {        result = ParentOffset(tempNode, getter_AddRefs(startNode), &startOffset);        if (NS_FAILED(result))          return NS_ERROR_FAILURE;        endOffset = startOffset+1;        dirtystart = PR_TRUE;        cellMode = PR_FALSE;      }      else if (atom.get() == nsRangeList::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;        endOffset = startOffset+1;        dirtystart = PR_TRUE;      }      result = tempNode->GetParentNode(getter_AddRefs(tempNode2));      if (NS_FAILED(result) || !tempNode2)        return NS_ERROR_FAILURE;      tempNode = tempNode2;    }  }    if (dirtystart)  {    *aFixupState = PR_TRUE;    if (*aDir == eDirNext)    {      if (NS_FAILED(aRange->SetStart(startNode,startOffset)) || NS_FAILED(aRange->SetEnd(startNode, endOffset)))      {        *aDir = eDirPrevious;        aRange->SetStart(startNode, endOffset);        aRange->SetEnd(startNode, startOffset);      }    }    else    {      if (NS_FAILED(aRange->SetStart(startNode,endOffset)) || NS_FAILED(aRange->SetEnd(startNode, startOffset)))      {        aRange->SetStart(startNode, startOffset);        aRange->SetEnd(startNode, endOffset);      }    }  }  return NS_OK;}