HTMLTableSectionElement.cpp 4.0 KB

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