if (NS_FAILED(aNewFocus->GetParent(*getter_AddRefs(parent))) || !parent)
if (NS_FAILED(aNewFocus->GetParent(getter_AddRefs(parent))) || !parent)
nsSelection::TakeFocus(nsIContent *aNewFocus, PRUint32 aContentOffset,                        PRUint32 aContentEndOffset, PRBool aContinueSelection, PRBool aMultipleSelection){  if (!aNewFocus)    return NS_ERROR_NULL_POINTER;  STATUS_CHECK_RETURN_MACRO();  if (!IsValidSelectionPoint(this,aNewFocus))    return NS_ERROR_FAILURE;  // Clear all table selection data  mSelectingTableCellMode = 0;  mDragSelectingCells = PR_FALSE;  mStartSelectedCell = nsnull;  mEndSelectedCell = nsnull;  mAppendStartSelectedCell = nsnull;  //HACKHACKHACK  nsCOMPtr<nsIContent> content;  nsCOMPtr<nsIDOMNode> domNode;  nsCOMPtr<nsIContent> parent;  nsCOMPtr<nsIContent> parent2;  if (NS_FAILED(aNewFocus->GetParent(*getter_AddRefs(parent))) || !parent)    return NS_ERROR_FAILURE;  //END HACKHACKHACK /checking for root frames/content  PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);  domNode = do_QueryInterface(aNewFocus);  //traverse through document and unselect crap here  if (!aContinueSelection){ //single click? setting cursor down    PRUint32 batching = mBatching;//hack to use the collapse code.    PRBool changes = mChangesDuringBatching;    mBatching = 1;    if (aMultipleSelection){      nsCOMPtr<nsIDOMRange> newRange;      NS_NewRange(getter_AddRefs(newRange));      newRange->SetStart(domNode,aContentOffset);      newRange->SetEnd(domNode,aContentOffset);      mDomSelections[index]->AddRange(newRange);      mBatching = batching;      mChangesDuringBatching = changes;      mDomSelections[index]->SetOriginalAnchorPoint(domNode,aContentOffset);    }    else    {      PRBool oldDesiredXSet = mDesiredXSet; //need to keep old desired X if it was set.      mDomSelections[index]->Collapse(domNode, aContentOffset);      mDesiredXSet = oldDesiredXSet; //now reset desired X back.      mBatching = batching;      mChangesDuringBatching = changes;    }    if (aContentEndOffset != aContentOffset)      mDomSelections[index]->Extend(domNode,aContentEndOffset);    //find out if we are inside a table. if so, find out which one and which cell    //once we do that, the next time we get a takefocus, check the parent tree.     //if we are no longer inside same table ,cell then switch to table selection mode.    // BUT only do this in an editor    nsCOMPtr<nsIPresContext> presContext;    nsresult result = mTracker->GetPresContext(getter_AddRefs(presContext));    if (NS_FAILED(result) || !presContext)      return result?result:NS_ERROR_FAILURE;    nsCOMPtr<nsIPresShell> presShell;    presContext->GetShell(getter_AddRefs(presShell));    if (!presShell)      return NS_ERROR_FAILURE;    PRInt16 displaySelection;    result = presShell->GetSelectionFlags(&displaySelection);    if (NS_FAILED(result))      return result;    // Editor has DISPLAY_ALL selection type    if (displaySelection == nsISelectionDisplay::DISPLAY_ALL)    {      mCellParent = GetCellParent(domNode);#ifdef DEBUG_TABLE_SELECTION      if (mCellParent)        printf(" * TakeFocus - Collapsing into new cell\n");#endif    }  }  else {    // Now update the range list:    if (aContinueSelection && domNode)    {      PRInt32 offset;      nsIDOMNode *cellparent = GetCellParent(domNode);      if (mCellParent && cellparent && cellparent != mCellParent) //switch to cell selection mode      {#ifdef DEBUG_TABLE_SELECTIONprintf(" * TakeFocus - moving into new cell\n");#endif        nsCOMPtr<nsIDOMNode> parent;        nsCOMPtr<nsIContent> parentContent;        nsMouseEvent event;        nsresult result;        // Start selecting in the cell we were in before        result = ParentOffset(mCellParent, getter_AddRefs(parent),&offset);        parentContent = do_QueryInterface(parent);        if (parentContent)          result = HandleTableSelection(parentContent, offset, nsISelectionPrivate::TABLESELECTION_CELL, &event);        // Find the parent of this new cell and extend selection to it        result = ParentOffset(cellparent,getter_AddRefs(parent),&offset);        parentContent = do_QueryInterface(parent);        // XXXX We need to REALLY get the current key shift state        //  (we'd need to add event listener -- let's not bother for now)        event.isShift = PR_FALSE; //aContinueSelection;        if (parentContent)        {          mCellParent = cellparent;          // Continue selection into next cell          result = HandleTableSelection(parentContent, offset, nsISelectionPrivate::TABLESELECTION_CELL, &event);        }      }      else      {        // XXXX Problem: Shift+click in browser is appending text selection to selected table!!!        //   is this the place to erase seleced cells ?????        if (mDomSelections[index]->GetDirection() == eDirNext && aContentEndOffset > aContentOffset) //didnt go far enough         {          mDomSelections[index]->Extend(domNode, aContentEndOffset);//this will only redraw the diff         }        else          mDomSelections[index]->Extend(domNode, aContentOffset);      }    }  }  // Don't notify selection listeners if batching is on:  if (GetBatching())    return NS_OK;  return NotifySelectionListeners(nsISelectionController::SELECTION_NORMAL);}