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-08-28 13:42:07 +02:00
|
|
|
|
#include <LibWeb/Bindings/HTMLTableElementPrototype.h>
|
2021-07-30 19:31:46 +01:00
|
|
|
|
#include <LibWeb/CSS/Parser/Parser.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>
|
2022-08-28 13:42:07 +02:00
|
|
|
|
#include <LibWeb/HTML/Window.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-08-28 13:42:07 +02:00
|
|
|
|
set_prototype(&window().ensure_web_prototype<Bindings::HTMLTableElementPrototype>("HTMLTableElement"));
|
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
|
|
|
|
|
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())
|
2020-07-26 20:01:35 +02:00
|
|
|
|
style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
|
2020-06-13 10:54:20 +02:00
|
|
|
|
return;
|
2020-06-12 22:52:38 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::GCPtr<HTMLTableCaptionElement> HTMLTableElement::caption()
|
2021-05-02 17:43:42 +01:00
|
|
|
|
{
|
|
|
|
|
return first_child_of_type<HTMLTableCaptionElement>();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 05:45:20 +01:00
|
|
|
|
void HTMLTableElement::set_caption(HTMLTableCaptionElement* caption)
|
2021-05-02 17:43:42 +01:00
|
|
|
|
{
|
2021-07-05 05:45:20 +01:00
|
|
|
|
// FIXME: This is not always the case, but this function is currently written in a way that assumes non-null.
|
|
|
|
|
VERIFY(caption);
|
|
|
|
|
|
2021-05-02 17:43:42 +01:00
|
|
|
|
// FIXME: The spec requires deleting the current caption if caption is null
|
|
|
|
|
// Currently the wrapper generator doesn't send us a nullable value
|
|
|
|
|
delete_caption();
|
|
|
|
|
|
2021-07-05 05:45:20 +01:00
|
|
|
|
pre_insert(*caption, first_child());
|
2021-05-02 17:43:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto caption = DOM::create_element(document(), TagNames::caption, Namespace::HTML);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HTMLTableElement::delete_caption()
|
|
|
|
|
{
|
|
|
|
|
auto maybe_caption = caption();
|
|
|
|
|
if (maybe_caption) {
|
|
|
|
|
maybe_caption->remove(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::GCPtr<HTMLTableSectionElement> HTMLTableElement::t_head()
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 05:45:20 +01:00
|
|
|
|
DOM::ExceptionOr<void> HTMLTableElement::set_t_head(HTMLTableSectionElement* thead)
|
2021-05-02 18:03:47 +01:00
|
|
|
|
{
|
2021-07-05 05:45:20 +01:00
|
|
|
|
// FIXME: This is not always the case, but this function is currently written in a way that assumes non-null.
|
|
|
|
|
VERIFY(thead);
|
|
|
|
|
|
2021-07-10 20:47:32 +01:00
|
|
|
|
if (thead->local_name() != TagNames::thead)
|
2021-05-02 18:03:47 +01:00
|
|
|
|
return DOM::HierarchyRequestError::create("Element is not thead");
|
|
|
|
|
|
|
|
|
|
// FIXME: The spec requires deleting the current thead if thead is null
|
|
|
|
|
// Currently the wrapper generator doesn't send us a nullable value
|
|
|
|
|
delete_t_head();
|
|
|
|
|
|
|
|
|
|
// We insert the new thead after any <caption> or <colgroup> elements
|
|
|
|
|
DOM::Node* child_to_append_after = nullptr;
|
|
|
|
|
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
|
|
|
|
|
child_to_append_after = child;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 05:45:20 +01:00
|
|
|
|
pre_insert(*thead, child_to_append_after);
|
2021-05-02 18:03:47 +01:00
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
auto thead = DOM::create_element(document(), TagNames::thead, Namespace::HTML);
|
|
|
|
|
|
|
|
|
|
// We insert the new thead after any <caption> or <colgroup> elements
|
|
|
|
|
DOM::Node* child_to_append_after = nullptr;
|
|
|
|
|
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
|
|
|
|
|
child_to_append_after = child;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pre_insert(thead, child_to_append_after);
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return static_cast<HTMLTableSectionElement&>(*thead);
|
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-08-28 13:42:07 +02:00
|
|
|
|
JS::GCPtr<HTMLTableSectionElement> HTMLTableElement::t_foot()
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 05:45:20 +01:00
|
|
|
|
DOM::ExceptionOr<void> HTMLTableElement::set_t_foot(HTMLTableSectionElement* tfoot)
|
2021-05-02 18:07:45 +01:00
|
|
|
|
{
|
2021-07-05 05:45:20 +01:00
|
|
|
|
// FIXME: This is not always the case, but this function is currently written in a way that assumes non-null.
|
|
|
|
|
VERIFY(tfoot);
|
|
|
|
|
|
2021-07-10 20:47:32 +01:00
|
|
|
|
if (tfoot->local_name() != TagNames::tfoot)
|
2021-05-02 18:07:45 +01:00
|
|
|
|
return DOM::HierarchyRequestError::create("Element is not tfoot");
|
|
|
|
|
|
|
|
|
|
// FIXME: The spec requires deleting the current tfoot if tfoot is null
|
|
|
|
|
// Currently the wrapper generator doesn't send us a nullable value
|
|
|
|
|
delete_t_foot();
|
|
|
|
|
|
|
|
|
|
// We insert the new tfoot at the end of the table
|
2021-07-05 05:45:20 +01:00
|
|
|
|
append_child(*tfoot);
|
2021-05-02 18:07:45 +01:00
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
auto tfoot = DOM::create_element(document(), TagNames::tfoot, Namespace::HTML);
|
|
|
|
|
append_child(tfoot);
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return static_cast<HTMLTableSectionElement&>(*tfoot);
|
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-09-01 20:50:16 +02:00
|
|
|
|
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::t_bodies()
|
2021-05-02 18:17:08 +01:00
|
|
|
|
{
|
|
|
|
|
return DOM::HTMLCollection::create(*this, [](DOM::Element const& element) {
|
2021-07-10 20:47:32 +01:00
|
|
|
|
return element.local_name() == TagNames::tbody;
|
2021-05-02 18:17:08 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_body()
|
2021-05-02 18:17:08 +01:00
|
|
|
|
{
|
|
|
|
|
auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML);
|
|
|
|
|
|
|
|
|
|
// We insert the new tbody after the last <tbody> element
|
|
|
|
|
DOM::Node* child_to_append_after = nullptr;
|
|
|
|
|
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
|
|
|
|
|
child_to_append_after = child->next_sibling();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pre_insert(tbody, child_to_append_after);
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return static_cast<HTMLTableSectionElement&>(*tbody);
|
2021-05-02 18:17:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
|
|
return 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-07-10 20:47:32 +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)
|
2021-04-26 16:36:40 +01:00
|
|
|
|
&& element.parent()->parent() == table_node) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
DOM::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) {
|
2021-04-26 16:36:40 +01:00
|
|
|
|
return DOM::IndexSizeError::create("Index is negative or greater than the number of rows");
|
|
|
|
|
}
|
2022-08-28 13:42:07 +02:00
|
|
|
|
auto& tr = static_cast<HTMLTableRowElement&>(*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>()) {
|
|
|
|
|
auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML);
|
|
|
|
|
tbody->append_child(tr);
|
|
|
|
|
append_child(tbody);
|
|
|
|
|
} else if (rows_length == 0) {
|
|
|
|
|
auto tbody = last_child_of_type<HTMLTableRowElement>();
|
|
|
|
|
tbody->append_child(tr);
|
|
|
|
|
} else if (index == -1 || index == (long)rows_length) {
|
|
|
|
|
auto parent_of_last_tr = rows->item(rows_length - 1)->parent_element();
|
|
|
|
|
parent_of_last_tr->append_child(tr);
|
|
|
|
|
} 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
|
2021-04-26 16:36:40 +01:00
|
|
|
|
DOM::ExceptionOr<void> HTMLTableElement::delete_row(long index)
|
|
|
|
|
{
|
|
|
|
|
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-03-12 23:48:05 +02:00
|
|
|
|
return DOM::IndexSizeError::create("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
|
|
|
|
}
|