2020-08-01 03:07:00 +01:00
|
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2022-02-26 11:43:52 +01:00
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
2020-08-01 03:07:00 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 03:07:00 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2022-03-12 23:50:35 +02:00
|
|
|
|
#include <LibWeb/DOM/ElementFactory.h>
|
2022-02-26 11:43:52 +01:00
|
|
|
|
#include <LibWeb/DOM/HTMLCollection.h>
|
|
|
|
|
#include <LibWeb/HTML/HTMLTableRowElement.h>
|
2020-08-01 03:07:00 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLTableSectionElement.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
|
#include <LibWeb/HTML/Window.h>
|
2022-03-12 23:50:35 +02:00
|
|
|
|
#include <LibWeb/Namespace.h>
|
2020-08-01 03:07:00 +01:00
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
|
HTMLTableSectionElement::HTMLTableSectionElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 11:20:15 +01:00
|
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-08-01 03:07:00 +01:00
|
|
|
|
{
|
2022-09-03 18:43:24 +02:00
|
|
|
|
set_prototype(&window().cached_web_prototype("HTMLTableSectionElement"));
|
2020-08-01 03:07:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
|
HTMLTableSectionElement::~HTMLTableSectionElement() = default;
|
2020-08-01 03:07:00 +01:00
|
|
|
|
|
2022-02-26 11:43:52 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-rows
|
2022-09-01 20:50:16 +02:00
|
|
|
|
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableSectionElement::rows() const
|
2022-02-26 11:43:52 +01:00
|
|
|
|
{
|
|
|
|
|
// The rows attribute must return an HTMLCollection rooted at this element,
|
|
|
|
|
// whose filter matches only tr elements that are children of this element.
|
|
|
|
|
// FIXME: This should return the same HTMLCollection object every time,
|
|
|
|
|
// but that would cause a reference cycle since HTMLCollection refs the root.
|
|
|
|
|
return DOM::HTMLCollection::create(const_cast<HTMLTableSectionElement&>(*this), [this](Element const& element) {
|
|
|
|
|
return element.parent() == this
|
|
|
|
|
&& is<HTMLTableRowElement>(element);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 23:50:35 +02:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-insertrow
|
2022-09-25 17:03:42 +01:00
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableRowElement>> HTMLTableSectionElement::insert_row(long index)
|
2022-03-12 23:50:35 +02:00
|
|
|
|
{
|
|
|
|
|
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)
|
2022-09-25 17:28:46 +01:00
|
|
|
|
return WebIDL::IndexSizeError::create(global_object(), "Index is negative or greater than the number of rows");
|
2022-03-12 23:50:35 +02:00
|
|
|
|
|
|
|
|
|
// 2. Let table row be the result of creating an element given this element's node document, tr, and the HTML namespace.
|
2022-08-28 13:42:07 +02:00
|
|
|
|
auto& table_row = static_cast<HTMLTableRowElement&>(*DOM::create_element(document(), TagNames::tr, Namespace::HTML));
|
2022-03-12 23:50:35 +02:00
|
|
|
|
|
|
|
|
|
// 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
|
2022-08-28 13:42:07 +02:00
|
|
|
|
table_row.insert_before(*this, rows_collection->item(index));
|
2022-03-12 23:50:35 +02:00
|
|
|
|
|
|
|
|
|
// 5. Return table row.
|
2022-08-28 13:42:07 +02:00
|
|
|
|
return JS::NonnullGCPtr(table_row);
|
2022-03-12 23:50:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 23:50:53 +02:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-deleterow
|
2022-09-25 17:03:42 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> HTMLTableSectionElement::delete_row(long index)
|
2022-03-12 23:50:53 +02:00
|
|
|
|
{
|
|
|
|
|
auto rows_collection = rows();
|
|
|
|
|
auto rows_collection_size = static_cast<long>(rows_collection->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 >= rows_collection_size)
|
2022-09-25 17:28:46 +01:00
|
|
|
|
return WebIDL::IndexSizeError::create(global_object(), "Index is negative or greater than or equal to the number of rows");
|
2022-03-12 23:50:53 +02:00
|
|
|
|
|
|
|
|
|
// 2. If index is −1, then remove the last element in the rows collection from this element, or do nothing if the rows collection is empty.
|
|
|
|
|
if (index == -1) {
|
|
|
|
|
if (rows_collection_size > 0)
|
|
|
|
|
rows_collection->item(rows_collection_size - 1)->remove();
|
|
|
|
|
}
|
|
|
|
|
// 3. Otherwise, remove the indexth element in the rows collection from this element.
|
|
|
|
|
else {
|
|
|
|
|
rows_collection->item(index)->remove();
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 03:07:00 +01:00
|
|
|
|
}
|