HTMLTableElement.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Parser/DeprecatedCSSParser.h>
  7. #include <LibWeb/DOM/ElementFactory.h>
  8. #include <LibWeb/DOM/HTMLCollection.h>
  9. #include <LibWeb/HTML/HTMLTableColElement.h>
  10. #include <LibWeb/HTML/HTMLTableElement.h>
  11. #include <LibWeb/HTML/HTMLTableRowElement.h>
  12. #include <LibWeb/Namespace.h>
  13. namespace Web::HTML {
  14. HTMLTableElement::HTMLTableElement(DOM::Document& document, QualifiedName qualified_name)
  15. : HTMLElement(document, move(qualified_name))
  16. {
  17. }
  18. HTMLTableElement::~HTMLTableElement()
  19. {
  20. }
  21. void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) const
  22. {
  23. for_each_attribute([&](auto& name, auto& value) {
  24. if (name == HTML::AttributeNames::width) {
  25. if (auto parsed_value = parse_html_length(document(), value))
  26. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  27. return;
  28. }
  29. if (name == HTML::AttributeNames::height) {
  30. if (auto parsed_value = parse_html_length(document(), value))
  31. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  32. return;
  33. }
  34. if (name == HTML::AttributeNames::bgcolor) {
  35. auto color = Color::from_string(value);
  36. if (color.has_value())
  37. style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
  38. return;
  39. }
  40. });
  41. }
  42. RefPtr<HTMLTableCaptionElement> HTMLTableElement::caption()
  43. {
  44. return first_child_of_type<HTMLTableCaptionElement>();
  45. }
  46. void HTMLTableElement::set_caption(HTMLTableCaptionElement& caption)
  47. {
  48. // FIXME: The spec requires deleting the current caption if caption is null
  49. // Currently the wrapper generator doesn't send us a nullable value
  50. delete_caption();
  51. pre_insert(caption, first_child());
  52. }
  53. NonnullRefPtr<HTMLTableCaptionElement> HTMLTableElement::create_caption()
  54. {
  55. auto maybe_caption = caption();
  56. if (maybe_caption) {
  57. return *maybe_caption;
  58. }
  59. auto caption = DOM::create_element(document(), TagNames::caption, Namespace::HTML);
  60. pre_insert(caption, first_child());
  61. return caption;
  62. }
  63. void HTMLTableElement::delete_caption()
  64. {
  65. auto maybe_caption = caption();
  66. if (maybe_caption) {
  67. maybe_caption->remove(false);
  68. }
  69. }
  70. RefPtr<HTMLTableSectionElement> HTMLTableElement::t_head()
  71. {
  72. for (auto* child = first_child(); child; child = child->next_sibling()) {
  73. if (is<HTMLTableSectionElement>(*child)) {
  74. auto table_section_element = &downcast<HTMLTableSectionElement>(*child);
  75. if (table_section_element->tag_name() == TagNames::thead)
  76. return table_section_element;
  77. }
  78. }
  79. return nullptr;
  80. }
  81. DOM::ExceptionOr<void> HTMLTableElement::set_t_head(HTMLTableSectionElement& thead)
  82. {
  83. if (thead.tag_name() != TagNames::thead)
  84. return DOM::HierarchyRequestError::create("Element is not thead");
  85. // FIXME: The spec requires deleting the current thead if thead is null
  86. // Currently the wrapper generator doesn't send us a nullable value
  87. delete_t_head();
  88. // We insert the new thead after any <caption> or <colgroup> elements
  89. DOM::Node* child_to_append_after = nullptr;
  90. for (auto* child = first_child(); child; child = child->next_sibling()) {
  91. if (!is<HTMLElement>(*child))
  92. continue;
  93. if (is<HTMLTableCaptionElement>(*child))
  94. continue;
  95. if (is<HTMLTableColElement>(*child)) {
  96. auto table_col_element = &downcast<HTMLTableColElement>(*child);
  97. if (table_col_element->tag_name() == TagNames::colgroup)
  98. continue;
  99. }
  100. // We have found an element which is not a <caption> or <colgroup>, we'll insert before this
  101. child_to_append_after = child;
  102. break;
  103. }
  104. pre_insert(thead, child_to_append_after);
  105. return {};
  106. }
  107. NonnullRefPtr<HTMLTableSectionElement> HTMLTableElement::create_t_head()
  108. {
  109. auto maybe_thead = t_head();
  110. if (maybe_thead)
  111. return *maybe_thead;
  112. auto thead = DOM::create_element(document(), TagNames::thead, Namespace::HTML);
  113. // We insert the new thead after any <caption> or <colgroup> elements
  114. DOM::Node* child_to_append_after = nullptr;
  115. for (auto* child = first_child(); child; child = child->next_sibling()) {
  116. if (!is<HTMLElement>(*child))
  117. continue;
  118. if (is<HTMLTableCaptionElement>(*child))
  119. continue;
  120. if (is<HTMLTableColElement>(*child)) {
  121. auto table_col_element = &downcast<HTMLTableColElement>(*child);
  122. if (table_col_element->tag_name() == TagNames::colgroup)
  123. continue;
  124. }
  125. // We have found an element which is not a <caption> or <colgroup>, we'll insert before this
  126. child_to_append_after = child;
  127. break;
  128. }
  129. pre_insert(thead, child_to_append_after);
  130. return thead;
  131. }
  132. void HTMLTableElement::delete_t_head()
  133. {
  134. auto maybe_thead = t_head();
  135. if (maybe_thead) {
  136. maybe_thead->remove(false);
  137. }
  138. }
  139. RefPtr<HTMLTableSectionElement> HTMLTableElement::t_foot()
  140. {
  141. for (auto* child = first_child(); child; child = child->next_sibling()) {
  142. if (is<HTMLTableSectionElement>(*child)) {
  143. auto table_section_element = &downcast<HTMLTableSectionElement>(*child);
  144. if (table_section_element->tag_name() == TagNames::tfoot)
  145. return table_section_element;
  146. }
  147. }
  148. return nullptr;
  149. }
  150. DOM::ExceptionOr<void> HTMLTableElement::set_t_foot(HTMLTableSectionElement& tfoot)
  151. {
  152. if (tfoot.tag_name() != TagNames::tfoot)
  153. return DOM::HierarchyRequestError::create("Element is not tfoot");
  154. // FIXME: The spec requires deleting the current tfoot if tfoot is null
  155. // Currently the wrapper generator doesn't send us a nullable value
  156. delete_t_foot();
  157. // We insert the new tfoot at the end of the table
  158. append_child(tfoot);
  159. return {};
  160. }
  161. NonnullRefPtr<HTMLTableSectionElement> HTMLTableElement::create_t_foot()
  162. {
  163. auto maybe_tfoot = t_foot();
  164. if (maybe_tfoot)
  165. return *maybe_tfoot;
  166. auto tfoot = DOM::create_element(document(), TagNames::tfoot, Namespace::HTML);
  167. append_child(tfoot);
  168. return tfoot;
  169. }
  170. void HTMLTableElement::delete_t_foot()
  171. {
  172. auto maybe_tfoot = t_foot();
  173. if (maybe_tfoot) {
  174. maybe_tfoot->remove(false);
  175. }
  176. }
  177. NonnullRefPtr<DOM::HTMLCollection> HTMLTableElement::rows()
  178. {
  179. HTMLTableElement* table_node = this;
  180. // FIXME: The elements in the collection must be ordered such that those elements whose parent is a thead are
  181. // included first, in tree order, followed by those elements whose parent is either a table or tbody
  182. // element, again in tree order, followed finally by those elements whose parent is a tfoot element,
  183. // still in tree order.
  184. // How do you sort HTMLCollection?
  185. return DOM::HTMLCollection::create(*this, [table_node](DOM::Element const& element) {
  186. // Only match TR elements which are:
  187. // * children of the table element
  188. // * children of the thead, tbody, or tfoot elements that are themselves children of the table element
  189. if (!is<HTMLTableRowElement>(element)) {
  190. return false;
  191. }
  192. if (element.parent_element() == table_node)
  193. return true;
  194. if (element.parent_element() && (element.parent_element()->tag_name() == TagNames::thead || element.parent_element()->tag_name() == TagNames::tbody || element.parent_element()->tag_name() == TagNames::tfoot)
  195. && element.parent()->parent() == table_node) {
  196. return true;
  197. }
  198. return false;
  199. });
  200. }
  201. DOM::ExceptionOr<NonnullRefPtr<HTMLTableRowElement>> HTMLTableElement::insert_row(long index)
  202. {
  203. auto rows = this->rows();
  204. auto rows_length = rows->length();
  205. if (index < -1 || index >= (long)rows_length) {
  206. return DOM::IndexSizeError::create("Index is negative or greater than the number of rows");
  207. }
  208. auto tr = static_cast<NonnullRefPtr<HTMLTableRowElement>>(DOM::create_element(document(), TagNames::tr, Namespace::HTML));
  209. if (rows_length == 0 && !has_child_of_type<HTMLTableRowElement>()) {
  210. auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML);
  211. tbody->append_child(tr);
  212. append_child(tbody);
  213. } else if (rows_length == 0) {
  214. auto tbody = last_child_of_type<HTMLTableRowElement>();
  215. tbody->append_child(tr);
  216. } else if (index == -1 || index == (long)rows_length) {
  217. auto parent_of_last_tr = rows->item(rows_length - 1)->parent_element();
  218. parent_of_last_tr->append_child(tr);
  219. } else {
  220. rows->item(index)->parent_element()->insert_before(tr, rows->item(index));
  221. }
  222. return tr;
  223. }
  224. DOM::ExceptionOr<void> HTMLTableElement::delete_row(long index)
  225. {
  226. auto rows = this->rows();
  227. auto rows_length = rows->length();
  228. if (index < -1 || index >= (long)rows_length) {
  229. return DOM::IndexSizeError::create("Index is negative or greater than the number of rows");
  230. }
  231. if (index == -1 && rows_length > 0) {
  232. auto row_to_remove = rows->item(rows_length - 1);
  233. row_to_remove->remove(false);
  234. } else {
  235. auto row_to_remove = rows->item(index);
  236. row_to_remove->remove(false);
  237. }
  238. return {};
  239. }
  240. }