HTMLTableElement.cpp 15 KB

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