HTMLTableSectionElement.cpp 3.6 KB

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