HTMLTableSectionElement.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. void HTMLTableSectionElement::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTableSectionElementPrototype>(realm, "HTMLTableSectionElement"));
  23. }
  24. void HTMLTableSectionElement::visit_edges(Cell::Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. visitor.visit(m_rows);
  28. }
  29. // https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-rows
  30. JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableSectionElement::rows() const
  31. {
  32. // The rows attribute must return an HTMLCollection rooted at this element,
  33. // whose filter matches only tr elements that are children of this element.
  34. if (!m_rows) {
  35. m_rows = DOM::HTMLCollection::create(const_cast<HTMLTableSectionElement&>(*this), [this](Element const& element) {
  36. return element.parent() == this
  37. && is<HTMLTableRowElement>(element);
  38. });
  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&>(*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. }