HTMLTableRowElement.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. }
  19. HTMLTableRowElement::~HTMLTableRowElement() = default;
  20. JS::ThrowCompletionOr<void> HTMLTableRowElement::initialize(JS::Realm& realm)
  21. {
  22. MUST_OR_THROW_OOM(Base::initialize(realm));
  23. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTableRowElementPrototype>(realm, "HTMLTableRowElement"));
  24. return {};
  25. }
  26. void HTMLTableRowElement::visit_edges(Cell::Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. visitor.visit(m_cells);
  30. }
  31. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-cells
  32. JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableRowElement::cells() const
  33. {
  34. // The cells attribute must return an HTMLCollection rooted at this tr element,
  35. // whose filter matches only td and th elements that are children of the tr element.
  36. if (!m_cells) {
  37. m_cells = DOM::HTMLCollection::create(const_cast<HTMLTableRowElement&>(*this), [this](Element const& element) {
  38. return element.parent() == this
  39. && is<HTMLTableCellElement>(element);
  40. }).release_value_but_fixme_should_propagate_errors();
  41. }
  42. return *m_cells;
  43. }
  44. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-rowindex
  45. int HTMLTableRowElement::row_index() const
  46. {
  47. // The rowIndex attribute must, if this element has a parent table element,
  48. // or a parent tbody, thead, or tfoot element and a grandparent table element,
  49. // return the index of this tr element in that table element's rows collection.
  50. // If there is no such table element, then the attribute must return −1.
  51. auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
  52. if (!parent())
  53. return nullptr;
  54. if (is<HTMLTableElement>(*parent()))
  55. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
  56. if (is<HTMLTableSectionElement>(*parent()) && parent()->parent() && is<HTMLTableElement>(*parent()->parent()))
  57. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent()->parent())).rows();
  58. return nullptr;
  59. }();
  60. if (!rows_collection)
  61. return -1;
  62. auto rows = rows_collection->collect_matching_elements();
  63. for (size_t i = 0; i < rows.size(); ++i) {
  64. if (rows[i] == this)
  65. return i;
  66. }
  67. return -1;
  68. }
  69. int HTMLTableRowElement::section_row_index() const
  70. {
  71. // The sectionRowIndex attribute must, if this element has a parent table, tbody, thead, or tfoot element,
  72. // return the index of the tr element in the parent element's rows collection
  73. // (for tables, that's HTMLTableElement's rows collection; for table sections, that's HTMLTableSectionElement's rows collection).
  74. // If there is no such parent element, then the attribute must return −1.
  75. auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
  76. if (!parent())
  77. return nullptr;
  78. if (is<HTMLTableElement>(*parent()))
  79. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
  80. if (is<HTMLTableSectionElement>(*parent()))
  81. return static_cast<HTMLTableSectionElement const&>(*parent()).rows();
  82. return nullptr;
  83. }();
  84. if (!rows_collection)
  85. return -1;
  86. auto rows = rows_collection->collect_matching_elements();
  87. for (size_t i = 0; i < rows.size(); ++i) {
  88. if (rows[i] == this)
  89. return i;
  90. }
  91. return -1;
  92. }
  93. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-insertcell
  94. WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableCellElement>> HTMLTableRowElement::insert_cell(i32 index)
  95. {
  96. auto cells_collection = cells();
  97. auto cells_collection_size = static_cast<i32>(cells_collection->length());
  98. // 1. If index is less than −1 or greater than the number of elements in the cells collection, then throw an "IndexSizeError" DOMException.
  99. if (index < -1 || index > cells_collection_size)
  100. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of cells");
  101. // 2. Let table cell be the result of creating an element given this tr element's node document, td, and the HTML namespace.
  102. auto& table_cell = static_cast<HTMLTableCellElement&>(*TRY(DOM::create_element(document(), HTML::TagNames::td, Namespace::HTML)));
  103. // 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.
  104. if (index == -1 || index == cells_collection_size)
  105. TRY(append_child(table_cell));
  106. // 4. Otherwise, insert table cell as a child of this tr element, immediately before the indexth td or th element in the cells collection.
  107. else
  108. insert_before(table_cell, cells_collection->item(index));
  109. // 5. Return table cell.
  110. return JS::NonnullGCPtr(table_cell);
  111. }
  112. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-deletecell
  113. WebIDL::ExceptionOr<void> HTMLTableRowElement::delete_cell(i32 index)
  114. {
  115. auto cells_collection = cells();
  116. auto cells_collection_size = static_cast<i32>(cells_collection->length());
  117. // 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.
  118. if (index < -1 || index >= cells_collection_size)
  119. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of cells");
  120. // 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.
  121. if (index == -1) {
  122. if (cells_collection_size > 0)
  123. cells_collection->item(cells_collection_size - 1)->remove();
  124. }
  125. // 3. Otherwise, remove the indexth element in the cells collection from its parent.
  126. else {
  127. cells_collection->item(index)->remove();
  128. }
  129. return {};
  130. }
  131. }