HTMLTableElement.cpp 12 KB

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