From 887fa18e32297290639f07fa12a7557f5ee33aa5 Mon Sep 17 00:00:00 2001 From: Adam Hodgen Date: Mon, 26 Apr 2021 16:36:40 +0100 Subject: [PATCH] LibWeb: Implement HTMLTableElement row attributes rows returns a HTMLCollection of all the tr elements contained within the table. We leave the SameObject attribute off the attribute in the IDL as we cannot currently return the same HTMLCollection every time (see the FIXME on DOM::Document::applets) The WrapperGenerator currently does not correctly handle the default value for the type long on insertRow. Currently not specifying the index will insert a row at index 0. --- .../CodeGenerators/WrapperGenerator.cpp | 6 ++ .../LibWeb/HTML/HTMLTableElement.cpp | 76 +++++++++++++++++++ .../Libraries/LibWeb/HTML/HTMLTableElement.h | 6 ++ .../LibWeb/HTML/HTMLTableElement.idl | 4 + 4 files changed, 92 insertions(+) diff --git a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 8364500ea36..e56e56edc8a 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -657,6 +657,12 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter auto @cpp_name@ = (u16)@js_name@@js_suffix@.to_u32(global_object); if (vm.exception()) @return_statement@ +)~~~"); + } else if (parameter.type.name == "long") { + scoped_generator.append(R"~~~( + auto @cpp_name@ = @js_name@@js_suffix@.to_i32(global_object); + if (vm.exception()) + @return_statement@ )~~~"); } else if (parameter.type.name == "EventHandler") { // x.onfoo = function() { ... } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp index 908f845ba4c..67a6bf43b3c 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableElement.cpp @@ -5,7 +5,11 @@ */ #include +#include +#include #include +#include +#include namespace Web::HTML { @@ -40,4 +44,76 @@ void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) c }); } +NonnullRefPtr 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()->tag_name() == TagNames::thead || element.parent_element()->tag_name() == TagNames::tbody || element.parent_element()->tag_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("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 tr; +} + +DOM::ExceptionOr HTMLTableElement::delete_row(long index) +{ + auto rows = this->rows(); + auto rows_length = rows->length(); + + if (index < -1 || index >= (long)rows_length) { + return DOM::IndexSizeError::create("Index is negative or greater than the number of rows"); + } + if (index == -1 && rows_length > 0) { + auto row_to_remove = rows->item(rows_length - 1); + row_to_remove->remove(false); + } else { + auto row_to_remove = rows->item(index); + row_to_remove->remove(false); + } + + return {}; +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableElement.h b/Userland/Libraries/LibWeb/HTML/HTMLTableElement.h index 94a389c79e2..5df428a0aa6 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableElement.h @@ -6,7 +6,9 @@ #pragma once +#include #include +#include namespace Web::HTML { @@ -17,6 +19,10 @@ public: HTMLTableElement(DOM::Document&, QualifiedName); virtual ~HTMLTableElement() override; + NonnullRefPtr rows(); + DOM::ExceptionOr> insert_row(long index); + DOM::ExceptionOr delete_row(long index); + private: virtual void apply_presentational_hints(CSS::StyleProperties&) const override; }; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLTableElement.idl index 0990efa4a1e..e007d88585c 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableElement.idl @@ -1,5 +1,9 @@ interface HTMLTableElement : HTMLElement { + readonly attribute HTMLCollection rows; + HTMLTableRowElement insertRow(optional long index = -1); + undefined deleteRow(long index); + [Reflect] attribute DOMString align; [Reflect] attribute DOMString border; [Reflect] attribute DOMString frame;