LibWeb: Implement HTMLTableSectionElement::insertRow()

This commit is contained in:
Idan Horowitz 2022-03-12 23:50:35 +02:00 committed by Andreas Kling
parent 57090f75ae
commit 009588eb28
Notes: sideshowbarker 2024-07-17 17:32:16 +09:00
3 changed files with 29 additions and 0 deletions

View File

@ -5,9 +5,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/HTML/HTMLTableRowElement.h>
#include <LibWeb/HTML/HTMLTableSectionElement.h>
#include <LibWeb/Namespace.h>
namespace Web::HTML {
@ -33,4 +35,28 @@ NonnullRefPtr<DOM::HTMLCollection> HTMLTableSectionElement::rows() const
});
}
// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-insertrow
DOM::ExceptionOr<NonnullRefPtr<HTMLTableRowElement>> HTMLTableSectionElement::insert_row(long index)
{
auto rows_collection = rows();
auto rows_collection_size = static_cast<long>(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<HTMLTableRowElement>(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;
}
}

View File

@ -19,6 +19,7 @@ public:
virtual ~HTMLTableSectionElement() override;
NonnullRefPtr<DOM::HTMLCollection> rows() const;
DOM::ExceptionOr<NonnullRefPtr<HTMLTableRowElement>> insert_row(long index);
};
}

View File

@ -1,5 +1,6 @@
#import <DOM/HTMLCollection.idl>
#import <HTML/HTMLElement.idl>
#import <HTML/HTMLTableRowElement.idl>
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);
};