1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 13:44:58 +00:00

LibWeb: Implement HTMLTableRowElement.{rowIndex,sectionRowIndex}

Another point on Acid3. :^)
This commit is contained in:
Andreas Kling 2022-03-21 16:15:10 +01:00
parent 1206dd2215
commit c8bdac8736
3 changed files with 59 additions and 0 deletions

View file

@ -6,7 +6,9 @@
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/HTML/HTMLTableCellElement.h>
#include <LibWeb/HTML/HTMLTableElement.h>
#include <LibWeb/HTML/HTMLTableRowElement.h>
#include <LibWeb/HTML/HTMLTableSectionElement.h>
namespace Web::HTML {
@ -30,4 +32,55 @@ NonnullRefPtr<DOM::HTMLCollection> HTMLTableRowElement::cells() const
});
}
// https://html.spec.whatwg.org/multipage/tables.html#dom-tr-rowindex
int HTMLTableRowElement::row_index() const
{
// The rowIndex attribute must, if this element has a parent table element,
// or a parent tbody, thead, or tfoot element and a grandparent table element,
// return the index of this tr element in that table element's rows collection.
// If there is no such table element, then the attribute must return 1.
auto rows_collection = [&]() -> RefPtr<DOM::HTMLCollection> {
if (!parent())
return nullptr;
if (is<HTMLTableElement>(*parent()))
return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
if (is<HTMLTableSectionElement>(*parent()) && parent()->parent() && is<HTMLTableElement>(*parent()->parent()))
return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent()->parent())).rows();
return nullptr;
}();
if (!rows_collection)
return -1;
auto rows = rows_collection->collect_matching_elements();
for (size_t i = 0; i < rows.size(); ++i) {
if (rows[i].ptr() == this)
return i;
}
return -1;
}
int HTMLTableRowElement::section_row_index() const
{
// The sectionRowIndex attribute must, if this element has a parent table, tbody, thead, or tfoot element,
// return the index of the tr element in the parent element's rows collection
// (for tables, that's HTMLTableElement's rows collection; for table sections, that's HTMLTableSectionElement's rows collection).
// If there is no such parent element, then the attribute must return 1.
auto rows_collection = [&]() -> RefPtr<DOM::HTMLCollection> {
if (!parent())
return nullptr;
if (is<HTMLTableElement>(*parent()))
return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
if (is<HTMLTableSectionElement>(*parent()))
return static_cast<HTMLTableSectionElement const&>(*parent()).rows();
return nullptr;
}();
if (!rows_collection)
return -1;
auto rows = rows_collection->collect_matching_elements();
for (size_t i = 0; i < rows.size(); ++i) {
if (rows[i].ptr() == this)
return i;
}
return -1;
}
}