HTMLTableSectionElement.cpp 3.7 KB

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