HTMLTableElement.cpp 14 KB

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