2020-06-07 23:10:45 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-05-02 18:17:08 +01:00
|
|
|
|
* Copyright (c) 2021, Adam Hodgen <ant1441@gmail.com>
|
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-09-30 17:16:16 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2021-07-30 19:31:46 +01:00
|
|
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
2023-05-08 07:51:03 +02:00
|
|
|
|
#include <LibWeb/CSS/StyleProperties.h>
|
2023-03-23 21:12:15 +00:00
|
|
|
|
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
|
2021-04-26 16:36:40 +01:00
|
|
|
|
#include <LibWeb/DOM/ElementFactory.h>
|
|
|
|
|
#include <LibWeb/DOM/HTMLCollection.h>
|
2021-05-02 18:03:47 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLTableColElement.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
|
#include <LibWeb/HTML/HTMLTableElement.h>
|
2021-04-26 16:36:40 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLTableRowElement.h>
|
2022-03-26 14:29:52 +01:00
|
|
|
|
#include <LibWeb/HTML/Parser/HTMLParser.h>
|
2021-04-26 16:36:40 +01:00
|
|
|
|
#include <LibWeb/Namespace.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
|
|
|
|
HTMLTableElement::HTMLTableElement(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-03-14 13:21:51 -06:00
|
|
|
|
HTMLTableElement::~HTMLTableElement() = default;
|
2020-06-07 23:10:45 +02:00
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
|
JS::ThrowCompletionOr<void> HTMLTableElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2023-01-28 12:33:35 -05:00
|
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2023-01-10 06:28:20 -05:00
|
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTableElementPrototype>(realm, "HTMLTableElement"));
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
|
|
return {};
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:04:47 +01:00
|
|
|
|
void HTMLTableElement::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_rows);
|
|
|
|
|
visitor.visit(m_t_bodies);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-26 20:01:35 +02:00
|
|
|
|
void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) const
|
2020-06-12 22:52:38 +02:00
|
|
|
|
{
|
|
|
|
|
for_each_attribute([&](auto& name, auto& value) {
|
|
|
|
|
if (name == HTML::AttributeNames::width) {
|
2022-03-26 14:11:34 +01:00
|
|
|
|
if (auto parsed_value = parse_nonzero_dimension_value(value))
|
2020-06-28 12:43:22 +02:00
|
|
|
|
style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
|
2020-06-13 10:54:20 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-26 21:16:29 +02:00
|
|
|
|
if (name == HTML::AttributeNames::height) {
|
2022-03-26 14:11:34 +01:00
|
|
|
|
if (auto parsed_value = parse_nonzero_dimension_value(value))
|
2020-08-26 21:16:29 +02:00
|
|
|
|
style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-06-13 10:54:20 +02:00
|
|
|
|
if (name == HTML::AttributeNames::bgcolor) {
|
|
|
|
|
auto color = Color::from_string(value);
|
|
|
|
|
if (color.has_value())
|
2023-05-05 15:02:03 +01:00
|
|
|
|
style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()).release_value_but_fixme_should_propagate_errors());
|
2020-06-13 10:54:20 +02:00
|
|
|
|
return;
|
2020-06-12 22:52:38 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 19:48:22 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-caption
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::GCPtr<HTMLTableCaptionElement> HTMLTableElement::caption()
|
2021-05-02 17:43:42 +01:00
|
|
|
|
{
|
2022-12-15 19:48:22 +01:00
|
|
|
|
// The caption IDL attribute must return, on getting, the first caption element child of the table element,
|
|
|
|
|
// if any, or null otherwise.
|
2021-05-02 17:43:42 +01:00
|
|
|
|
return first_child_of_type<HTMLTableCaptionElement>();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 19:57:16 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-caption
|
2021-07-05 05:45:20 +01:00
|
|
|
|
void HTMLTableElement::set_caption(HTMLTableCaptionElement* caption)
|
2021-05-02 17:43:42 +01:00
|
|
|
|
{
|
2022-12-15 19:57:16 +01:00
|
|
|
|
// On setting, the first caption element child of the table element, if any, must be removed,
|
|
|
|
|
// and the new value, if not null, must be inserted as the first node of the table element.
|
2021-05-02 17:43:42 +01:00
|
|
|
|
delete_caption();
|
|
|
|
|
|
2022-12-15 19:57:16 +01:00
|
|
|
|
if (caption)
|
|
|
|
|
MUST(pre_insert(*caption, first_child()));
|
2021-05-02 17:43:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:11:01 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createcaption
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::NonnullGCPtr<HTMLTableCaptionElement> HTMLTableElement::create_caption()
|
2021-05-02 17:43:42 +01:00
|
|
|
|
{
|
|
|
|
|
auto maybe_caption = caption();
|
|
|
|
|
if (maybe_caption) {
|
|
|
|
|
return *maybe_caption;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 16:22:24 +01:00
|
|
|
|
auto caption = DOM::create_element(document(), TagNames::caption, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
|
2022-10-30 17:50:04 +00:00
|
|
|
|
MUST(pre_insert(caption, first_child()));
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return static_cast<HTMLTableCaptionElement&>(*caption);
|
2021-05-02 17:43:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:11:01 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletecaption
|
2021-05-02 17:43:42 +01:00
|
|
|
|
void HTMLTableElement::delete_caption()
|
|
|
|
|
{
|
|
|
|
|
auto maybe_caption = caption();
|
|
|
|
|
if (maybe_caption) {
|
|
|
|
|
maybe_caption->remove(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:04:44 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-thead
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::GCPtr<HTMLTableSectionElement> HTMLTableElement::t_head()
|
2021-05-02 18:03:47 +01:00
|
|
|
|
{
|
2022-12-15 20:04:44 +01:00
|
|
|
|
// The tHead IDL attribute must return, on getting, the first thead element child of the table element,
|
|
|
|
|
// if any, or null otherwise.
|
2021-05-02 18:03:47 +01:00
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
|
if (is<HTMLTableSectionElement>(*child)) {
|
2021-06-24 19:53:42 +02:00
|
|
|
|
auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
|
2021-07-10 20:47:32 +01:00
|
|
|
|
if (table_section_element->local_name() == TagNames::thead)
|
2021-05-02 18:03:47 +01:00
|
|
|
|
return table_section_element;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:04:44 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-thead
|
2022-09-25 17:03:42 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> HTMLTableElement::set_t_head(HTMLTableSectionElement* thead)
|
2021-05-02 18:03:47 +01:00
|
|
|
|
{
|
2022-12-15 20:04:44 +01:00
|
|
|
|
// If the new value is neither null nor a thead element, then a "HierarchyRequestError" DOMException must be thrown instead.
|
|
|
|
|
if (thead && thead->local_name() != TagNames::thead)
|
2022-09-25 16:38:21 -06:00
|
|
|
|
return WebIDL::HierarchyRequestError::create(realm(), "Element is not thead");
|
2021-05-02 18:03:47 +01:00
|
|
|
|
|
2022-12-15 20:04:44 +01:00
|
|
|
|
// On setting, if the new value is null or a thead element, the first thead element child of the table element,
|
|
|
|
|
// if any, must be removed,
|
2021-05-02 18:03:47 +01:00
|
|
|
|
delete_t_head();
|
|
|
|
|
|
2022-12-15 20:04:44 +01:00
|
|
|
|
if (!thead)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// and the new value, if not null, must be inserted immediately before the first element in the table element
|
|
|
|
|
// that is neither a caption element nor a colgroup element, if any,
|
|
|
|
|
// or at the end of the table if there are no such elements.
|
|
|
|
|
|
2021-05-02 18:03:47 +01:00
|
|
|
|
// We insert the new thead after any <caption> or <colgroup> elements
|
2022-12-16 19:14:26 +01:00
|
|
|
|
DOM::Node* child_to_insert_before = nullptr;
|
2021-05-02 18:03:47 +01:00
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
|
if (!is<HTMLElement>(*child))
|
|
|
|
|
continue;
|
|
|
|
|
if (is<HTMLTableCaptionElement>(*child))
|
|
|
|
|
continue;
|
|
|
|
|
if (is<HTMLTableColElement>(*child)) {
|
2021-06-24 19:53:42 +02:00
|
|
|
|
auto table_col_element = &verify_cast<HTMLTableColElement>(*child);
|
2021-07-10 20:47:32 +01:00
|
|
|
|
if (table_col_element->local_name() == TagNames::colgroup)
|
2021-05-02 18:03:47 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We have found an element which is not a <caption> or <colgroup>, we'll insert before this
|
2022-12-16 19:14:26 +01:00
|
|
|
|
child_to_insert_before = child;
|
2021-05-02 18:03:47 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 19:14:26 +01:00
|
|
|
|
TRY(pre_insert(*thead, child_to_insert_before));
|
2021-05-02 18:03:47 +01:00
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:11:01 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createthead
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_head()
|
2021-05-02 18:03:47 +01:00
|
|
|
|
{
|
|
|
|
|
auto maybe_thead = t_head();
|
|
|
|
|
if (maybe_thead)
|
|
|
|
|
return *maybe_thead;
|
|
|
|
|
|
2023-02-19 16:22:24 +01:00
|
|
|
|
auto thead = DOM::create_element(document(), TagNames::thead, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
|
2021-05-02 18:03:47 +01:00
|
|
|
|
|
|
|
|
|
// We insert the new thead after any <caption> or <colgroup> elements
|
2022-12-16 19:14:26 +01:00
|
|
|
|
DOM::Node* child_to_insert_before = nullptr;
|
2021-05-02 18:03:47 +01:00
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
|
if (!is<HTMLElement>(*child))
|
|
|
|
|
continue;
|
|
|
|
|
if (is<HTMLTableCaptionElement>(*child))
|
|
|
|
|
continue;
|
|
|
|
|
if (is<HTMLTableColElement>(*child)) {
|
2021-06-24 19:53:42 +02:00
|
|
|
|
auto table_col_element = &verify_cast<HTMLTableColElement>(*child);
|
2021-07-10 20:47:32 +01:00
|
|
|
|
if (table_col_element->local_name() == TagNames::colgroup)
|
2021-05-02 18:03:47 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We have found an element which is not a <caption> or <colgroup>, we'll insert before this
|
2022-12-16 19:14:26 +01:00
|
|
|
|
child_to_insert_before = child;
|
2021-05-02 18:03:47 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 19:14:26 +01:00
|
|
|
|
MUST(pre_insert(thead, child_to_insert_before));
|
2021-05-02 18:03:47 +01:00
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return static_cast<HTMLTableSectionElement&>(*thead);
|
2021-05-02 18:03:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:11:01 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletethead
|
2021-05-02 18:03:47 +01:00
|
|
|
|
void HTMLTableElement::delete_t_head()
|
|
|
|
|
{
|
|
|
|
|
auto maybe_thead = t_head();
|
|
|
|
|
if (maybe_thead) {
|
|
|
|
|
maybe_thead->remove(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:08:13 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tfoot
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::GCPtr<HTMLTableSectionElement> HTMLTableElement::t_foot()
|
2021-05-02 18:07:45 +01:00
|
|
|
|
{
|
2022-12-15 20:08:13 +01:00
|
|
|
|
// The tFoot IDL attribute must return, on getting, the first tfoot element child of the table element,
|
|
|
|
|
// if any, or null otherwise.
|
2021-05-02 18:07:45 +01:00
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
|
if (is<HTMLTableSectionElement>(*child)) {
|
2021-06-24 19:53:42 +02:00
|
|
|
|
auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
|
2021-07-10 20:47:32 +01:00
|
|
|
|
if (table_section_element->local_name() == TagNames::tfoot)
|
2021-05-02 18:07:45 +01:00
|
|
|
|
return table_section_element;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:08:13 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tfoot
|
2022-09-25 17:03:42 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> HTMLTableElement::set_t_foot(HTMLTableSectionElement* tfoot)
|
2021-05-02 18:07:45 +01:00
|
|
|
|
{
|
2022-12-15 20:08:13 +01:00
|
|
|
|
// If the new value is neither null nor a tfoot element, then a "HierarchyRequestError" DOMException must be thrown instead.
|
|
|
|
|
if (tfoot && tfoot->local_name() != TagNames::tfoot)
|
2022-09-25 16:38:21 -06:00
|
|
|
|
return WebIDL::HierarchyRequestError::create(realm(), "Element is not tfoot");
|
2021-05-02 18:07:45 +01:00
|
|
|
|
|
2022-12-15 20:08:13 +01:00
|
|
|
|
// On setting, if the new value is null or a tfoot element, the first tfoot element child of the table element,
|
|
|
|
|
// if any, must be removed,
|
2021-05-02 18:07:45 +01:00
|
|
|
|
delete_t_foot();
|
|
|
|
|
|
2022-12-15 20:08:13 +01:00
|
|
|
|
// and the new value, if not null, must be inserted at the end of the table.
|
|
|
|
|
if (tfoot) {
|
|
|
|
|
TRY(append_child(*tfoot));
|
|
|
|
|
}
|
2021-05-02 18:07:45 +01:00
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:11:01 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createtfoot
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_foot()
|
2021-05-02 18:07:45 +01:00
|
|
|
|
{
|
|
|
|
|
auto maybe_tfoot = t_foot();
|
|
|
|
|
if (maybe_tfoot)
|
|
|
|
|
return *maybe_tfoot;
|
|
|
|
|
|
2023-02-19 16:22:24 +01:00
|
|
|
|
auto tfoot = DOM::create_element(document(), TagNames::tfoot, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
|
2022-10-30 17:50:04 +00:00
|
|
|
|
MUST(append_child(tfoot));
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return static_cast<HTMLTableSectionElement&>(*tfoot);
|
2021-05-02 18:07:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:11:01 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletetfoot
|
2021-05-02 18:07:45 +01:00
|
|
|
|
void HTMLTableElement::delete_t_foot()
|
|
|
|
|
{
|
|
|
|
|
auto maybe_tfoot = t_foot();
|
|
|
|
|
if (maybe_tfoot) {
|
|
|
|
|
maybe_tfoot->remove(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:04:47 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-tbodies
|
2022-09-01 20:50:16 +02:00
|
|
|
|
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::t_bodies()
|
2021-05-02 18:17:08 +01:00
|
|
|
|
{
|
2022-11-24 19:04:47 +01:00
|
|
|
|
// The tBodies attribute must return an HTMLCollection rooted at the table node,
|
|
|
|
|
// whose filter matches only tbody elements that are children of the table element.
|
|
|
|
|
if (!m_t_bodies) {
|
|
|
|
|
m_t_bodies = DOM::HTMLCollection::create(*this, [](DOM::Element const& element) {
|
|
|
|
|
return element.local_name() == TagNames::tbody;
|
2023-02-14 22:53:08 +01:00
|
|
|
|
}).release_value_but_fixme_should_propagate_errors();
|
2022-11-24 19:04:47 +01:00
|
|
|
|
}
|
|
|
|
|
return *m_t_bodies;
|
2021-05-02 18:17:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:11:01 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-createtbody
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_body()
|
2021-05-02 18:17:08 +01:00
|
|
|
|
{
|
2023-02-19 16:22:24 +01:00
|
|
|
|
auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
|
2021-05-02 18:17:08 +01:00
|
|
|
|
|
|
|
|
|
// We insert the new tbody after the last <tbody> element
|
2022-12-16 19:14:26 +01:00
|
|
|
|
DOM::Node* child_to_insert_before = nullptr;
|
2021-05-02 18:17:08 +01:00
|
|
|
|
for (auto* child = last_child(); child; child = child->previous_sibling()) {
|
|
|
|
|
if (!is<HTMLElement>(*child))
|
|
|
|
|
continue;
|
|
|
|
|
if (is<HTMLTableSectionElement>(*child)) {
|
2021-06-24 19:53:42 +02:00
|
|
|
|
auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
|
2021-07-10 20:47:32 +01:00
|
|
|
|
if (table_section_element->local_name() == TagNames::tbody) {
|
2021-05-02 18:17:08 +01:00
|
|
|
|
// We have found an element which is a <tbody> we'll insert after this
|
2022-12-16 19:14:26 +01:00
|
|
|
|
child_to_insert_before = child->next_sibling();
|
2021-05-02 18:17:08 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 19:14:26 +01:00
|
|
|
|
MUST(pre_insert(tbody, child_to_insert_before));
|
2021-05-02 18:17:08 +01:00
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return static_cast<HTMLTableSectionElement&>(*tbody);
|
2021-05-02 18:17:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:04:47 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-rows
|
2022-09-01 20:50:16 +02:00
|
|
|
|
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::rows()
|
2021-04-26 16:36:40 +01:00
|
|
|
|
{
|
|
|
|
|
HTMLTableElement* table_node = this;
|
|
|
|
|
// FIXME: The elements in the collection must be ordered such that those elements whose parent is a thead are
|
|
|
|
|
// included first, in tree order, followed by those elements whose parent is either a table or tbody
|
|
|
|
|
// element, again in tree order, followed finally by those elements whose parent is a tfoot element,
|
|
|
|
|
// still in tree order.
|
|
|
|
|
// How do you sort HTMLCollection?
|
|
|
|
|
|
2022-11-24 19:04:47 +01:00
|
|
|
|
if (!m_rows) {
|
|
|
|
|
m_rows = DOM::HTMLCollection::create(*this, [table_node](DOM::Element const& element) {
|
|
|
|
|
// Only match TR elements which are:
|
|
|
|
|
// * children of the table element
|
|
|
|
|
// * children of the thead, tbody, or tfoot elements that are themselves children of the table element
|
|
|
|
|
if (!is<HTMLTableRowElement>(element)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (element.parent_element() == table_node)
|
|
|
|
|
return true;
|
2021-04-26 16:36:40 +01:00
|
|
|
|
|
2022-11-24 19:04:47 +01:00
|
|
|
|
if (element.parent_element() && (element.parent_element()->local_name() == TagNames::thead || element.parent_element()->local_name() == TagNames::tbody || element.parent_element()->local_name() == TagNames::tfoot)
|
|
|
|
|
&& element.parent()->parent() == table_node) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-04-26 16:36:40 +01:00
|
|
|
|
|
2022-11-24 19:04:47 +01:00
|
|
|
|
return false;
|
2023-02-14 22:53:08 +01:00
|
|
|
|
}).release_value_but_fixme_should_propagate_errors();
|
2022-11-24 19:04:47 +01:00
|
|
|
|
}
|
|
|
|
|
return *m_rows;
|
2021-04-26 16:36:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:11:01 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-insertrow
|
2022-09-25 17:03:42 +01:00
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableRowElement>> HTMLTableElement::insert_row(long index)
|
2021-04-26 16:36:40 +01:00
|
|
|
|
{
|
|
|
|
|
auto rows = this->rows();
|
|
|
|
|
auto rows_length = rows->length();
|
|
|
|
|
|
2022-03-12 23:48:05 +02:00
|
|
|
|
if (index < -1 || index > (long)rows_length) {
|
2022-09-25 16:38:21 -06:00
|
|
|
|
return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of rows");
|
2021-04-26 16:36:40 +01:00
|
|
|
|
}
|
2023-02-19 16:22:24 +01:00
|
|
|
|
auto& tr = static_cast<HTMLTableRowElement&>(*TRY(DOM::create_element(document(), TagNames::tr, Namespace::HTML)));
|
2021-04-26 16:36:40 +01:00
|
|
|
|
if (rows_length == 0 && !has_child_of_type<HTMLTableRowElement>()) {
|
2023-02-19 16:22:24 +01:00
|
|
|
|
auto tbody = TRY(DOM::create_element(document(), TagNames::tbody, Namespace::HTML));
|
2022-10-30 17:50:04 +00:00
|
|
|
|
TRY(tbody->append_child(tr));
|
|
|
|
|
TRY(append_child(tbody));
|
2021-04-26 16:36:40 +01:00
|
|
|
|
} else if (rows_length == 0) {
|
|
|
|
|
auto tbody = last_child_of_type<HTMLTableRowElement>();
|
2022-10-30 17:50:04 +00:00
|
|
|
|
TRY(tbody->append_child(tr));
|
2021-04-26 16:36:40 +01:00
|
|
|
|
} else if (index == -1 || index == (long)rows_length) {
|
|
|
|
|
auto parent_of_last_tr = rows->item(rows_length - 1)->parent_element();
|
2022-10-30 17:50:04 +00:00
|
|
|
|
TRY(parent_of_last_tr->append_child(tr));
|
2021-04-26 16:36:40 +01:00
|
|
|
|
} else {
|
|
|
|
|
rows->item(index)->parent_element()->insert_before(tr, rows->item(index));
|
|
|
|
|
}
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return JS::NonnullGCPtr(tr);
|
2021-04-26 16:36:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:08:55 +00:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-table-deleterow
|
2022-09-25 17:03:42 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> HTMLTableElement::delete_row(long index)
|
2021-04-26 16:36:40 +01:00
|
|
|
|
{
|
|
|
|
|
auto rows = this->rows();
|
|
|
|
|
auto rows_length = rows->length();
|
|
|
|
|
|
2021-12-15 09:08:55 +00:00
|
|
|
|
// 1. If index is less than −1 or greater than or equal to the number of elements in the rows collection, then throw an "IndexSizeError" DOMException.
|
|
|
|
|
if (index < -1 || index >= (long)rows_length)
|
2022-09-25 16:38:21 -06:00
|
|
|
|
return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of rows");
|
2021-12-15 09:08:55 +00:00
|
|
|
|
|
|
|
|
|
// 2. If index is −1, then remove the last element in the rows collection from its parent, or do nothing if the rows collection is empty.
|
|
|
|
|
if (index == -1) {
|
|
|
|
|
if (rows_length == 0)
|
|
|
|
|
return {};
|
|
|
|
|
|
2021-04-26 16:36:40 +01:00
|
|
|
|
auto row_to_remove = rows->item(rows_length - 1);
|
|
|
|
|
row_to_remove->remove(false);
|
2021-12-15 09:08:55 +00:00
|
|
|
|
return {};
|
2021-04-26 16:36:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:08:55 +00:00
|
|
|
|
// 3. Otherwise, remove the indexth element in the rows collection from its parent.
|
|
|
|
|
auto row_to_remove = rows->item(index);
|
|
|
|
|
row_to_remove->remove(false);
|
2021-04-26 16:36:40 +01:00
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 23:10:45 +02:00
|
|
|
|
}
|