HTMLTableElement.cpp 12 KB

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