HTMLTableRowElement.cpp 7.8 KB

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