HTMLTableRowElement* HTMLTableElement::insertRow(
    int index,
    ExceptionState& exceptionState) {
  if (index < -1) {
    exceptionState.throwDOMException(
        IndexSizeError,
        "The index provided (" + String::number(index) + ") is less than -1.");
    return nullptr;
  }

  HTMLTableRowElement* lastRow = nullptr;
  HTMLTableRowElement* row = nullptr;
  if (index == -1) {
    lastRow = HTMLTableRowsCollection::lastRow(*this);
  } else {
    for (int i = 0; i <= index; ++i) {
      row = HTMLTableRowsCollection::rowAfter(*this, lastRow);
      if (!row) {
        if (i != index) {
          exceptionState.throwDOMException(
              IndexSizeError,
              "The index provided (" + String::number(index) +
                  ") is greater than the number of rows in the table (" +
                  String::number(i) + ").");
          return nullptr;
        }
        break;
      }
      lastRow = row;
    }
  }

  ContainerNode* parent;
  if (lastRow) {
    parent = row ? row->parentNode() : lastRow->parentNode();
  } else {
    parent = lastBody();
    if (!parent) {
      HTMLTableSectionElement* newBody =
          HTMLTableSectionElement::create(tbodyTag, document());
      HTMLTableRowElement* newRow = HTMLTableRowElement::create(document());
      newBody->appendChild(newRow, exceptionState);
      appendChild(newBody, exceptionState);
      return newRow;
    }
  }

  HTMLTableRowElement* newRow = HTMLTableRowElement::create(document());
  parent->insertBefore(newRow, row, exceptionState);
  return newRow;
}
void FTPDirectoryTokenizer::appendEntry(const String& filename, const String& size, const String& date, bool isDirectory)
{
    ExceptionCode ec;

    RefPtr<Element> rowElement = m_doc->createElementNS(xhtmlNamespaceURI, "tr", ec);
    rowElement->setAttribute("class", "ftpDirectoryEntryRow", ec);
   
    RefPtr<Element> element = m_doc->createElementNS(xhtmlNamespaceURI, "td", ec);
    element->appendChild(new Text(m_doc, String(&noBreakSpace, 1)), ec);
    if (isDirectory)
        element->setAttribute("class", "ftpDirectoryIcon ftpDirectoryTypeDirectory", ec);
    else
        element->setAttribute("class", "ftpDirectoryIcon ftpDirectoryTypeFile", ec);
    rowElement->appendChild(element, ec);
    
    element = createTDForFilename(filename);
    element->setAttribute("class", "ftpDirectoryFileName", ec);
    rowElement->appendChild(element, ec);
    
    element = m_doc->createElementNS(xhtmlNamespaceURI, "td", ec);
    element->appendChild(new Text(m_doc, date), ec);
    element->setAttribute("class", "ftpDirectoryFileDate", ec);
    rowElement->appendChild(element, ec);
    
    element = m_doc->createElementNS(xhtmlNamespaceURI, "td", ec);
    element->appendChild(new Text(m_doc, size), ec);
    element->setAttribute("class", "ftpDirectoryFileSize", ec);
    rowElement->appendChild(element, ec);
    
    // Append the new row to the first tbody if it exists.  
    // Many <TABLE> elements end up having an implicit <TBODY> created for them and in those
    // cases, it's more correct to append to the tbody instead of the table itself
    HTMLTableSectionElement* body = m_tableElement->firstTBody();
    if (body)
        body->appendChild(rowElement, ec);
    else
        m_tableElement->appendChild(rowElement, ec);
}