HTMLTableSectionElement.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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), DOM::HTMLCollection::Scope::Children, [](Element const& element) {
  36. return is<HTMLTableRowElement>(element);
  37. });
  38. }
  39. return *m_rows;
  40. }
  41. // https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-insertrow
  42. WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableRowElement>> HTMLTableSectionElement::insert_row(long index)
  43. {
  44. auto rows_collection = rows();
  45. auto rows_collection_size = static_cast<long>(rows_collection->length());
  46. // 1. If index is less than −1 or greater than the number of elements in the rows collection, throw an "IndexSizeError" DOMException.
  47. if (index < -1 || index > rows_collection_size)
  48. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of rows"_fly_string);
  49. // 2. Let table row be the result of creating an element given this element's node document, tr, and the HTML namespace.
  50. auto& table_row = static_cast<HTMLTableRowElement&>(*TRY(DOM::create_element(document(), TagNames::tr, Namespace::HTML)));
  51. // 3. If index is −1 or equal to the number of items in the rows collection, then append table row to this element.
  52. if (index == -1 || index == rows_collection_size)
  53. TRY(append_child(table_row));
  54. // 4. Otherwise, insert table row as a child of this element, immediately before the index-th tr element in the rows collection.
  55. else
  56. table_row.insert_before(*this, rows_collection->item(index));
  57. // 5. Return table row.
  58. return JS::NonnullGCPtr(table_row);
  59. }
  60. // https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-deleterow
  61. WebIDL::ExceptionOr<void> HTMLTableSectionElement::delete_row(long index)
  62. {
  63. auto rows_collection = rows();
  64. auto rows_collection_size = static_cast<long>(rows_collection->length());
  65. // 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.
  66. if (index < -1 || index >= rows_collection_size)
  67. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of rows"_fly_string);
  68. // 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.
  69. if (index == -1) {
  70. if (rows_collection_size > 0)
  71. rows_collection->item(rows_collection_size - 1)->remove();
  72. }
  73. // 3. Otherwise, remove the indexth element in the rows collection from this element.
  74. else {
  75. rows_collection->item(index)->remove();
  76. }
  77. return {};
  78. }
  79. }