HTMLTableRowElement.cpp 3.7 KB

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