HTMLTableSectionElement.cpp 3.8 KB

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