HTMLTableRowElement.cpp 6.4 KB

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