diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp index 9793ac88f7..404ba84f14 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.cpp @@ -5,11 +5,13 @@ */ #include +#include #include #include #include #include #include +#include namespace Web::HTML { @@ -85,4 +87,29 @@ int HTMLTableRowElement::section_row_index() const return -1; } +// https://html.spec.whatwg.org/multipage/tables.html#dom-tr-insertcell +WebIDL::ExceptionOr> HTMLTableRowElement::insert_cell(i32 index) +{ + auto cells_collection = cells(); + auto cells_collection_size = static_cast(cells_collection->length()); + + // 1. If index is less than −1 or greater than the number of elements in the cells collection, then throw an "IndexSizeError" DOMException. + if (index < -1 || index > cells_collection_size) + return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of cells"); + + // 2. Let table cell be the result of creating an element given this tr element's node document, td, and the HTML namespace. + auto& table_cell = static_cast(*DOM::create_element(document(), HTML::TagNames::td, Namespace::HTML)); + + // 3. If index is equal to −1 or equal to the number of items in cells collection, then append table cell to this tr element. + if (index == -1 || index == cells_collection_size) + TRY(append_child(table_cell)); + + // 4. Otherwise, insert table cell as a child of this tr element, immediately before the indexth td or th element in the cells collection. + else + insert_before(table_cell, cells_collection->item(index)); + + // 5. Return table cell. + return JS::NonnullGCPtr(table_cell); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.idl index 82c27c7ddf..0530647950 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLTableRowElement.idl @@ -1,5 +1,6 @@ #import #import +#import // https://html.spec.whatwg.org/multipage/tables.html#htmltablerowelement [Exposed=Window] @@ -16,5 +17,5 @@ interface HTMLTableRowElement : HTMLElement { readonly attribute long sectionRowIndex; [SameObject] readonly attribute HTMLCollection cells; - + HTMLTableCellElement insertCell(optional long index = -1); };