if (aIndex < 0) {
if (aIndex < -1) {
nsHTMLTableElement::InsertRow(PRInt32 aIndex, nsIDOMHTMLElement** aValue){  /* get the ref row at aIndex     if there is one,        get it's parent       insert the new row just before the ref row     else       get the first row group       insert the new row as its first child  */  *aValue = nsnull;  if (aIndex < 0) {    return NS_ERROR_DOM_INDEX_SIZE_ERR;  }  nsresult rv;  nsCOMPtr<nsIDOMHTMLCollection> rows;  GetRows(getter_AddRefs(rows));  PRUint32 rowCount;  rows->GetLength(&rowCount);  if ((PRUint32)aIndex > rowCount) {    return NS_ERROR_DOM_INDEX_SIZE_ERR;  }  // use local variable refIndex so we can remember original aIndex  PRUint32 refIndex = (PRUint32)aIndex;  if (rowCount > 0) {    if (refIndex == rowCount) {      // we set refIndex to the last row so we can get the last row's      // parent we then do an AppendChild below if (rowCount<aIndex)      refIndex = rowCount - 1;    }    nsCOMPtr<nsIDOMNode> refRow;    rows->Item(refIndex, getter_AddRefs(refRow));    nsCOMPtr<nsIDOMNode> parent;    refRow->GetParentNode(getter_AddRefs(parent));    // create the row    nsCOMPtr<nsIHTMLContent> newRow;    nsCOMPtr<nsINodeInfo> nodeInfo;    mNodeInfo->NameChanged(nsHTMLAtoms::tr, *getter_AddRefs(nodeInfo));    rv = NS_NewHTMLTableRowElement(getter_AddRefs(newRow), nodeInfo);    if (NS_SUCCEEDED(rv) && newRow) {      nsCOMPtr<nsIDOMNode> newRowNode(do_QueryInterface(newRow));      nsCOMPtr<nsIDOMNode> retChild;      // the index is greater than the number of rows, so just append      if ((0<=aIndex) && (PRInt32(rowCount)<=aIndex)) {        rv = parent->AppendChild(newRowNode, getter_AddRefs(retChild));      }      else      {        // insert the new row before the reference row we found above        rv = parent->InsertBefore(newRowNode, refRow,                                  getter_AddRefs(retChild));      }      if (retChild) {        retChild->QueryInterface(NS_GET_IID(nsIDOMHTMLElement),                                 (void **)aValue);      }    }  }  else  { // the row count was 0, so     // find the first row group and insert there as first child    nsCOMPtr<nsIDOMNode> rowGroup;    GenericElementCollection head(this, nsHTMLAtoms::thead);    PRUint32 length = 0;    head.GetLength(&length);    if (0 != length) {      head.Item(0, getter_AddRefs(rowGroup));    }    else    {      GenericElementCollection body(this, nsHTMLAtoms::tbody);      length = 0;      body.GetLength(&length);      if (length > 0) {        body.Item(0, getter_AddRefs(rowGroup));      }      else      {        GenericElementCollection foot(this, nsHTMLAtoms::tfoot);        length = 0;        foot.GetLength(&length);        if (length > 0) {          foot.Item(0, getter_AddRefs(rowGroup));        }      }    }    if (!rowGroup) { // need to create a TBODY      nsCOMPtr<nsIHTMLContent> newRowGroup;      nsCOMPtr<nsINodeInfo> nodeInfo;      mNodeInfo->NameChanged(nsHTMLAtoms::tbody, *getter_AddRefs(nodeInfo));      rv = NS_NewHTMLTableSectionElement(getter_AddRefs(newRowGroup),                                         nodeInfo);      if (NS_SUCCEEDED(rv) && newRowGroup) {        rv = AppendChildTo(newRowGroup, PR_TRUE, PR_FALSE);        rowGroup = do_QueryInterface(newRowGroup);      }    }    if (rowGroup) {      nsCOMPtr<nsIHTMLContent> newRow;      nsCOMPtr<nsINodeInfo> nodeInfo;      mNodeInfo->NameChanged(nsHTMLAtoms::tr, *getter_AddRefs(nodeInfo));      rv = NS_NewHTMLTableRowElement(getter_AddRefs(newRow), nodeInfo);      nsCOMPtr<nsIContent> rowGroupContent(do_QueryInterface(rowGroup));      GenericElementCollection rowGroupRows(rowGroupContent, nsHTMLAtoms::tr);      nsCOMPtr<nsIDOMNode> firstRow;      // it's ok if this returns nsnull      rowGroupRows.Item(0, getter_AddRefs(firstRow));      if (newRow) {        nsCOMPtr<nsIDOMNode> retNode, newRowNode(do_QueryInterface(newRow));        rowGroup->InsertBefore(newRowNode, firstRow, getter_AddRefs(retNode));        if (retNode) {          retNode->QueryInterface(NS_GET_IID(nsIDOMHTMLElement),                                  (void **)aValue);        }      }    }  }  return NS_OK;}