HTMLTableElement.cpp 10 KB

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