HTMLTableElement.cpp 15 KB

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