if (NS_FAILED(tempNode->GetParent(*getter_AddRefs(parent))))
if (NS_FAILED(tempNode->GetParent(getter_AddRefs(parent))))
nsresult nsContentIterator::PositionAt(nsIContent* aCurNode){  nsCOMPtr<nsIContent> newCurNode;  nsCOMPtr<nsIContent> tempNode(mCurNode);  if (!aCurNode)    return NS_ERROR_NULL_POINTER;  mCurNode = newCurNode = do_QueryInterface(aCurNode);  // take an early out if this doesn't actually change the position  if (mCurNode == tempNode)  {    mIsDone = PR_FALSE;  // paranoia    return NS_OK;  }  // Check to see if the node falls within the traversal range.  nsCOMPtr<nsIDOMNode> firstNode(do_QueryInterface(mFirst));  nsCOMPtr<nsIDOMNode> lastNode(do_QueryInterface(mLast));  PRInt32 firstOffset=0, lastOffset=0;  if (firstNode && lastNode)  {    PRUint32 numChildren;    if (mPre)    {      ContentToParentOffset(mFirst, getter_AddRefs(firstNode), &firstOffset);      numChildren = GetNumChildren(lastNode);      if (numChildren)        lastOffset = 0;      else      {        ContentToParentOffset(mLast, getter_AddRefs(lastNode), &lastOffset);        ++lastOffset;      }    }    else    {      numChildren = GetNumChildren(firstNode);      if (numChildren)        firstOffset = numChildren;      else        ContentToParentOffset(mFirst, getter_AddRefs(firstNode), &firstOffset);          ContentToParentOffset(mLast, getter_AddRefs(lastNode), &lastOffset);      ++lastOffset;    }  }  if (!firstNode || !lastNode ||      !ContentIsInTraversalRange(mCurNode, mPre, firstNode, firstOffset,                                 lastNode, lastOffset))  {    mIsDone = PR_TRUE;    return NS_ERROR_FAILURE;  }  // We can be at ANY node in the sequence.  // Need to regenerate the array of indexes back to the root or common parent!  nsCOMPtr<nsIContent> parent;  nsAutoVoidArray      oldParentStack;  nsAutoVoidArray      newIndexes;  // Get a list of the parents up to the root, then compare the new node  // with entries in that array until we find a match (lowest common  // ancestor).  If no match, use IndexOf, take the parent, and repeat.  // This avoids using IndexOf() N times on possibly large arrays.  We  // still end up doing it a fair bit.  It's better to use Clone() if  // possible.  // we know the depth we're down (though we may not have started at the  // top).  if (!oldParentStack.SizeTo(mIndexes.Count()+1))    return NS_ERROR_FAILURE;  // plus one for the node we're currently on.  for (PRInt32 i = mIndexes.Count()+1; i > 0 && tempNode; i--)  {    // Insert at head since we're walking up    oldParentStack.InsertElementAt(tempNode,0);    if (NS_FAILED(tempNode->GetParent(*getter_AddRefs(parent))))      return NS_ERROR_FAILURE;    if (!parent)  // this node has no parent, and thus no index      break;    if (parent == mCurNode)    {      // The position was moved to a parent of the current position.       // All we need to do is drop some indexes.  Shortcut here.      mIndexes.RemoveElementsAt(mIndexes.Count() - (oldParentStack.Count()+1),                                oldParentStack.Count());      mIsDone = PR_FALSE;      return NS_OK;    }    tempNode = parent;  }  // Ok.  We have the array of old parents.  Look for a match.  while (newCurNode)  {    PRInt32 indx;    if (NS_FAILED(newCurNode->GetParent(*getter_AddRefs(parent))))      return NS_ERROR_FAILURE;    if (!parent)  // this node has no parent, and thus no index      break;    if (NS_FAILED(parent->IndexOf(newCurNode, indx)))      return NS_ERROR_FAILURE;    // insert at the head!    newIndexes.InsertElementAt(NS_INT32_TO_PTR(indx),0);    // look to see if the parent is in the stack    indx = oldParentStack.IndexOf(parent);    if (indx >= 0)    {      // ok, the parent IS on the old stack!  Rework things.      // we want newIndexes to replace all nodes equal to or below the match      // Note that index oldParentStack.Count()-1 is the last node, which is      // one BELOW the last index in the mIndexes stack.      PRInt32 numToDrop = oldParentStack.Count()-(1+indx);      if (numToDrop > 0)        mIndexes.RemoveElementsAt(mIndexes.Count() - numToDrop,numToDrop);      mIndexes.InsertElementsAt(newIndexes,mIndexes.Count());      break;    }    newCurNode = parent;  }  // phew!  mIsDone = PR_FALSE;  return NS_OK;}