HTMLTableSectionElement.cpp 3.8 KB

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