HTMLTableElement.cpp 13 KB

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