HTMLTableRowElement.cpp 8.6 KB

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