HTMLTableRowsCollection.cpp [plain text]
#include "config.h"
#include "HTMLTableRowsCollection.h"
#include "ElementIterator.h"
#include "HTMLNames.h"
#include "HTMLTableElement.h"
#include "HTMLTableRowElement.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
using namespace HTMLNames;
WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLTableRowsCollection);
static inline void assertRowIsInTable(HTMLTableElement& table, HTMLTableRowElement* row)
{
#if ASSERT_ENABLED
UNUSED_PARAM(table);
UNUSED_PARAM(row);
#else // not ASSERT_ENABLED
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 // not ASSERT_ENABLED
}
static inline bool isInSection(HTMLTableRowElement& row, const HTMLQualifiedName& sectionTag)
{
return downcast<HTMLElement>(*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;
}
RefPtr<Element> child;
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 (is<HTMLTableRowElement>(*child))
return downcast<HTMLTableRowElement>(child.get());
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 (is<HTMLTableRowElement>(*child))
return downcast<HTMLTableRowElement>(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)
: CachedHTMLCollection(table, TableRows)
{
}
Ref<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()), downcast<HTMLTableRowElement>(previous));
}
}