2020-06-07 23:10:45 +02:00
|
|
|
|
/*
|
2022-02-26 11:43:52 +01:00
|
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
|
2020-06-07 23:10:45 +02:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-07 23:10:45 +02:00
|
|
|
|
*/
|
|
|
|
|
|
2022-02-26 11:43:52 +01:00
|
|
|
|
#include <LibWeb/DOM/HTMLCollection.h>
|
|
|
|
|
#include <LibWeb/HTML/HTMLTableCellElement.h>
|
2022-03-21 16:15:10 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLTableElement.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
|
#include <LibWeb/HTML/HTMLTableRowElement.h>
|
2022-03-21 16:15:10 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLTableSectionElement.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
|
#include <LibWeb/HTML/Window.h>
|
2020-06-07 23:10:45 +02:00
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
|
namespace Web::HTML {
|
2020-06-07 23:10:45 +02:00
|
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
|
HTMLTableRowElement::HTMLTableRowElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 11:20:15 +01:00
|
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-06-07 23:10:45 +02:00
|
|
|
|
{
|
2022-09-25 16:38:21 -06:00
|
|
|
|
set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLTableRowElement"));
|
2020-06-07 23:10:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
|
HTMLTableRowElement::~HTMLTableRowElement() = default;
|
2020-06-07 23:10:45 +02:00
|
|
|
|
|
2022-02-26 11:43:52 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-tr-cells
|
2022-09-01 20:50:16 +02:00
|
|
|
|
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableRowElement::cells() const
|
2022-02-26 11:43:52 +01:00
|
|
|
|
{
|
|
|
|
|
// 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<HTMLTableRowElement&>(*this), [this](Element const& element) {
|
|
|
|
|
return element.parent() == this
|
|
|
|
|
&& is<HTMLTableCellElement>(element);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 16:15:10 +01:00
|
|
|
|
// 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.
|
2022-09-01 20:50:16 +02:00
|
|
|
|
auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
|
2022-03-21 16:15:10 +01:00
|
|
|
|
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) {
|
2022-08-28 13:42:07 +02:00
|
|
|
|
if (rows[i] == this)
|
2022-03-21 16:15:10 +01:00
|
|
|
|
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.
|
2022-09-01 20:50:16 +02:00
|
|
|
|
auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
|
2022-03-21 16:15:10 +01:00
|
|
|
|
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) {
|
2022-08-28 13:42:07 +02:00
|
|
|
|
if (rows[i] == this)
|
2022-03-21 16:15:10 +01:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 23:10:45 +02:00
|
|
|
|
}
|