HTMLTableRowElement.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/HTMLCollection.h>
  7. #include <LibWeb/HTML/HTMLTableCellElement.h>
  8. #include <LibWeb/HTML/HTMLTableElement.h>
  9. #include <LibWeb/HTML/HTMLTableRowElement.h>
  10. #include <LibWeb/HTML/HTMLTableSectionElement.h>
  11. namespace Web::HTML {
  12. HTMLTableRowElement::HTMLTableRowElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : HTMLElement(document, move(qualified_name))
  14. {
  15. }
  16. HTMLTableRowElement::~HTMLTableRowElement() = default;
  17. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-cells
  18. NonnullRefPtr<DOM::HTMLCollection> HTMLTableRowElement::cells() const
  19. {
  20. // The cells attribute must return an HTMLCollection rooted at this tr element,
  21. // whose filter matches only td and th elements that are children of the tr element.
  22. // FIXME: This should return the same HTMLCollection object every time,
  23. // but that would cause a reference cycle since HTMLCollection refs the root.
  24. return DOM::HTMLCollection::create(const_cast<HTMLTableRowElement&>(*this), [this](Element const& element) {
  25. return element.parent() == this
  26. && is<HTMLTableCellElement>(element);
  27. });
  28. }
  29. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-rowindex
  30. int HTMLTableRowElement::row_index() const
  31. {
  32. // The rowIndex attribute must, if this element has a parent table element,
  33. // or a parent tbody, thead, or tfoot element and a grandparent table element,
  34. // return the index of this tr element in that table element's rows collection.
  35. // If there is no such table element, then the attribute must return −1.
  36. auto rows_collection = [&]() -> RefPtr<DOM::HTMLCollection> {
  37. if (!parent())
  38. return nullptr;
  39. if (is<HTMLTableElement>(*parent()))
  40. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
  41. if (is<HTMLTableSectionElement>(*parent()) && parent()->parent() && is<HTMLTableElement>(*parent()->parent()))
  42. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent()->parent())).rows();
  43. return nullptr;
  44. }();
  45. if (!rows_collection)
  46. return -1;
  47. auto rows = rows_collection->collect_matching_elements();
  48. for (size_t i = 0; i < rows.size(); ++i) {
  49. if (rows[i].ptr() == this)
  50. return i;
  51. }
  52. return -1;
  53. }
  54. int HTMLTableRowElement::section_row_index() const
  55. {
  56. // The sectionRowIndex attribute must, if this element has a parent table, tbody, thead, or tfoot element,
  57. // return the index of the tr element in the parent element's rows collection
  58. // (for tables, that's HTMLTableElement's rows collection; for table sections, that's HTMLTableSectionElement's rows collection).
  59. // If there is no such parent element, then the attribute must return −1.
  60. auto rows_collection = [&]() -> RefPtr<DOM::HTMLCollection> {
  61. if (!parent())
  62. return nullptr;
  63. if (is<HTMLTableElement>(*parent()))
  64. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
  65. if (is<HTMLTableSectionElement>(*parent()))
  66. return static_cast<HTMLTableSectionElement const&>(*parent()).rows();
  67. return nullptr;
  68. }();
  69. if (!rows_collection)
  70. return -1;
  71. auto rows = rows_collection->collect_matching_elements();
  72. for (size_t i = 0; i < rows.size(); ++i) {
  73. if (rows[i].ptr() == this)
  74. return i;
  75. }
  76. return -1;
  77. }
  78. }