From c8bdac8736b6c1ba65b1e8b10bafc1b3cdeae9e8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 21 Mar 2022 16:15:10 +0100 Subject: [PATCH] LibWeb: Implement HTMLTableRowElement.{rowIndex,sectionRowIndex} Another point on Acid3. :^) --- .../LibWeb/HTML/HTMLTableRowElement.cpp | 53 +++++++++++++++++++ .../LibWeb/HTML/HTMLTableRowElement.h | 3 ++ .../LibWeb/HTML/HTMLTableRowElement.idl | 3 ++ 3 files changed, 59 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp index 2fea5861a9..107d807359 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp @@ -6,7 +6,9 @@ #include #include +#include #include +#include namespace Web::HTML { @@ -30,4 +32,55 @@ NonnullRefPtr 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 { + if (!parent()) + return nullptr; + if (is(*parent())) + return const_cast(static_cast(*parent())).rows(); + if (is(*parent()) && parent()->parent() && is(*parent()->parent())) + return const_cast(static_cast(*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 { + if (!parent()) + return nullptr; + if (is(*parent())) + return const_cast(static_cast(*parent())).rows(); + if (is(*parent())) + return static_cast(*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; +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.h b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.h index 1a85e95da4..065244ec7a 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.h @@ -18,6 +18,9 @@ public: virtual ~HTMLTableRowElement() override; NonnullRefPtr cells() const; + + int row_index() const; + int section_row_index() const; }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.idl index 8ddaa33eb3..29d21d4f99 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.idl @@ -10,6 +10,9 @@ interface HTMLTableRowElement : HTMLElement { [LegacyNullToEmptyString, Reflect=bgcolor] attribute DOMString bgColor; + readonly attribute long rowIndex; + readonly attribute long sectionRowIndex; + [SameObject] readonly attribute HTMLCollection cells; };