HTMLTableRowElement.cpp 8.1 KB

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