HTMLTableRowElement.cpp 3.6 KB

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