HTMLTableRowElement.cpp 7.9 KB

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