Serenity Operating System
at master 98 lines 4.1 kB view raw
1/* 2 * Copyright (c) 2020, the SerenityOS developers. 3 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <LibWeb/Bindings/Intrinsics.h> 9#include <LibWeb/DOM/ElementFactory.h> 10#include <LibWeb/DOM/HTMLCollection.h> 11#include <LibWeb/HTML/HTMLTableRowElement.h> 12#include <LibWeb/HTML/HTMLTableSectionElement.h> 13#include <LibWeb/Namespace.h> 14 15namespace Web::HTML { 16 17HTMLTableSectionElement::HTMLTableSectionElement(DOM::Document& document, DOM::QualifiedName qualified_name) 18 : HTMLElement(document, move(qualified_name)) 19{ 20} 21 22HTMLTableSectionElement::~HTMLTableSectionElement() = default; 23 24JS::ThrowCompletionOr<void> HTMLTableSectionElement::initialize(JS::Realm& realm) 25{ 26 MUST_OR_THROW_OOM(Base::initialize(realm)); 27 set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTableSectionElementPrototype>(realm, "HTMLTableSectionElement")); 28 29 return {}; 30} 31 32void HTMLTableSectionElement::visit_edges(Cell::Visitor& visitor) 33{ 34 Base::visit_edges(visitor); 35 visitor.visit(m_rows); 36} 37 38// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-rows 39JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableSectionElement::rows() const 40{ 41 // The rows attribute must return an HTMLCollection rooted at this element, 42 // whose filter matches only tr elements that are children of this element. 43 if (!m_rows) { 44 m_rows = DOM::HTMLCollection::create(const_cast<HTMLTableSectionElement&>(*this), [this](Element const& element) { 45 return element.parent() == this 46 && is<HTMLTableRowElement>(element); 47 }).release_value_but_fixme_should_propagate_errors(); 48 } 49 return *m_rows; 50} 51 52// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-insertrow 53WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableRowElement>> HTMLTableSectionElement::insert_row(long index) 54{ 55 auto rows_collection = rows(); 56 auto rows_collection_size = static_cast<long>(rows_collection->length()); 57 58 // 1. If index is less than −1 or greater than the number of elements in the rows collection, throw an "IndexSizeError" DOMException. 59 if (index < -1 || index > rows_collection_size) 60 return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of rows"); 61 62 // 2. Let table row be the result of creating an element given this element's node document, tr, and the HTML namespace. 63 auto& table_row = static_cast<HTMLTableRowElement&>(*TRY(DOM::create_element(document(), TagNames::tr, Namespace::HTML))); 64 65 // 3. If index is −1 or equal to the number of items in the rows collection, then append table row to this element. 66 if (index == -1 || index == rows_collection_size) 67 TRY(append_child(table_row)); 68 // 4. Otherwise, insert table row as a child of this element, immediately before the index-th tr element in the rows collection. 69 else 70 table_row.insert_before(*this, rows_collection->item(index)); 71 72 // 5. Return table row. 73 return JS::NonnullGCPtr(table_row); 74} 75 76// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-deleterow 77WebIDL::ExceptionOr<void> HTMLTableSectionElement::delete_row(long index) 78{ 79 auto rows_collection = rows(); 80 auto rows_collection_size = static_cast<long>(rows_collection->length()); 81 82 // 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. 83 if (index < -1 || index >= rows_collection_size) 84 return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of rows"); 85 86 // 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. 87 if (index == -1) { 88 if (rows_collection_size > 0) 89 rows_collection->item(rows_collection_size - 1)->remove(); 90 } 91 // 3. Otherwise, remove the indexth element in the rows collection from this element. 92 else { 93 rows_collection->item(index)->remove(); 94 } 95 return {}; 96} 97 98}