From 009588eb28a0ed86c2cc5140c36797cd789c059f Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 12 Mar 2022 23:50:35 +0200 Subject: [PATCH] LibWeb: Implement HTMLTableSectionElement::insertRow() --- .../LibWeb/HTML/HTMLTableSectionElement.cpp | 26 +++++++++++++++++++ .../LibWeb/HTML/HTMLTableSectionElement.h | 1 + .../LibWeb/HTML/HTMLTableSectionElement.idl | 2 ++ 3 files changed, 29 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.cpp index 0e08d6f8f6..187a40e521 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.cpp @@ -5,9 +5,11 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include +#include namespace Web::HTML { @@ -33,4 +35,28 @@ NonnullRefPtr HTMLTableSectionElement::rows() const }); } +// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-insertrow +DOM::ExceptionOr> HTMLTableSectionElement::insert_row(long index) +{ + auto rows_collection = rows(); + auto rows_collection_size = static_cast(rows_collection->length()); + + // 1. If index is less than −1 or greater than the number of elements in the rows collection, throw an "IndexSizeError" DOMException. + if (index < -1 || index > rows_collection_size) + return DOM::IndexSizeError::create("Index is negative or greater than the number of rows"); + + // 2. Let table row be the result of creating an element given this element's node document, tr, and the HTML namespace. + auto table_row = static_ptr_cast(DOM::create_element(document(), TagNames::tr, Namespace::HTML)); + + // 3. If index is −1 or equal to the number of items in the rows collection, then append table row to this element. + if (index == -1 || index == rows_collection_size) + append_child(table_row); + // 4. Otherwise, insert table row as a child of this element, immediately before the index-th tr element in the rows collection. + else + table_row->insert_before(*this, rows_collection->item(index)); + + // 5. Return table row. + return table_row; +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.h b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.h index ebba9b8322..d6db3c28e0 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.h @@ -19,6 +19,7 @@ public: virtual ~HTMLTableSectionElement() override; NonnullRefPtr rows() const; + DOM::ExceptionOr> insert_row(long index); }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.idl index 8c7325a338..2f74fa52b4 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableSectionElement.idl @@ -1,5 +1,6 @@ #import #import +#import interface HTMLTableSectionElement : HTMLElement { @@ -9,5 +10,6 @@ interface HTMLTableSectionElement : HTMLElement { [Reflect=valign] attribute DOMString vAlign; [SameObject] readonly attribute HTMLCollection rows; + HTMLTableRowElement insertRow(optional long index = -1); };