HTMLTableRowsCollection.cpp [plain text]
#include "config.h"
#include "HTMLTableRowsCollection.h"
#include "ElementIterator.h"
#include "HTMLNames.h"
#include "HTMLTableElement.h"
#include "HTMLTableRowElement.h"
namespace WebCore {
using namespace HTMLNames;
static inline void assertRowIsInTable(HTMLTableElement* table, HTMLTableRowElement* row)
{
#if !ASSERT_DISABLED
UNUSED_PARAM(table);
UNUSED_PARAM(row);
#else
if (!row)
return;
if (row->parentNode() == table)
return;
ASSERT(row->parentNode());
ASSERT(row->parentNode()->hasTagName(theadTag) || row->parentNode()->hasTagName(tbodyTag) || row->parentNode()->hasTagName(tfootTag));
ASSERT(row->parentNode()->parentNode() == table);
#endif
}
static inline bool isInSection(HTMLTableRowElement& row, const HTMLQualifiedName& sectionTag)
{
return toHTMLElement(row.parentNode())->hasTagName(sectionTag);
}
HTMLTableRowElement* HTMLTableRowsCollection::rowAfter(HTMLTableElement* table, HTMLTableRowElement* previous)
{
assertRowIsInTable(table, previous);
if (previous && previous->parentNode() != table) {
auto childRows = childrenOfType<HTMLTableRowElement>(*previous->parentNode());
auto row = childRows.beginAt(*previous);
if (++row != childRows.end())
return &*row;
}
Element* child = nullptr;
if (!previous)
child = ElementTraversal::firstChild(table);
else if (isInSection(*previous, theadTag))
child = ElementTraversal::nextSibling(previous->parentNode());
for (; child; child = ElementTraversal::nextSibling(child)) {
if (child->hasTagName(theadTag)) {
if (auto row = childrenOfType<HTMLTableRowElement>(*child).first())
return row;
}
}
if (!previous || isInSection(*previous, theadTag))
child = ElementTraversal::firstChild(table);
else if (previous->parentNode() == table)
child = ElementTraversal::nextSibling(previous);
else if (isInSection(*previous, tbodyTag))
child = ElementTraversal::nextSibling(previous->parentNode());
for (; child; child = ElementTraversal::nextSibling(child)) {
if (isHTMLTableRowElement(child))
return toHTMLTableRowElement(child);
if (child->hasTagName(tbodyTag)) {
if (auto row = childrenOfType<HTMLTableRowElement>(*child).first())
return row;
}
}
if (!previous || !isInSection(*previous, tfootTag))
child = ElementTraversal::firstChild(table);
else
child = ElementTraversal::nextSibling(previous->parentNode());
for (; child; child = ElementTraversal::nextSibling(child)) {
if (child->hasTagName(tfootTag)) {
if (auto row = childrenOfType<HTMLTableRowElement>(*child).first())
return row;
}
}
return nullptr;
}
HTMLTableRowElement* HTMLTableRowsCollection::lastRow(HTMLTableElement* table)
{
for (auto* child = ElementTraversal::lastChild(table); child; child = ElementTraversal::previousSibling(child)) {
if (child->hasTagName(tfootTag)) {
if (auto* row = childrenOfType<HTMLTableRowElement>(*child).last())
return row;
}
}
for (auto* child = ElementTraversal::lastChild(table); child; child = ElementTraversal::previousSibling(child)) {
if (isHTMLTableRowElement(child))
return toHTMLTableRowElement(child);
if (child->hasTagName(tbodyTag)) {
if (auto* row = childrenOfType<HTMLTableRowElement>(*child).last())
return row;
}
}
for (auto* child = ElementTraversal::lastChild(table); child; child = ElementTraversal::previousSibling(child)) {
if (child->hasTagName(theadTag)) {
if (auto* row = childrenOfType<HTMLTableRowElement>(*child).last())
return row;
}
}
return nullptr;
}
HTMLTableRowsCollection::HTMLTableRowsCollection(HTMLTableElement& table)
: HTMLCollection(table, TableRows, CustomForwardOnlyTraversal)
{
}
PassRef<HTMLTableRowsCollection> HTMLTableRowsCollection::create(HTMLTableElement& table, CollectionType type)
{
ASSERT_UNUSED(type, type == TableRows);
return adoptRef(*new HTMLTableRowsCollection(table));
}
Element* HTMLTableRowsCollection::customElementAfter(Element* previous) const
{
return rowAfter(const_cast<HTMLTableElement*>(&tableElement()), toHTMLTableRowElement(previous));
}
}