/* * Copyright (c) 2020-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include namespace Web::HTML { HTMLTableRowElement::HTMLTableRowElement(DOM::Document& document, DOM::QualifiedName qualified_name) : HTMLElement(document, move(qualified_name)) { set_prototype(&window().cached_web_prototype("HTMLTableRowElement")); } HTMLTableRowElement::~HTMLTableRowElement() = default; // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-cells JS::NonnullGCPtr HTMLTableRowElement::cells() const { // The cells attribute must return an HTMLCollection rooted at this tr element, // whose filter matches only td and th elements that are children of the tr element. // FIXME: This should return the same HTMLCollection object every time, // but that would cause a reference cycle since HTMLCollection refs the root. return DOM::HTMLCollection::create(const_cast(*this), [this](Element const& element) { return element.parent() == this && is(element); }); } // 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 = [&]() -> JS::GCPtr { 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] == 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 = [&]() -> JS::GCPtr { 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] == this) return i; } return -1; } }