bug 9271 - correctly insert cells, rows when insertion index equals number of elements.

This commit is contained in:
karnaze%netscape.com 2000-05-03 14:57:00 +00:00
parent d3dd6e17e4
commit 902ff895ab
4 changed files with 48 additions and 66 deletions

View File

@ -523,35 +523,32 @@ nsHTMLTableRowElement::InsertCell(PRInt32 aIndex, nsIDOMHTMLElement** aValue)
*aValue = nsnull; *aValue = nsnull;
PRInt32 refIndex = (0 <= aIndex) ? aIndex : 0; PRInt32 refIndex = (0 <= aIndex) ? aIndex : 0;
nsIDOMHTMLCollection *cells; nsCOMPtr<nsIDOMHTMLCollection> cells;
GetCells(&cells); GetCells(getter_AddRefs(cells));
PRUint32 cellCount; PRUint32 cellCount;
cells->GetLength(&cellCount); cells->GetLength(&cellCount);
if (cellCount <= PRUint32(aIndex)) { PRBool doInsert = (aIndex < PRInt32(cellCount));
refIndex = cellCount - 1; // refIndex will be -1 if there are no cells
} // create the cell
// create the cell nsIHTMLContent* cellContent = nsnull;
nsIHTMLContent *cellContent = nsnull;
nsresult rv = NS_NewHTMLTableCellElement(&cellContent, nsHTMLAtoms::td); nsresult rv = NS_NewHTMLTableCellElement(&cellContent, nsHTMLAtoms::td);
if (NS_SUCCEEDED(rv) && (nsnull != cellContent)) { if (NS_SUCCEEDED(rv) && (nsnull != cellContent)) {
nsIDOMNode *cellNode = nsnull; nsIDOMNode* cellNode = nsnull;
rv = cellContent->QueryInterface(kIDOMNodeIID, (void **)&cellNode); rv = cellContent->QueryInterface(kIDOMNodeIID, (void **)&cellNode);
if (NS_SUCCEEDED(rv) && (nsnull != cellNode)) { if (NS_SUCCEEDED(rv) && (nsnull != cellNode)) {
if (refIndex >= 0) { if (doInsert) {
nsIDOMNode *refCell; PRInt32 refIndex = PR_MAX(aIndex, 0);
cells->Item(refIndex, &refCell); nsCOMPtr<nsIDOMNode> refCell;
cells->Item(refIndex, getter_AddRefs(refCell));
rv = InsertBefore(cellNode, refCell, (nsIDOMNode **)aValue); rv = InsertBefore(cellNode, refCell, (nsIDOMNode **)aValue);
} else { } else {
rv = AppendChild(cellNode, (nsIDOMNode **)aValue); rv = AppendChild(cellNode, (nsIDOMNode **)aValue);
} }
NS_RELEASE(cellNode); NS_RELEASE(cellNode);
} }
NS_RELEASE(cellContent); NS_RELEASE(cellContent);
} }
NS_RELEASE(cells);
return NS_OK; return NS_OK;
} }

View File

@ -168,37 +168,31 @@ nsHTMLTableSectionElement::InsertRow(PRInt32 aIndex, nsIDOMHTMLElement** aValue)
{ {
*aValue = nsnull; *aValue = nsnull;
PRInt32 refIndex = (0 <= aIndex) ? aIndex : 0; nsCOMPtr<nsIDOMHTMLCollection> rows;
GetRows(getter_AddRefs(rows));
nsIDOMHTMLCollection *rows;
GetRows(&rows);
PRUint32 rowCount; PRUint32 rowCount;
rows->GetLength(&rowCount); rows->GetLength(&rowCount);
if (rowCount <= PRUint32(aIndex)) { PRBool doInsert = (aIndex < PRInt32(rowCount));
refIndex = rowCount - 1; // refIndex will be -1 if there are no rows
}
// create the row // create the row
nsIHTMLContent *rowContent = nsnull; nsIHTMLContent* rowContent = nsnull;
nsresult rv = NS_NewHTMLTableRowElement(&rowContent, nsHTMLAtoms::tr); nsresult rv = NS_NewHTMLTableRowElement(&rowContent, nsHTMLAtoms::tr);
if (NS_SUCCEEDED(rv) && (nsnull != rowContent)) { if (NS_SUCCEEDED(rv) && (nsnull != rowContent)) {
nsIDOMNode *rowNode = nsnull; nsIDOMNode* rowNode = nsnull;
rv = rowContent->QueryInterface(kIDOMNodeIID, (void **)&rowNode); rv = rowContent->QueryInterface(kIDOMNodeIID, (void **)&rowNode);
if (NS_SUCCEEDED(rv) && (nsnull != rowNode)) { if (NS_SUCCEEDED(rv) && (nsnull != rowNode)) {
if (refIndex >= 0) { if (doInsert) {
nsIDOMNode *refRow; PRInt32 refIndex = PR_MAX(aIndex, 0);
rows->Item(refIndex, &refRow); nsCOMPtr<nsIDOMNode> refRow;
rows->Item(refIndex, getter_AddRefs(refRow));
rv = InsertBefore(rowNode, refRow, (nsIDOMNode **)aValue); rv = InsertBefore(rowNode, refRow, (nsIDOMNode **)aValue);
NS_RELEASE(refRow);
} else { } else {
rv = AppendChild(rowNode, (nsIDOMNode **)aValue); rv = AppendChild(rowNode, (nsIDOMNode **)aValue);
} }
NS_RELEASE(rowNode); NS_RELEASE(rowNode);
} }
NS_RELEASE(rowContent); NS_RELEASE(rowContent);
} }
NS_RELEASE(rows);
return NS_OK; return NS_OK;
} }

