HTMLTableElement.cpp 15 KB

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