diff options
author | Andreas Kling <kling@serenityos.org> | 2022-03-21 16:15:10 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-21 16:15:33 +0100 |
commit | c8bdac8736b6c1ba65b1e8b10bafc1b3cdeae9e8 (patch) | |
tree | 656f2145076c40702dc00ef8327af87a5b3a2950 /Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp | |
parent | 1206dd22151f77d58b6f36348d0f465a7fd31f14 (diff) | |
download | serenity-c8bdac8736b6c1ba65b1e8b10bafc1b3cdeae9e8.zip |
LibWeb: Implement HTMLTableRowElement.{rowIndex,sectionRowIndex}
Another point on Acid3. :^)
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
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 <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; +} + } |