/* * Copyright (c) 2020, Andreas Kling * Copyright (c) 2021, Adam Hodgen * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include namespace Web::HTML { HTMLTableElement::HTMLTableElement(DOM::Document& document, DOM::QualifiedName qualified_name) : HTMLElement(document, move(qualified_name)) { set_prototype(&window().cached_web_prototype("HTMLTableElement")); } HTMLTableElement::~HTMLTableElement() = default; void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) const { for_each_attribute([&](auto& name, auto& value) { if (name == HTML::AttributeNames::width) { if (auto parsed_value = parse_nonzero_dimension_value(value)) style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull()); return; } if (name == HTML::AttributeNames::height) { if (auto parsed_value = parse_nonzero_dimension_value(value)) style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull()); return; } if (name == HTML::AttributeNames::bgcolor) { auto color = Color::from_string(value); if (color.has_value()) style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value())); return; } }); } JS::GCPtr HTMLTableElement::caption() { return first_child_of_type(); } void HTMLTableElement::set_caption(HTMLTableCaptionElement* caption) { // FIXME: This is not always the case, but this function is currently written in a way that assumes non-null. VERIFY(caption); // 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(); pre_insert(*caption, first_child()); } JS::NonnullGCPtr HTMLTableElement::create_caption() { 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()); return static_cast(*caption); } void HTMLTableElement::delete_caption() { auto maybe_caption = caption(); if (maybe_caption) { maybe_caption->remove(false); } } JS::GCPtr HTMLTableElement::t_head() { for (auto* child = first_child(); child; child = child->next_sibling()) { if (is(*child)) { auto table_section_element = &verify_cast(*child); if (table_section_element->local_name() == TagNames::thead) return table_section_element; } } return nullptr; } DOM::ExceptionOr HTMLTableElement::set_t_head(HTMLTableSectionElement* thead) { // FIXME: This is not always the case, but this function is currently written in a way that assumes non-null. VERIFY(thead); if (thead->local_name() != TagNames::thead) return DOM::HierarchyRequestError::create(global_object(), "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 or elements DOM::Node* child_to_append_after = nullptr; for (auto* child = first_child(); child; child = child->next_sibling()) { if (!is(*child)) continue; if (is(*child)) continue; if (is(*child)) { auto table_col_element = &verify_cast(*child); if (table_col_element->local_name() == TagNames::colgroup) continue; } // We have found an element which is not a or , we'll insert before this child_to_append_after = child; break; } pre_insert(*thead, child_to_append_after); return {}; } JS::NonnullGCPtr HTMLTableElement::create_t_head() { 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 or elements DOM::Node* child_to_append_after = nullptr; for (auto* child = first_child(); child; child = child->next_sibling()) { if (!is(*child)) continue; if (is(*child)) continue; if (is(*child)) { auto table_col_element = &verify_cast(*child); if (table_col_element->local_name() == TagNames::colgroup) continue; } // We have found an element which is not a or , we'll insert before this child_to_append_after = child; break; } pre_insert(thead, child_to_append_after); return static_cast(*thead); } void HTMLTableElement::delete_t_head() { auto maybe_thead = t_head(); if (maybe_thead) { maybe_thead->remove(false); } } JS::GCPtr HTMLTableElement::t_foot() { for (auto* child = first_child(); child; child = child->next_sibling()) { if (is(*child)) { auto table_section_element = &verify_cast(*child); if (table_section_element->local_name() == TagNames::tfoot) return table_section_element; } } return nullptr; } DOM::ExceptionOr HTMLTableElement::set_t_foot(HTMLTableSectionElement* tfoot) { // FIXME: This is not always the case, but this function is currently written in a way that assumes non-null. VERIFY(tfoot); if (tfoot->local_name() != TagNames::tfoot) return DOM::HierarchyRequestError::create(global_object(), "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 append_child(*tfoot); return {}; } JS::NonnullGCPtr HTMLTableElement::create_t_foot() { auto maybe_tfoot = t_foot(); if (maybe_tfoot) return *maybe_tfoot; auto tfoot = DOM::create_element(document(), TagNames::tfoot, Namespace::HTML); append_child(tfoot); return static_cast(*tfoot); } void HTMLTableElement::delete_t_foot() { auto maybe_tfoot = t_foot(); if (maybe_tfoot) { maybe_tfoot->remove(false); } } JS::NonnullGCPtr HTMLTableElement::t_bodies() { return DOM::HTMLCollection::create(*this, [](DOM::Element const& element) { return element.local_name() == TagNames::tbody; }); } JS::NonnullGCPtr HTMLTableElement::create_t_body() { auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML); // We insert the new tbody after the last element DOM::Node* child_to_append_after = nullptr; for (auto* child = last_child(); child; child = child->previous_sibling()) { if (!is(*child)) continue; if (is(*child)) { auto table_section_element = &verify_cast(*child); if (table_section_element->local_name() == TagNames::tbody) { // We have found an element which is a we'll insert after this child_to_append_after = child->next_sibling(); break; } } } pre_insert(tbody, child_to_append_after); return static_cast(*tbody); } JS::NonnullGCPtr HTMLTableElement::rows() { 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(element)) { return false; } if (element.parent_element() == table_node) return true; 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; } return false; }); } DOM::ExceptionOr> HTMLTableElement::insert_row(long index) { auto rows = this->rows(); auto rows_length = rows->length(); if (index < -1 || index > (long)rows_length) { return DOM::IndexSizeError::create(global_object(), "Index is negative or greater than the number of rows"); } auto& tr = static_cast(*DOM::create_element(document(), TagNames::tr, Namespace::HTML)); if (rows_length == 0 && !has_child_of_type()) { 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(); 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)); } return JS::NonnullGCPtr(tr); } // https://html.spec.whatwg.org/multipage/tables.html#dom-table-deleterow DOM::ExceptionOr HTMLTableElement::delete_row(long index) { auto rows = this->rows(); auto rows_length = rows->length(); // 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) return DOM::IndexSizeError::create(global_object(), "Index is negative or greater than or equal to the number of rows"); // 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 {}; auto row_to_remove = rows->item(rows_length - 1); row_to_remove->remove(false); return {}; } // 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); return {}; } }