View File

@ -523,35 +523,32 @@ nsHTMLTableRowElement::InsertCell(PRInt32 aIndex, nsIDOMHTMLElement** aValue)
*aValue = nsnull; *aValue = nsnull;
PRInt32 refIndex = (0 <= aIndex) ? aIndex : 0; PRInt32 refIndex = (0 <= aIndex) ? aIndex : 0;
nsIDOMHTMLCollection *cells; nsCOMPtr<nsIDOMHTMLCollection> cells;
GetCells(&cells); GetCells(getter_AddRefs(cells));
PRUint32 cellCount; PRUint32 cellCount;
cells->GetLength(&cellCount); cells->GetLength(&cellCount);
if (cellCount <= PRUint32(aIndex)) { PRBool doInsert = (aIndex < PRInt32(cellCount));
refIndex = cellCount - 1; // refIndex will be -1 if there are no cells
} // create the cell
// create the cell nsIHTMLContent* cellContent = nsnull;
nsIHTMLContent *cellContent = nsnull;
nsresult rv = NS_NewHTMLTableCellElement(&cellContent, nsHTMLAtoms::td); nsresult rv = NS_NewHTMLTableCellElement(&cellContent, nsHTMLAtoms::td);
if (NS_SUCCEEDED(rv) && (nsnull != cellContent)) { if (NS_SUCCEEDED(rv) && (nsnull != cellContent)) {
nsIDOMNode *cellNode = nsnull; nsIDOMNode* cellNode = nsnull;
rv = cellContent->QueryInterface(kIDOMNodeIID, (void **)&cellNode); rv = cellContent->QueryInterface(kIDOMNodeIID, (void **)&cellNode);
if (NS_SUCCEEDED(rv) && (nsnull != cellNode)) { if (NS_SUCCEEDED(rv) && (nsnull != cellNode)) {
if (refIndex >= 0) { if (doInsert) {
nsIDOMNode *refCell; PRInt32 refIndex = PR_MAX(aIndex, 0);
cells->Item(refIndex, &refCell); nsCOMPtr<nsIDOMNode> refCell;
cells->Item(refIndex, getter_AddRefs(refCell));
rv = InsertBefore(cellNode, refCell, (nsIDOMNode **)aValue); rv = InsertBefore(cellNode, refCell, (nsIDOMNode **)aValue);
} else { } else {
rv = AppendChild(cellNode, (nsIDOMNode **)aValue); rv = AppendChild(cellNode, (nsIDOMNode **)aValue);
} }
NS_RELEASE(cellNode); NS_RELEASE(cellNode);
} }
NS_RELEASE(cellContent); NS_RELEASE(cellContent);
} }
NS_RELEASE(cells);
return NS_OK; return NS_OK;
} }

View File

@ -168,37 +168,31 @@ nsHTMLTableSectionElement::InsertRow(PRInt32 aIndex, nsIDOMHTMLElement** aValue)
{ {
*aValue = nsnull; *aValue = nsnull;
PRInt32 refIndex = (0 <= aIndex) ? aIndex : 0; nsCOMPtr<nsIDOMHTMLCollection> rows;
GetRows(getter_AddRefs(rows));
nsIDOMHTMLCollection *rows;
GetRows(&rows);
PRUint32 rowCount; PRUint32 rowCount;
rows->GetLength(&rowCount); rows->GetLength(&rowCount);
if (rowCount <= PRUint32(aIndex)) { PRBool doInsert = (aIndex < PRInt32(rowCount));
refIndex = rowCount - 1; // refIndex will be -1 if there are no rows
}
// create the row // create the row
nsIHTMLContent *rowContent = nsnull; nsIHTMLContent* rowContent = nsnull;
nsresult rv = NS_NewHTMLTableRowElement(&rowContent, nsHTMLAtoms::tr); nsresult rv = NS_NewHTMLTableRowElement(&rowContent, nsHTMLAtoms::tr);
if (NS_SUCCEEDED(rv) && (nsnull != rowContent)) { if (NS_SUCCEEDED(rv) && (nsnull != rowContent)) {
nsIDOMNode *rowNode = nsnull; nsIDOMNode* rowNode = nsnull;
rv = rowContent->QueryInterface(kIDOMNodeIID, (void **)&rowNode); rv = rowContent->QueryInterface(kIDOMNodeIID, (void **)&rowNode);
if (NS_SUCCEEDED(rv) && (nsnull != rowNode)) { if (NS_SUCCEEDED(rv) && (nsnull != rowNode)) {
if (refIndex >= 0) { if (doInsert) {
nsIDOMNode *refRow; PRInt32 refIndex = PR_MAX(aIndex, 0);
rows->Item(refIndex, &refRow); nsCOMPtr<nsIDOMNode> refRow;
rows->Item(refIndex, getter_AddRefs(refRow));
rv = InsertBefore(rowNode, refRow, (nsIDOMNode **)aValue); rv = InsertBefore(rowNode, refRow, (nsIDOMNode **)aValue);
NS_RELEASE(refRow);
} else { } else {
rv = AppendChild(rowNode, (nsIDOMNode **)aValue); rv = AppendChild(rowNode, (nsIDOMNode **)aValue);
} }
NS_RELEASE(rowNode); NS_RELEASE(rowNode);
} }
NS_RELEASE(rowContent); NS_RELEASE(rowContent);
} }
NS_RELEASE(rows);
return NS_OK; return NS_OK;
} }