for (i=mEndOffset; --i; i>=mStartOffset)
for (i=mEndOffset; i>=mStartOffset; --i)
nsresult nsRange::DeleteContents(){   nsIContent *cStart;  nsIContent *cEnd;    // get the content versions of our endpoints  nsresult res = mStartParent->QueryInterface(kIContentIID, (void**)&cStart);  if (!NS_SUCCEEDED(res))   {    NS_NOTREACHED("nsRange::DeleteContents");    return NS_ERROR_UNEXPECTED;  }  res = mEndParent->QueryInterface(kIContentIID, (void**)&cEnd);  if (!NS_SUCCEEDED(res))   {    NS_NOTREACHED("nsRange::DeleteContents");    NS_IF_RELEASE(cStart);    return NS_ERROR_UNEXPECTED;  }    // effeciency hack for simple case  if (cStart == cEnd)  {    PRBool hasChildren;    res = cStart->CanContainChildren(hasChildren);    if (!NS_SUCCEEDED(res))     {      NS_NOTREACHED("nsRange::DeleteContents");      NS_IF_RELEASE(cStart);      NS_IF_RELEASE(cEnd);      return res;    }        if (hasChildren)    {      PRInt32 i;        for (i=mEndOffset; --i; i>=mStartOffset)      {        res = cStart->RemoveChildAt(i, PR_TRUE);        if (!NS_SUCCEEDED(res))         {          NS_NOTREACHED("nsRange::DeleteContents");          NS_IF_RELEASE(cStart);          NS_IF_RELEASE(cEnd);          return res;        }      }      NS_IF_RELEASE(cStart);      NS_IF_RELEASE(cEnd);      return NS_OK;    }    else // textnode, or somesuch.  offsets refer to data in node    {      nsIDOMText *textNode;      res = mStartParent->QueryInterface(kIDOMTextIID, (void**)&textNode);      if (!NS_SUCCEEDED(res)) // if it's not a text node, punt      {        NS_NOTREACHED("nsRange::DeleteContents");        NS_IF_RELEASE(cStart);        NS_IF_RELEASE(cEnd);        return NS_ERROR_UNEXPECTED;      }      // delete the text      res = textNode->DeleteData(mStartOffset, mEndOffset);      if (!NS_SUCCEEDED(res))       {        NS_NOTREACHED("nsRange::DeleteContents");        NS_IF_RELEASE(cStart);        NS_IF_RELEASE(cEnd);        NS_IF_RELEASE(textNode);        return res;      }      NS_IF_RELEASE(textNode);      return NS_OK;    }  }     /* complex case: cStart != cEnd     revisit - there are potential optimizations here and also tradeoffs.  */    // get start node ancestors  nsVoidArray startAncestorList;  FillArrayWithAncestors(&startAncestorList,mStartParent);  nsContentIterator iter(this);  nsVoidArray deleteList;  nsIContent *cN;  nsIContent *cParent;  PRInt32 indx;   // loop through the content iterator, which returns nodes in the range in   // close tag order, and mark for deletion any node that is not an ancestor  // of the start node.  res = iter.CurrentNode(&cN);  while (NS_COMFALSE == iter.IsDone())  {    // if node is not an ancestor of start node, delete it    if (mStartAncestors->IndexOf(NS_STATIC_CAST(void*,cN)) == -1)    {      deleteList.AppendElement(NS_STATIC_CAST(void*,cN));    }    iter.Next();    NS_IF_RELEASE(cN);  // balances addref inside CurrentNode()    res = iter.CurrentNode(&cN);  }    // remove the nodes on the delete list  while (deleteList.Count())  {    cN = NS_STATIC_CAST(nsIContent*, deleteList.ElementAt(0));    res = cN->GetParent(cParent);    res = cParent->IndexOf(cN,indx);    res = cParent->RemoveChildAt(indx, PR_TRUE);  }    NS_IF_RELEASE(cStart);  NS_IF_RELEASE(cEnd);  return NS_OK;}