HTMLTableRowElement.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/CSS/StyleProperties.h>
  8. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  9. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/ElementFactory.h>
  12. #include <LibWeb/DOM/HTMLCollection.h>
  13. #include <LibWeb/HTML/HTMLTableCellElement.h>
  14. #include <LibWeb/HTML/HTMLTableElement.h>
  15. #include <LibWeb/HTML/HTMLTableRowElement.h>
  16. #include <LibWeb/HTML/HTMLTableSectionElement.h>
  17. #include <LibWeb/HTML/Parser/HTMLParser.h>
  18. #include <LibWeb/Namespace.h>
  19. namespace Web::HTML {
  20. HTMLTableRowElement::HTMLTableRowElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : HTMLElement(document, move(qualified_name))
  22. {
  23. }
  24. HTMLTableRowElement::~HTMLTableRowElement() = default;
  25. void HTMLTableRowElement::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTableRowElementPrototype>(realm, "HTMLTableRowElement"));
  29. }
  30. void HTMLTableRowElement::apply_presentational_hints(CSS::StyleProperties& style) const
  31. {
  32. Base::apply_presentational_hints(style);
  33. for_each_attribute([&](auto& name, auto& value) {
  34. if (name == HTML::AttributeNames::bgcolor) {
  35. // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value
  36. auto color = parse_legacy_color_value(value);
  37. if (color.has_value())
  38. style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
  39. return;
  40. } else if (name == HTML::AttributeNames::background) {
  41. if (auto parsed_value = document().parse_url(value); parsed_value.is_valid())
  42. style.set_property(CSS::PropertyID::BackgroundImage, CSS::ImageStyleValue::create(parsed_value));
  43. return;
  44. }
  45. });
  46. }
  47. void HTMLTableRowElement::visit_edges(Cell::Visitor& visitor)
  48. {
  49. Base::visit_edges(visitor);
  50. visitor.visit(m_cells);
  51. }
  52. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-cells
  53. JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableRowElement::cells() const
  54. {
  55. // The cells attribute must return an HTMLCollection rooted at this tr element,
  56. // whose filter matches only td and th elements that are children of the tr element.
  57. if (!m_cells) {
  58. m_cells = DOM::HTMLCollection::create(const_cast<HTMLTableRowElement&>(*this), DOM::HTMLCollection::Scope::Children, [](Element const& element) {
  59. return is<HTMLTableCellElement>(element);
  60. });
  61. }
  62. return *m_cells;
  63. }
  64. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-rowindex
  65. int HTMLTableRowElement::row_index() const
  66. {
  67. // The rowIndex attribute must, if this element has a parent table element,
  68. // or a parent tbody, thead, or tfoot element and a grandparent table element,
  69. // return the index of this tr element in that table element's rows collection.
  70. // If there is no such table element, then the attribute must return −1.
  71. auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
  72. if (!parent())
  73. return nullptr;
  74. if (is<HTMLTableElement>(*parent()))
  75. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
  76. if (is<HTMLTableSectionElement>(*parent()) && parent()->parent() && is<HTMLTableElement>(*parent()->parent()))
  77. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent()->parent())).rows();
  78. return nullptr;
  79. }();
  80. if (!rows_collection)
  81. return -1;
  82. auto rows = rows_collection->collect_matching_elements();
  83. for (size_t i = 0; i < rows.size(); ++i) {
  84. if (rows[i] == this)
  85. return i;
  86. }
  87. return -1;
  88. }
  89. int HTMLTableRowElement::section_row_index() const
  90. {
  91. // The sectionRowIndex attribute must, if this element has a parent table, tbody, thead, or tfoot element,
  92. // return the index of the tr element in the parent element's rows collection
  93. // (for tables, that's HTMLTableElement's rows collection; for table sections, that's HTMLTableSectionElement's rows collection).
  94. // If there is no such parent element, then the attribute must return −1.
  95. auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
  96. if (!parent())
  97. return nullptr;
  98. if (is<HTMLTableElement>(*parent()))
  99. return const_cast<HTMLTableElement&>(static_cast<HTMLTableElement const&>(*parent())).rows();
  100. if (is<HTMLTableSectionElement>(*parent()))
  101. return static_cast<HTMLTableSectionElement const&>(*parent()).rows();
  102. return nullptr;
  103. }();
  104. if (!rows_collection)
  105. return -1;
  106. auto rows = rows_collection->collect_matching_elements();
  107. for (size_t i = 0; i < rows.size(); ++i) {
  108. if (rows[i] == this)
  109. return i;
  110. }
  111. return -1;
  112. }
  113. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-insertcell
  114. WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLTableCellElement>> HTMLTableRowElement::insert_cell(i32 index)
  115. {
  116. auto cells_collection = cells();
  117. auto cells_collection_size = static_cast<i32>(cells_collection->length());
  118. // 1. If index is less than −1 or greater than the number of elements in the cells collection, then throw an "IndexSizeError" DOMException.
  119. if (index < -1 || index > cells_collection_size)
  120. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than the number of cells"_fly_string);
  121. // 2. Let table cell be the result of creating an element given this tr element's node document, td, and the HTML namespace.
  122. auto& table_cell = static_cast<HTMLTableCellElement&>(*TRY(DOM::create_element(document(), HTML::TagNames::td, Namespace::HTML)));
  123. // 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.
  124. if (index == -1 || index == cells_collection_size)
  125. TRY(append_child(table_cell));
  126. // 4. Otherwise, insert table cell as a child of this tr element, immediately before the indexth td or th element in the cells collection.
  127. else
  128. insert_before(table_cell, cells_collection->item(index));
  129. // 5. Return table cell.
  130. return JS::NonnullGCPtr(table_cell);
  131. }
  132. // https://html.spec.whatwg.org/multipage/tables.html#dom-tr-deletecell
  133. WebIDL::ExceptionOr<void> HTMLTableRowElement::delete_cell(i32 index)
  134. {
  135. auto cells_collection = cells();
  136. auto cells_collection_size = static_cast<i32>(cells_collection->length());
  137. // 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.
  138. if (index < -1 || index >= cells_collection_size)
  139. return WebIDL::IndexSizeError::create(realm(), "Index is negative or greater than or equal to the number of cells"_fly_string);
  140. // 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.
  141. if (index == -1) {
  142. if (cells_collection_size > 0)
  143. cells_collection->item(cells_collection_size - 1)->remove();
  144. }
  145. // 3. Otherwise, remove the indexth element in the cells collection from its parent.
  146. else {
  147. cells_collection->item(index)->remove();
  148. }
  149. return {};
  150. }
  151. }