HTMLTableElement.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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/Parser.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/HTML/Parser/HTMLParser.h>
  14. #include <LibWeb/Namespace.h>
  15. namespace Web::HTML {
  16. HTMLTableElement::HTMLTableElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  17. : HTMLElement(document, move(qualified_name))
  18. {
  19. }
  20. HTMLTableElement::~HTMLTableElement() = default;
  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_nonzero_dimension_value(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_nonzero_dimension_value(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: This is not always the case, but this function is currently written in a way that assumes non-null.
  49. VERIFY(caption);
  50. // FIXME: The spec requires deleting the current caption if caption is null
  51. // Currently the wrapper generator doesn't send us a nullable value
  52. delete_caption();
  53. pre_insert(*caption, first_child());
  54. }
  55. NonnullRefPtr<HTMLTableCaptionElement> HTMLTableElement::create_caption()
  56. {
  57. auto maybe_caption = caption();
  58. if (maybe_caption) {
  59. return *maybe_caption;
  60. }
  61. auto caption = DOM::create_element(document(), TagNames::caption, Namespace::HTML);
  62. pre_insert(caption, first_child());
  63. return static_ptr_cast<HTMLTableCaptionElement>(caption);
  64. }
  65. void HTMLTableElement::delete_caption()
  66. {
  67. auto maybe_caption = caption();
  68. if (maybe_caption) {
  69. maybe_caption->remove(false);
  70. }
  71. }
  72. RefPtr<HTMLTableSectionElement> HTMLTableElement::t_head()
  73. {
  74. for (auto* child = first_child(); child; child = child->next_sibling()) {
  75. if (is<HTMLTableSectionElement>(*child)) {
  76. auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
  77. if (table_section_element->local_name() == TagNames::thead)
  78. return table_section_element;
  79. }
  80. }
  81. return nullptr;
  82. }
  83. DOM::ExceptionOr<void> HTMLTableElement::set_t_head(HTMLTableSectionElement* thead)
  84. {
  85. // FIXME: This is not always the case, but this function is currently written in a way that assumes non-null.
  86. VERIFY(thead);
  87. if (thead->local_name() != TagNames::thead)
  88. return DOM::HierarchyRequestError::create("Element is not thead");
  89. // FIXME: The spec requires deleting the current thead if thead is null
  90. // Currently the wrapper generator doesn't send us a nullable value
  91. delete_t_head();
  92. // We insert the new thead after any <caption> or <colgroup> elements
  93. DOM::Node* child_to_append_after = nullptr;
  94. for (auto* child = first_child(); child; child = child->next_sibling()) {
  95. if (!is<HTMLElement>(*child))
  96. continue;
  97. if (is<HTMLTableCaptionElement>(*child))
  98. continue;
  99. if (is<HTMLTableColElement>(*child)) {
  100. auto table_col_element = &verify_cast<HTMLTableColElement>(*child);
  101. if (table_col_element->local_name() == TagNames::colgroup)
  102. continue;
  103. }
  104. // We have found an element which is not a <caption> or <colgroup>, we'll insert before this
  105. child_to_append_after = child;
  106. break;
  107. }
  108. pre_insert(*thead, child_to_append_after);
  109. return {};
  110. }
  111. NonnullRefPtr<HTMLTableSectionElement> HTMLTableElement::create_t_head()
  112. {
  113. auto maybe_thead = t_head();
  114. if (maybe_thead)
  115. return *maybe_thead;
  116. auto thead = DOM::create_element(document(), TagNames::thead, Namespace::HTML);
  117. // We insert the new thead after any <caption> or <colgroup> elements
  118. DOM::Node* child_to_append_after = nullptr;
  119. for (auto* child = first_child(); child; child = child->next_sibling()) {
  120. if (!is<HTMLElement>(*child))
  121. continue;
  122. if (is<HTMLTableCaptionElement>(*child))
  123. continue;
  124. if (is<HTMLTableColElement>(*child)) {
  125. auto table_col_element = &verify_cast<HTMLTableColElement>(*child);
  126. if (table_col_element->local_name() == TagNames::colgroup)
  127. continue;
  128. }
  129. // We have found an element which is not a <caption> or <colgroup>, we'll insert before this
  130. child_to_append_after = child;
  131. break;
  132. }
  133. pre_insert(thead, child_to_append_after);
  134. return static_ptr_cast<HTMLTableSectionElement>(thead);
  135. }
  136. void HTMLTableElement::delete_t_head()
  137. {
  138. auto maybe_thead = t_head();
  139. if (maybe_thead) {
  140. maybe_thead->remove(false);
  141. }
  142. }
  143. RefPtr<HTMLTableSectionElement> HTMLTableElement::t_foot()
  144. {
  145. for (auto* child = first_child(); child; child = child->next_sibling()) {
  146. if (is<HTMLTableSectionElement>(*child)) {
  147. auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
  148. if (table_section_element->local_name() == TagNames::tfoot)
  149. return table_section_element;
  150. }
  151. }
  152. return nullptr;
  153. }
  154. DOM::ExceptionOr<void> HTMLTableElement::set_t_foot(HTMLTableSectionElement* tfoot)
  155. {
  156. // FIXME: This is not always the case, but this function is currently written in a way that assumes non-null.
  157. VERIFY(tfoot);
  158. if (tfoot->local_name() != TagNames::tfoot)
  159. return DOM::HierarchyRequestError::create("Element is not tfoot");
  160. // FIXME: The spec requires deleting the current tfoot if tfoot is null
  161. // Currently the wrapper generator doesn't send us a nullable value
  162. delete_t_foot();
  163. // We insert the new tfoot at the end of the table
  164. append_child(*tfoot);
  165. return {};
  166. }
  167. NonnullRefPtr<HTMLTableSectionElement> HTMLTableElement::create_t_foot()
  168. {
  169. auto maybe_tfoot = t_foot();
  170. if (maybe_tfoot)
  171. return *maybe_tfoot;
  172. auto tfoot = DOM::create_element(document(), TagNames::tfoot, Namespace::HTML);
  173. append_child(tfoot);
  174. return static_ptr_cast<HTMLTableSectionElement>(tfoot);
  175. }
  176. void HTMLTableElement::delete_t_foot()
  177. {
  178. auto maybe_tfoot = t_foot();
  179. if (maybe_tfoot) {
  180. maybe_tfoot->remove(false);
  181. }
  182. }
  183. NonnullRefPtr<DOM::HTMLCollection> HTMLTableElement::t_bodies()
  184. {
  185. return DOM::HTMLCollection::create(*this, [](DOM::Element const& element) {
  186. return element.local_name() == TagNames::tbody;
  187. });
  188. }
  189. NonnullRefPtr<HTMLTableSectionElement> HTMLTableElement::create_t_body()
  190. {
  191. auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML);
  192. // We insert the new tbody after the last <tbody> element
  193. DOM::Node* child_to_append_after = nullptr;
  194. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  195. if (!is<HTMLElement>(*child))
  196. continue;
  197. if (is<HTMLTableSectionElement>(*child)) {
  198. auto table_section_element = &verify_cast<HTMLTableSectionElement>(*child);
  199. if (table_section_element->local_name() == TagNames::tbody) {
  200. // We have found an element which is a <tbody> we'll insert after this
  201. child_to_append_after = child->next_sibling();
  202. break;
  203. }
  204. }
  205. }
  206. pre_insert(tbody, child_to_append_after);
  207. return static_ptr_cast<HTMLTableSectionElement>(tbody);
  208. }
  209. NonnullRefPtr<DOM::HTMLCollection> HTMLTableElement::rows()
  210. {
  211. HTMLTableElement* table_node = this;
  212. // FIXME: The elements in the collection must be ordered such that those elements whose parent is a thead are
  213. // included first, in tree order, followed by those elements whose parent is either a table or tbody
  214. // element, again in tree order, followed finally by those elements whose parent is a tfoot element,
  215. // still in tree order.
  216. // How do you sort HTMLCollection?
  217. return DOM::HTMLCollection::create(*this, [table_node](DOM::Element const& element) {
  218. // Only match TR elements which are:
  219. // * children of the table element
  220. // * children of the thead, tbody, or tfoot elements that are themselves children of the table element
  221. if (!is<HTMLTableRowElement>(element)) {
  222. return false;
  223. }
  224. if (element.parent_element() == table_node)
  225. return true;
  226. if (element.parent_element() && (element.parent_element()->local_name() == TagNames::thead || element.parent_element()->local_name() == TagNames::tbody || element.parent_element()->local_name() == TagNames::tfoot)
  227. && element.parent()->parent() == table_node) {
  228. return true;
  229. }
  230. return false;
  231. });
  232. }
  233. DOM::ExceptionOr<NonnullRefPtr<HTMLTableRowElement>> HTMLTableElement::insert_row(long index)
  234. {
  235. auto rows = this->rows();
  236. auto rows_length = rows->length();
  237. if (index < -1 || index > (long)rows_length) {
  238. return DOM::IndexSizeError::create("Index is negative or greater than the number of rows");
  239. }
  240. auto tr = static_ptr_cast<HTMLTableRowElement>(DOM::create_element(document(), TagNames::tr, Namespace::HTML));
  241. if (rows_length == 0 && !has_child_of_type<HTMLTableRowElement>()) {
  242. auto tbody = DOM::create_element(document(), TagNames::tbody, Namespace::HTML);
  243. tbody->append_child(tr);
  244. append_child(tbody);
  245. } else if (rows_length == 0) {
  246. auto tbody = last_child_of_type<HTMLTableRowElement>();
  247. tbody->append_child(tr);
  248. } else if (index == -1 || index == (long)rows_length) {
  249. auto parent_of_last_tr = rows->item(rows_length - 1)->parent_element();
  250. parent_of_last_tr->append_child(tr);
  251. } else {
  252. rows->item(index)->parent_element()->insert_before(tr, rows->item(index));
  253. }
  254. return tr;
  255. }
  256. // https://html.spec.whatwg.org/multipage/tables.html#dom-table-deleterow
  257. DOM::ExceptionOr<void> HTMLTableElement::delete_row(long index)
  258. {
  259. auto rows = this->rows();
  260. auto rows_length = rows->length();
  261. // 1. If index is less than −1 or greater than or equal to the number of elements in the rows collection, then throw an "IndexSizeError" DOMException.
  262. if (index < -1 || index >= (long)rows_length)
  263. return DOM::IndexSizeError::create("Index is negative or greater than or equal to the number of rows");
  264. // 2. If index is −1, then remove the last element in the rows collection from its parent, or do nothing if the rows collection is empty.
  265. if (index == -1) {
  266. if (rows_length == 0)
  267. return {};
  268. auto row_to_remove = rows->item(rows_length - 1);
  269. row_to_remove->remove(false);
  270. return {};
  271. }
  272. // 3. Otherwise, remove the indexth element in the rows collection from its parent.
  273. auto row_to_remove = rows->item(index);
  274. row_to_remove->remove(false);
  275. return {};
  276. }
  277. }