HTMLTableRowElement.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/ElementFactory.h>
  8. #include <LibWeb/DOM/HTMLCollection.h>
  9. #include <LibWeb/HTML/HTMLTableCellElement.h>
  10. #include <LibWeb/HTML/HTMLTableElement.h>
  11. #include <LibWeb/HTML/HTMLTableRowElement.h>
  12. #include <LibWeb/HTML/HTMLTableSectionElement.h>
  13. #include <LibWeb/Namespace.h>
  14. namespace Web::HTML {
  15. HTMLTableRowElement::HTMLTableRowElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : HTMLElement(document, move(qualified_name))
  17. {
  18. set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLTableRowElement"));
  19. }
  20. HTMLTableRowElement::~HTMLTableRowElement() = default;
  21. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-cells
  22. JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableRowElement::cells() const
  23. {
  24. // The cells attribute must return an HTMLCollection rooted at this tr element,
  25. // whose filter matches only td and th elements that are children of the tr 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<HTMLTableRowElement&>(*this), [this](Element const& element) {
  29. return element.parent() == this
  30. && is<HTMLTableCellElement>(element);
  31. });
  32. }
  33. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-rowindex
  34. int HTMLTableRowElement::row_index() const
  35. {
  36. // The rowIndex attribute must, if this element has a parent table element,
  37. // or a parent tbody, thead, or tfoot element and a grandparent table element,
  38. // return the index of this tr element in that table element's rows collection.
  39. // If there is no such table element, then the attribute must return −1.
  40. auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
  41. if (!parent())
  42. return nullptr;
  43. if (is<HTMLTableElement>(*parent()))
  44. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
  45. if (is<HTMLTableSectionElement>(*parent()) && parent()->parent() && is<HTMLTableElement>(*parent()->parent()))
  46. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent()->parent())).rows();
  47. return nullptr;
  48. }();
  49. if (!rows_collection)
  50. return -1;
  51. auto rows = rows_collection->collect_matching_elements();
  52. for (size_t i = 0; i < rows.size(); ++i) {
  53. if (rows[i] == this)
  54. return i;
  55. }
  56. return -1;
  57. }
  58. int HTMLTableRowElement::section_row_index() const
  59. {
  60. // The sectionRowIndex attribute must, if this element has a parent table, tbody, thead, or tfoot element,
  61. // return the index of the tr element in the parent element's rows collection
  62. // (for tables, that's HTMLTableElement's rows collection; for table sections, that's HTMLTableSectionElement's rows collection).
  63. // If there is no such parent element, then the attribute must return −1.
  64. auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
  65. if (!parent())
  66. return nullptr;
  67. if (is<HTMLTableElement>(*parent()))
  68. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
  69. if (is<HTMLTableSectionElement>(*parent()))
  70. return static_cast<HTMLTableSectionElement const&>(*parent()).rows();
  71. return nullptr;
  72. }();
  73. if (!rows_collection)
  74. return -1;
  75. auto rows = rows_collection->collect_matching_elements();
  76. for (size_t i = 0; i < rows.size(); ++i) {
  77. if (rows[i] == this)
  78. return i;
  79. }
  80. return -1;
  81. }
  82. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-insertcell
  83. WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableCellElement>> HTMLTableRowElement::insert_cell(i32 index)
  84. {
  85. auto cells_collection = cells();
  86. auto cells_collection_size = static_cast<i32>(cells_collection->length());
  87. // 1. If index is less than −1 or greater than the number of elements in the cells collection, then throw an "IndexSizeError" DOMException.
  88. if (index < -1 || index > cells_collection_size)
  89. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of cells");
  90. // 2. Let table cell be the result of creating an element given this tr element's node document, td, and the HTML namespace.
  91. auto& table_cell = static_cast<HTMLTableCellElement&>(*DOM::create_element(document(), HTML::TagNames::td, Namespace::HTML));
  92. // 3. If index is equal to −1 or equal to the number of items in cells collection, then append table cell to this tr element.
  93. if (index == -1 || index == cells_collection_size)
  94. TRY(append_child(table_cell));
  95. // 4. Otherwise, insert table cell as a child of this tr element, immediately before the indexth td or th element in the cells collection.
  96. else
  97. insert_before(table_cell, cells_collection->item(index));
  98. // 5. Return table cell.
  99. return JS::NonnullGCPtr(table_cell);
  100. }
  101. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-deletecell
  102. WebIDL::ExceptionOr<void> HTMLTableRowElement::delete_cell(i32 index)
  103. {
  104. auto cells_collection = cells();
  105. auto cells_collection_size = static_cast<i32>(cells_collection->length());
  106. // 1. If index is less than −1 or greater than or equal to the number of elements in the cells collection, then throw an "IndexSizeError" DOMException.
  107. if (index < -1 || index >= cells_collection_size)
  108. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of cells");
  109. // 2. If index is −1, then remove the last element in the cells collection from its parent, or do nothing if the cells collection is empty.
  110. if (index == -1) {
  111. if (cells_collection_size > 0)
  112. cells_collection->item(cells_collection_size - 1)->remove();
  113. }
  114. // 3. Otherwise, remove the indexth element in the cells collection from its parent.
  115. else {
  116. cells_collection->item(index)->remove();
  117. }
  118. return {};
  119. }
  120. }